Java else Statement
The else statement in Java is used along with the if statement to define an alternative block of code that executes when the condition in the if statement is false. Together, if and else help in decision-making, allowing your Java program to take different execution paths depending on whether a condition is true or false.
In simple terms, while the if block handles the case when a condition is true, the else block ensures that there’s a defined response when that condition is not met. This makes your program more predictable and logical.
Syntax of the if-else Statement
if (condition) {
// Executes when the condition is true
} else {
// Executes when the condition is false
}
Here’s how it works:
- The condition is a boolean expression that returns either true or false.
- If the condition evaluates to true, the code inside the if block runs.
- If the condition evaluates to false, the code inside the else block executes.
Example 1: Checking if a Number is Positive or Negative
public class PositiveNegativeCheck {
public static void main(String[] args) {
int number = -5;
if (number >= 0) {
System.out.println(“The number is positive.”);
} else {
System.out.println(“The number is negative.”);
}
}
}
Output:
The number is negative.
Explanation:
Here, the condition number >= 0 evaluates to false because number is -5. Therefore, the code inside the else block executes.
Example 2: Checking Even or Odd Number
public class EvenOddCheck {
public static void main(String[] args) {
int num = 7;
if (num % 2 == 0) {
System.out.println(“The number is even.”);
} else {
System.out.println(“The number is odd.”);
}
}
}
Output:
The number is odd.
Explanation:
The condition num % 2 == 0 checks if the number is divisible by 2. Since 7 % 2 gives 1, the condition is false, and the else block prints that the number is odd.
Example 3: Checking Voting Eligibility
public class VotingEligibility {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println(“You are eligible to vote.”);
} else {
System.out.println(“You are not eligible to vote.”);
}
}
}
Output:
You are not eligible to vote.
Explanation:
The condition age >= 18 evaluates to false since the person’s age is 16. Therefore, the code inside the else block executes, printing the message that the person is not eligible.
Example 4: Using else with Logical Operators
public class LoginCheck {
public static void main(String[] args) {
String username = “admin”;
String password = “wrongpass”;
if (username.equals(“admin”) && password.equals(“12345”)) {
System.out.println(“Login successful!”);
} else {
System.out.println(“Invalid username or password!”);
}
}
}
Output:
Invalid username or password!
Explanation:
Both username and password need to match for a successful login. Since the password is incorrect, the if condition fails, and the else block displays an error message.
Single-Line if-else Without Braces
If there’s only one statement in each block, you can omit the curly braces {}.
int x = 10;
if (x > 5) System.out.println(“x is greater than 5”);
else System.out.println(“x is less than or equal to 5”);
Output:
x is greater than 5
Note: Although omitting braces makes the code shorter, it’s generally recommended to use braces for better readability and to avoid logical errors in complex programs.
Key Points to Remember:
- The else block is optional — it only runs if the if condition is false.
- There can be only one else block for each if statement.
- You can nest if-else statements for more complex decision-making.
- Use indentation properly for clarity when using multiple if-else blocks.
Example 5: Nested if-else
public class NumberCheck {
public static void main(String[] args) {
int num = 0;
if (num > 0) {
System.out.println(“Positive number”);
} else {
if (num < 0) {
System.out.println(“Negative number”);
} else {
System.out.println(“Number is zero”);
}
}
}
}
Output:
Number is zero
Explanation:
Here, the first if condition checks if the number is positive. Since it’s not, the program moves to the else block, where another if condition checks if it’s negative. If both are false, the final nested else executes.
Conclusion
The else statement in Java complements the if statement by handling the false case of a condition. It ensures that your program can respond appropriately whether a condition is true or not. By using if and else together, you can write programs that are logical, dynamic, and robust — handling multiple decision-making scenarios efficiently.