Utilize the else if statement to introduce a new condition in case the initial condition is false.
if (condition1) { // block of code to be executed if condition1 is true // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false |
int time = 22; |
In the example provided, since the time (22) is greater than 10, the first condition is false. Subsequently, the following condition within the else if statement is also false. Consequently, as both condition1 and condition2 are false, we proceed to the else block and print “Good evening” to the screen.
If the time were 14, our program would output “Good day.”