loading

else

Java Else

The else Statement

To indicate a block of code to be performed in the event that the condition is false, use the else statement.

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

				
					int time = 20;
if (time < 18) {
  System.out.println("Good day.");
} else {
  System.out.println("Good evening.");
}
// Outputs "Good evening."
				
			

Example explained

The preceding example demonstrates that the condition is false since time (20) is larger than 18. As a result, we print “Good evening” on the screen and go on to the else condition. “Good day” would be printed by the program if the time was less than 18.

Share this Doc

else

Or copy link

Explore Topic