Java If-Else
Conditional statements let your program make decisions. Java provides if, else, else if, and switch to control the flow of execution based on conditions.
1. Simple if Statement
int age = 20;
if (age >= 18) {
System.out.println("You are an adult");
}
Java requires curly braces for multi-statement blocks. For single statements, braces are optional but recommended — always use them.
2. if-else
int age = 15;
if (age >= 18) {
System.out.println("You can vote");
} else {
System.out.println("You cannot vote yet");
}
3. if-else-if Ladder
int marks = 78;
if (marks >= 90) {
System.out.println("Grade: A+");
} else if (marks >= 80) {
System.out.println("Grade: A");
} else if (marks >= 70) {
System.out.println("Grade: B");
} else if (marks >= 60) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
// Output: Grade: B
4. Nested if
int age = 25;
boolean hasLicense = true;
if (age >= 18) {
if (hasLicense) {
System.out.println("You can drive");
} else {
System.out.println("Get a license first");
}
} else {
System.out.println("Too young to drive");
}
5. switch Statement
Efficient alternative to long if-else-if chains when comparing one value against multiple cases.
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
default:
System.out.println("Weekend");
}
// Output: Wednesday
Why break?
Without break, execution "falls through" to the next case. This can be intentional or a bug.
int x = 2;
switch (x) {
case 1:
case 2:
case 3:
System.out.println("1, 2, or 3");
break;
default:
System.out.println("Other");
}
6. Enhanced Switch (Java 14+)
Modern Java allows arrow syntax — no break needed, no fall-through.
int day = 3;
String name = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4, 5 -> "Weekday";
default -> "Weekend";
};
System.out.println(name); // Wednesday
7. Ternary Operator (One-line if-else)
int age = 20;
String status = (age >= 18) ? "Adult" : "Minor";
System.out.println(status); // Adult
switch vs if-else — When to Use Which?
- switch — comparing ONE variable against many constant values. Cleaner and faster.
- if-else — complex conditions with ranges, logical operators, or different variable comparisons.
Real Example — Login Validation
String username = "admin";
String password = "java123";
if (username.equals("admin") && password.equals("java123")) {
System.out.println("Login successful");
} else if (!username.equals("admin")) {
System.out.println("Invalid username");
} else {
System.out.println("Wrong password");
}
Real Example — Grade Calculator with switch
char grade = 'B';
switch (grade) {
case 'A':
System.out.println("Excellent! 90-100");
break;
case 'B':
System.out.println("Good! 80-89");
break;
case 'C':
System.out.println("Average! 70-79");
break;
case 'D':
System.out.println("Below Average! 60-69");
break;
default:
System.out.println("Fail");
}
Use enhanced switch (Java 14+) whenever possible — cleaner syntax, no accidental fall-through. Older projects may still use classic switch, so know both.
Write a Java program that takes a number (1-7) as input and prints the day of the week using switch. Then write another program that takes marks and prints grade using if-else-if. Use ternary to print "Pass" if marks >= 40 else "Fail".
Quick Revision
if— runs block if condition is true.if-else— two-way branching.if-else-if— multiple conditions.switch— compare one value against many constants.break— prevents fall-through in switch.- Enhanced switch (Java 14+):
case X -> value. - Ternary:
cond ? a : b. - Use
.equals()for String comparison in conditions.
Interview Questions
Q: Difference between if-else and switch?
A: switch works only with specific types (int, char, String, enum) and compares one value against many constants. if-else supports any boolean expression, ranges, and complex conditions. switch is often cleaner for menu-like choices.
Q: Why is break needed in switch?
A: Without break, execution continues into the next case ("fall-through"). This is sometimes useful but usually a bug. Modern Java's enhanced switch eliminates this issue.
Q: Can switch work with long, float, or double?
A: No. switch supports byte, short, char, int, String, and enum. It does NOT support long, float, double, or boolean.
Q: What is the difference between == and .equals() in conditions?
A: == compares references for objects (memory location). .equals() compares content. For String comparison, always use .equals(). if (str1 == str2) can be unreliable.
Q: What is the enhanced switch?
A: Introduced in Java 14, it uses arrow syntax: case 1 -> value;. It eliminates the need for break and prevents accidental fall-through. It can also return values directly.
Want to Master Java?
Join our Java course in Varanasi — practical classes and career support at our Sarnath centre.