Tutorials Logic, IN info@tutorialslogic.com

Control Flow in Java if, else, switch

if, else-if, and else

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.

Marks Decision

Marks Decision
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");
        }
    }
}
  • Order matters. Check the most specific or highest threshold first.

Nested Conditions and Guard Clauses

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.

Guard Clause

Guard Clause
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 Statement and switch Expression

switch is useful when one value is compared against many known cases. Modern Java also supports switch expressions that return a value.

switch Expression

switch Expression
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);
    }
}
  • The arrow syntax avoids accidental fall-through.
  • Use default for unexpected values.

Boolean Design for Readable Conditions

Complex conditions become clearer when you extract meaningful boolean variables. This makes the rule read like English and helps when debugging.

Readable Conditions

Readable Conditions
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");
    }
}

Choosing the right branch with if, else, and switch

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.

  • Use if/else for ranges and compound boolean logic.
  • Use switch for fixed choices based on one expression.
  • Place more specific conditions before broader ones.
  • Always consider the default or fallback path.

Grade decision with ordered conditions

Grade decision with ordered conditions
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");
}
Before you move on

Control Flow in Java if, else, switch Mastery Check

5 checks
  • Use if when a block should run only when a condition is true.
  • Use else-if for multiple exclusive checks.
  • 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.
  • switch is useful when one value is compared against many known cases.

Core Java Questions Learners Ask

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.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.