Java Control Flow Statements: Decisions, Loops and Branching

Java control flow statements explained with if-else, switch, for, while, do-while, break, continue and yield, with examples and links to each tutorial.

Java control flow statements grouped into decisions, loops and branching

Control flow statements decide which statements a Java program runs and how often, by choosing between branches, repeating a block in a loop, or jumping out of a block early. Without them, a method runs its statements once, from top to bottom.

Every business rule in code uses them, from checking a discount code with if, to processing every order in a list with a loop, to stopping a search with break once the result is found. Java groups them into decision statements, loops and branching statements, with exceptions as a fourth way to leave a block.

The following example uses one statement of each group to total the paid orders until a limit is reached.

int[] orders = {120, 0, 75, 300, 40};
int total = 0;
for (int amount : orders) {             // loop
    if (amount == 0) {                  // decision
        continue;                       // branch, skip unpaid orders
    }
    if (total + amount > 400) {
        break;                          // branch, stop at the limit
    }
    total += amount;
}
int paid = total;                       // 195

Notice that continue skips one iteration and break ends the whole loop. The sections describe each group, compare the loops, show the newer switch forms, and link the detailed tutorial for every statement.

1. The Three Groups of Control Flow Statements

The JLS describes each statement separately, but they fall into three groups by what they do to the flow. Picking the right group first narrows the choice to one or two statements.

Java control flow statements grouped into decisions, loops and branching
Decisions pick a path, loops repeat a block, and branching statements leave a block early
GroupStatementsTutorial
Decisionif, if-else, else if, switchif-else, switch statement
Loopfor, enhanced for, while, do-whilefor, for-each, while, do-while
Branchingbreak, continue, return, yield, labelsbreak, continue, labeled statements, yield
Exceptionsthrow, try, catch, finallytry-catch-finally

2. Decision Statements

A decision statement evaluates a condition or a value once and runs one of several blocks. The if statement tests boolean conditions, so it handles ranges and combined checks, whereas switch compares one value against many constants or patterns.

int age = 17;
String ticket;
if (age < 12) {
    ticket = "child";
} else if (age < 18) {
    ticket = "teen";
} else {
    ticket = "adult";
}
String chosen = ticket;                 // "teen"

When the choice depends on one value, a switch reads better than a long else if chain. Since Java 14, a switch expression also returns a value, and since Java 21 it can match types and records with patterns.

DayOfWeek day = DayOfWeek.SATURDAY;
String kind = switch (day) {
    case SATURDAY, SUNDAY -> "weekend";
    default -> "weekday";
};
String result = kind;                   // "weekend"

3. Loops

A loop repeats a block while a condition holds, and the four loop forms differ only in where the condition is checked and who manages the counter. Every loop can be rewritten as a while loop, so the choice is about readability.

LoopChecks the conditionBest for
forBefore each iterationA known number of steps, index access
Enhanced forHidden, ends after the last elementEvery element of an array or Iterable
whileBefore each iterationAn unknown number of steps, such as reading input
do-whileAfter each iterationBlocks that must run at least once, such as a menu prompt
int attempts = 0;
int seed = 3;
do {
    seed = (seed * 3) % 10;
    attempts++;
} while (seed != 1);
int tries = attempts;                   // 3

Streams such as list.stream().filter(…).forEach(…) replace many loops in modern code. They cannot use break or continue, so a loop is often clearer when the iteration must stop at a condition that methods such as findFirst() or takeWhile() do not cover.

4. Branching Statements

Branching statements transfer control without evaluating a condition themselves. They mostly sit inside an if that decides when to jump.

  • The break statement ends the innermost loop or switch statement.
  • The continue statement skips the rest of the current loop iteration.
  • The return statement leaves the method, optionally with a value.
  • The yield statement returns a value from a block inside a switch expression.
  • A label such as outer: lets break and continue target an outer loop.
int[][] grid = {{3, 8}, {5, 42}, {9, 1}};
int row = -1;
outer:
for (int r = 0; r < grid.length; r++) {
    for (int value : grid[r]) {
        if (value == 42) {
            row = r;
            break outer;                // leaves both loops
        }
    }
}
int foundRow = row;                     // 1

Java has no goto. The word is reserved, and labeled break and continue cover the legitimate uses, such as leaving nested loops.

5. Java Control Flow FAQs

Developers learning Java often ask these questions about control flow.

5.1. How many control flow statements are there in Java?

Java has three decision statements (if, if-else and switch), four loop forms (for, enhanced for, while and do-while) and four branching statements (break, continue, return and yield), plus throw and try for exceptions. Counts differ between tutorials depending on whether else if and the exception statements are counted separately.

5.2. When should we use switch instead of if-else?

When one variable is compared against several constants, enum values or types. For ranges such as age < 18 or conditions that combine several variables, if-else fits better, because a constant case label matches one exact value.

5.3. Is exception handling a control flow statement?

Yes, in the JLS sense, because throw and try are statements that change which code runs next. We still use exceptions only for errors, not for normal branching, because creating and catching an exception costs far more than an if check.

6. Conclusion

Control flow statements fall into decisions, loops and branching statements, with exceptions as a separate way to leave a block. The if statement handles conditions, switch handles one value with many cases, and the four loop forms differ only in when they check their condition.

The statements break, continue, return and yield jump out of a block early, and labels extend break and continue to outer loops. Each linked tutorial covers one statement in depth with Java 25 examples.

7. References

Happy Learning !!

Source Code on Github

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.