The else Statement
The else statement in C programming is a crucial part of conditional statements, allowing you to specify alternative code to be executed based on whether the condition in an if statement is true or false.
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}Example
#include <stdio.h>
int main() {
int time = 20;
if (time < 18) {
printf("Good day.");
} else {
printf("Good By.");
}
return 0;
}
Output
Good By.
Example explained
In the above example, time(20) is greater than 18, so the condition is wrong. Because of this, we go to another position and print "Good Evening" on the screen. If the time was less than 18, the program would print "Good day".