Readable control flow uses boolean conditions with one purpose, non-overlapping branches, and a default path that handles unexpected input deliberately.
Use if when a block should run only when a condition is true. Use else-if for multiple exclusive checks. Use else for the fallback path.
public class MarksDecision {
public static void main(String[] args) {
int marks = 82;
if (marks >= 90) {
System.out.println("Grade A+");
} else if (marks >= 75) {
System.out.println("Grade A");
} else if (marks >= 60) {
System.out.println("Grade B");
} else if (marks >= 35) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}
Deep nesting makes code harder to scan. A guard clause checks invalid or special cases early and returns, allowing the main path to stay flat.
public class GuardClauseDemo {
static void withdraw(double balance, double amount) {
if (amount <= 0) {
System.out.println("Amount must be positive");
return;
}
if (amount > balance) {
System.out.println("Insufficient balance");
return;
}
System.out.println("Withdrawal allowed");
}
}
switch is useful when one value is compared against many known cases. Modern Java also supports switch expressions that return a value.
public class SwitchExpressionDemo {
public static void main(String[] args) {
String day = "MONDAY";
String type = switch (day) {
case "SATURDAY", "SUNDAY" -> "Weekend";
case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY" -> "Weekday";
default -> "Unknown";
};
System.out.println(type);
}
}
Complex conditions become clearer when you extract meaningful boolean variables. This makes the rule read like English and helps when debugging.
public class EligibilityCheck {
public static void main(String[] args) {
int age = 24;
boolean hasLicense = true;
boolean isSuspended = false;
boolean oldEnough = age >= 18;
boolean canDrive = oldEnough && hasLicense && !isSuspended;
System.out.println(canDrive ? "Allowed" : "Not allowed");
}
}
Control flow decides which part of a Java program runs. if and else are best when decisions depend on boolean conditions, ranges, or combined checks. switch is useful when one value is compared against a fixed set of known cases, such as menu options, status codes, or command names.
The order of conditions matters. In a grading program, checking score >= 40 before score >= 90 would classify excellent marks too early. Good control flow reads like a decision table: the most specific or highest-priority cases appear first, the default case handles everything that remains, and each condition is easy to explain.
int score = 82;
if (score >= 90) {
System.out.println("Excellent");
} else if (score >= 75) {
System.out.println("Good");
} else if (score >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Use switch when one value is matched against several fixed cases.
Without break, execution continues into the next case unless the switch uses arrow labels.
A boolean assignment can compile inside a condition, so use == when comparison was intended.
Explore 500+ free tutorials across 20+ languages and frameworks.