Java else Statement - Complete Guide
Introduction to else Statement
The else statement in Java works with if to define an alternative block of code that runs when the if condition evaluates to false. This makes decision-making in programs more complete by covering both outcomes.
Using else ensures that your program always has a defined action to take, whether a condition is true or false.
Basic if-else Syntax
The if-else structure lets you execute one block of code if a condition is true, and another block if it is false.
Example
public class ElseStatement {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
if (number % 2 == 0) {
System.out.println("The number is even.");
} else {
System.out.println("The number is odd.");
}
int age = 16;
if (age >= 18) {
System.out.println("You are an adult.");
System.out.println("You can vote.");
} else {
System.out.println("You are a minor.");
System.out.println("You cannot vote yet.");
}
int score = 75;
if (score >= 90) {
System.out.println("Grade: A");
} else {
if (score >= 80) {
System.out.println("Grade: B");
} else {
if (score >= 70) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
}
}
}
}
Output
The number is positive. The number is even. You are a minor. You cannot vote yet. Grade: C
Common Use Cases for else
The else statement is often used when you need to handle the false case of a condition, ensuring your program covers both possibilities.
Example
public class ElseUseCases {
public static void main(String[] args) {
String username = "admin";
String password = "password123";
if (username.equals("admin") && password.equals("password123")) {
System.out.println("Login successful!");
} else {
System.out.println("Invalid username or password.");
}
int number = 0;
if (number > 0) {
System.out.println("Positive number");
} else if (number < 0) {
System.out.println("Negative number");
} else {
System.out.println("Zero");
}
int age = 25;
if (age < 13) {
System.out.println("Child");
} else if (age < 20) {
System.out.println("Teenager");
} else if (age < 65) {
System.out.println("Adult");
} else {
System.out.println("Senior");
}
boolean fileExists = false;
if (fileExists) {
System.out.println("File found. Processing...");
} else {
System.out.println("File not found. Creating new file...");
}
}
}
Output
Login successful! Zero Adult File not found. Creating new file...
Best Practices for else Statements
- ✅ Use else to clearly define the alternative when an if condition is false
- ✅ Keep else blocks simple and avoid overly complex code
- ✅ Consider early returns to reduce deep nesting of else blocks
- ✅ Use comments to clarify the purpose of else branches if needed
- ✅ Avoid empty else blocks as they reduce clarity
- ✅ Use switch when checking many equality cases instead of long if-else chains
- ✅ Test both the if and else paths to ensure correctness
- ✅ Use else if for multiple exclusive conditions instead of nested if inside else