Utilize the else statement to designate a block of code for execution if the condition evaluates to false.
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 } |
int time = 20; if (time < 18) { printf(“Good day.”); } else { printf(“Good evening.”); } // Outputs “Good evening.” |
In the provided example, since the time (20) exceeds 18, the condition is false. Consequently, the program proceeds to the else condition and outputs “Good evening” to the screen. Had the time been less than 18, the program would have printed “Good day”.