Java Conditional Statements
if, else, else-if, switch
Conditional statements allow your program to choose different paths of execution depending on whether a condition is true or false. Think of them as the 'brain' of your logic – making choices on your behalf.
Types of Conditional Statements in Java
Java provides several ways to handle conditions:
if
statementif-else
statementelse-if
ladderswitch
statement
1. Java if
Statement
The if
statement checks a condition and executes the block of code inside it only if the condition is true.
public class IfExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
The number is positive.
Explanation
The program checks if number > 0
. Since 10 is greater than 0, it prints the message. If the number were negative, nothing would be printed.
public class IfExample {
public static void main(String[] args) {
int number = -5;
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
Explanation
The program checks if number > 0
. Since -5 is not greater than 0, if block does not execute and nothing has been printed.
2. Java if-else
Statement
Use this when you have two scenarios — one when the condition is true and another when it's false.
public class IfElseExample {
public static void main(String[] args) {
int number = -5;
if (number >= 0) {
System.out.println("Non-negative number");
} else {
System.out.println("Negative number");
}
}
}
Negative number
Since the given condition is false, else block executes.
This pattern is useful when your program must respond differently depending on a single true/false condition.
3. Java else-if
Ladder
When you have multiple conditions to evaluate in a sequence, else-if
ladders help maintain readability and logic flow.
public class ElseIfExample {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Grade A");
} else if (score >= 75) {
System.out.println("Grade B");
} else if (score >= 60) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}
}
}
Grade B
Verification Tip
Start with the highest condition and work your way down to the least. This avoids overlapping ranges or incorrect classifications.
4. Java switch
Statement
Use a switch
when you have a single variable that can take multiple constant values (like enums, numbers, or strings in Java 7+).
public class SwitchExample {
public static void main(String[] args) {
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("Another day");
}
}
}
Wednesday
Important Checks
- Always include a
break
statement to prevent fall-through unless you want cumulative behavior. - Include a
default
case to handle unexpected inputs.
Common Mistakes to Avoid
- Forgetting
break
inswitch
, which may lead to incorrect results. - Using assignment
=
instead of comparison==
in conditions. - Improper nesting of
if-else
, leading to confusion and logic errors.
When to Use What?
Use Case | Statement Type |
---|---|
Single true/false condition | if |
Binary decision path | if-else |
Multiple ranges or tiers | else-if ladder |
Discrete values for one variable | switch |
Summary
Conditional statements bring your Java programs to life by adding the power of choice. They're the difference between rigid, robotic scripts and adaptive, intelligent logic. Start small with if
, branch into else
, scale with else-if
, and simplify with switch
.
QUIZ
Question 1:What will be the output of the following Java program?
int number = -3;
if (number > 0) {
System.out.println("Positive");
}
int number = -3;
if (number > 0) {
System.out.println("Positive");
}