Conditional Statements
Conditional Statements
A school program needs reliable data and clear rules. A condition chooses which instructions run. A school report can distinguish passing from needing practice, while instructions after the decision still run.
Understand
A condition chooses which instructions run.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
An if condition must be boolean. A chain stops at its first matching branch, so the order of thresholds matters.
Conditional Statements in Java
Types of Conditional Statements in Java
Java provides several ways to handle conditions:
ifstatementif-elsestatementelse-ifladderswitchstatement
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
breakstatement to prevent fall-through unless you want cumulative behavior. - Include a
defaultcase to handle unexpected inputs.
Common Mistakes to Avoid
Java · example discussed here
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");
}
}
}
- Forgetting
breakinswitch, 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 |
Important boundaries and mistakes
An if condition must be boolean. A chain stops at its first matching branch, so the order of thresholds matters.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: An if condition must be boolean.
Java 17 · Main.java · complete practice program
public class Main {
public static void main(String[] args) {
int marks = 72;
if (marks >= 50) {
System.out.println("Pass");
}
else {
System.out.println("Practise");
}
}
}
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
public class Main {
public static void main(String[] args) {
int marks = 72;
if (marks >= 50) {
System.out.println("Pass");
}
else {
System.out.println("Practise");
}
}
}
Expected output
PassA condition chooses which instructions run. A school report can distinguish passing from needing practice, while instructions after the decision still run.
An if condition must be boolean. A chain stops at its first matching branch, so the order of thresholds matters.
Run with javac Main.java, then java Main. The declaration of Main names the class; main starts the example, and println writes a complete output line.
Check your understanding
What to remember
A condition chooses which instructions run. A school report can distinguish passing from needing practice, while instructions after the decision still run. An if condition must be boolean. A chain stops at its first matching branch, so the order of thresholds matters.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.