Understanding the Java else Statement

Learn how to effectively use the else statement in Java to control the flow of your program. This guide explains its purpose in handling false conditions following an if statement, with examples to illustrate its application.



The else Statement

Use the else statement to specify a block of Java code to be executed if the condition in the preceding if statement 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
}

Example:

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

Example Explained: In the example above, time (20) is greater than 18, so the condition time < 18 is false. As a result, the program moves to the else block and executes System.out.println("Good evening."); which prints "Good evening" to the screen. If time had been less than 18, the program would have printed "Good day."

h3>The else if Statement

Use the else if statement to specify a new condition if the preceding if statement is false.

if (condition1) {
  // block of code to be executed if condition1 is true
} else if (condition2) {
  // block of code to be executed if condition1 is false and condition2 is true
} else {
  // block of code to be executed if both condition1 and condition2 are false
}

Example:

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

Example Explained: In the example above, time (22) is greater than 10, so the first condition time < 10 is false. The program then checks the next condition in the else if statement, time < 18, which is also false. As a result, the program moves to the else block and executes System.out.println("Good evening."); which prints "Good evening" to the screen. If time had been 14, the program would have printed "Good day."

Java If...Else Examples

Example 1: Opening a door with a code

int doorCode = 1337;

if (doorCode == 1337) {
  System.out.println("Correct code. The door is now open.");
} else {
  System.out.println("Wrong code. The door remains closed.");
}

Explanation: In this example, we check if the doorCode matches the correct code (1337). If it does, the message "Correct code. The door is now open." is printed. Otherwise, if the code is incorrect, the message "Wrong code. The door remains closed." is printed.


Example 2: Checking if a number is positive or negative

int myNum = 10;

if (myNum > 0) {
  System.out.println("The value is a positive number.");
} else if (myNum < 0) {
  System.out.println("The value is a negative number.");
} else {
  System.out.println("The value is 0.");
}

Explanation: Here, we determine whether myNum is positive, negative, or zero using if...else if...else statements. Depending on the value of myNum, the appropriate message is printed.


Example 3: Checking if a person is old enough to vote

int myAge = 25;
int votingAge = 18;

if (myAge >= votingAge) {
  System.out.println("Old enough to vote!");
} else {
  System.out.println("Not old enough to vote.");
}

Explanation: This example checks if myAge is greater than or equal to votingAge (18). If true, it prints "Old enough to vote!". Otherwise, it prints "Not old enough to vote.". This demonstrates how if...else statements can be used for decision-making based on conditions.


Example 4: Checking if a number is even or odd

int myNum = 5;

if (myNum % 2 == 0) {
  System.out.println(myNum + " is even");
} else {
  System.out.println(myNum + " is odd");
}

Explanation: Here, we use the modulus operator (%) to check if myNum is even or odd. If the remainder when divided by 2 is 0, it prints "myNum is even". Otherwise, it prints "myNum is odd". This showcases how if...else statements can be used to perform different actions based on the result of a condition.