Use an else statement to define a block of code that will be executed if the condition is 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; System.out.println(“Good day.”); } else { System.out.println(“Good evening.”); |
In the example above, the time (20) is greater than 18, making the condition false. As a result, we proceed to the else block and print “Good evening” to the screen. If the time were less than 18, the program would print “Good day”.