Decision Making in Java – if, else-if, switch, and Nested Conditions
Decision-making statements in Java control the flow of execution based on conditions. They allow programs to behave dynamically depending on runtime values. Understanding if, else-if, switch, and nested conditions is essential for writing flexible and robust code.
🔍 Why Decision-Making is Important?
- Allows programs to make choices based on data.
- Used in every real-world Java application.
- Critical for implementing algorithms and user-driven logic.
- Frequently asked in Java interviews.
✅ if Statement
The if statement executes a block of code only when the condition is true
.
✅ Syntax:
if(condition) {
// Code executes if condition is true
}
✅ Example:
int age = 20;
if(age >= 18) {
System.out.println("You are eligible to vote.");
}
✅ if-else Statement
The if-else statement provides an alternative block when the condition is false
.
✅ Example:
int number = 5;
if(number % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
✅ else-if Ladder
When multiple conditions need to be checked sequentially, we use an else-if ladder.
✅ Example:
int marks = 75;
if(marks >= 90) {
System.out.println("Grade A");
} else if(marks >= 75) {
System.out.println("Grade B");
} else {
System.out.println("Grade C");
}
✅ switch Statement
The switch statement is used when multiple possible execution paths are based on a single variable or expression.
✅ Syntax:
switch(expression) {
case value1:
// Code block
break;
case value2:
// Code block
break;
default:
// Code block
}
✅ Example:
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;
default: System.out.println("Invalid day");
}
✅ Nested if Statements
You can place if statements inside other if statements for complex conditions.
✅ Example:
int age = 25;
boolean hasID = true;
if(age >= 18) {
if(hasID) {
System.out.println("Access granted");
} else {
System.out.println("ID required");
}
} else {
System.out.println("Underage");
}
🔄 Performance & When to Use
- if-else is better for fewer conditions or range checks.
- switch is efficient when comparing a single variable against multiple values.
- Use nested ifs only when logic requires multiple dependent conditions.
- Java 7+ allows
String
inswitch
. - Java 14+ introduced switch expressions with
yield
.
🚫 Common Mistakes
- Forgetting
break
in switch cases leads to fall-through. - Using nested ifs without braces, making code hard to maintain.
- Overusing nested conditions instead of refactoring into methods.
💡 Tips & Best Practices
- Keep conditions readable and simple.
- Use
switch
for discrete values; use if-else for ranges. - Always include
default
inswitch
. - Prefer enums in switch for type safety.
🧠 Interview Relevance
-
Q: Difference between if-else and switch?
-
A: if-else is flexible for ranges; switch is faster for discrete values.
-
Q: Can we use Strings in switch?
-
A: Yes, Java 7+ supports Strings in switch.
🧩 Java Version Relevance
Java Version | Feature |
---|---|
Java 1.0 | Basic if-else and switch statements |
Java 7 | Added String support in switch |
Java 14 | Introduced switch expressions |
✅ Summary
- if: For simple conditions.
- if-else: For binary choices.
- else-if: For multiple ranges.
- switch: For comparing discrete values.
- nested if: For dependent checks.