Control Flow in Java

    Atul Kabra3 min readUpdated

    Control flow decides which statements run and how often. Java gives you decisions (if/else, switch) and loops (for, while, do-while). Decisions branch based on a condition; loops repeat a block until a condition stops them. Modern Java also offers switch expressions that return a value cleanly. Master these and you can express almost any logic.

    The if / else if / else chain

    int marks = 72;
    if (marks >= 75) {
        System.out.println("Distinction");
    } else if (marks >= 40) {     // checked only if the first was false
        System.out.println("Pass");
    } else {
        System.out.println("Needs improvement");
    }
    

    Conditions must be boolean. Java evaluates each branch top to bottom and runs the first whose condition is true, then skips the rest.

    switch expressions (modern Java)

    Old C-style switch needed break after every case and was error-prone. Modern Java uses the arrow form, which returns a value and never falls through:

    int day = 3;
    String name = switch (day) {
        case 1 -> "Monday";
        case 2 -> "Tuesday";
        case 3 -> "Wednesday";
        case 6, 7 -> "Weekend";   // multiple labels share one arm
        default -> "Other";        // required when not all values are covered
    };
    System.out.println(name); // Wednesday
    

    Each arm is independent — no break needed. If an arm needs several statements, wrap them in braces and use yield to produce the value.

    The for loop

    Best when you know how many times to repeat:

    // Print 1 to 5.
    for (int i = 1; i <= 5; i++) { // init; condition; update
        System.out.println(i);
    }
    

    The three parts run as: initialize once, test the condition before each pass, run the update after each pass.

    The enhanced for (for-each) loop

    To iterate over an array or collection without an index:

    int[] marks = {88, 92, 75};
    for (int m : marks) {   // read "for each m in marks"
        System.out.println(m);
    }
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    while and do-while

    while tests before the body; do-while tests after, so it always runs at least once.

    int n = 5;
    while (n > 0) {     // checks first
        System.out.println(n);
        n--;
    }
    
    int count = 0;
    do {
        System.out.println("Runs at least once");
        count++;
    } while (count < 3); // checks after the body
    

    break and continue

    break exits the nearest loop immediately; continue skips to the next iteration.

    for (int i = 1; i <= 10; i++) {
        if (i == 4) continue; // skip 4
        if (i == 7) break;    // stop entirely at 7
        System.out.println(i); // prints 1 2 3 5 6
    }
    

    Putting it together

    // Print whether each number 1..6 is even or odd.
    for (int i = 1; i <= 6; i++) {
        String kind = (i % 2 == 0) ? "even" : "odd"; // ternary decision
        System.out.println(i + " is " + kind);
    }
    

    Common mistakes

    • Off-by-one loop bounds. i <= n includes n; i < n stops one short. Decide which you want.
    • Infinite loops. Forgetting to update the loop variable (or a while condition that never becomes false) hangs the program. Always change something the condition checks.
    • Assignment instead of comparison. if (x = 5) is a bug; you meant ==. Java rejects it unless x is boolean.
    • Fall-through in old-style switch. If you still use case x: with a colon, a missing break runs the next case too. Prefer the arrow form.
    • Declaring a variable inside a loop you need afterward. Variables declared inside { } vanish at the closing brace.

    FAQ

    When do I pick while over for? Use for when the count is known up front; use while when you loop until some event, such as reading input until end-of-file.

    Can switch work on Strings? Yes — switch supports int, String, enum, and more.

    Keep going

    Next, store many values with Arrays in Java, or review Operators in Java. All tutorials are on the Java hub; for guided practice in Jalgaon, see the Java course.


    Learn to write clear, correct logic at Infoplanet, Jalgaon. Join the waitlist for the Java course.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides