› Module 4: Control Flow › 4.3 break, continue, and Labeled Statements
M O D U L E

Module 4: Control Flow

Branching constructs, modern switch expressions, classic vs enhanced for-each loops, and labeled break/continue.

4.1 Decision Making: if, if-else, switch

Control flow statements determine the execution path of a program based on runtime conditions. Java provides standard conditional branching via if, else if, and else, evaluating boolean expressions from top to bottom.

Evolution of the Switch Statement in Java

Java's switch extends the traditional C-style switch in two major architectural ways:

  1. String Support: Since Java 7, String objects can be used as switch selectors and case labels (evaluated using .hashCode() and .equals() under the hood). C only allows primitive integral types.
  2. Enhanced Arrow Switch Expression (Java 14+): The traditional colon syntax suffered from accidental fall-through bugs when a developer forgot a break statement. Java introduced arrow syntax (case ->), which eliminates fall-through entirely and allows the switch to act as an expression that evaluates directly to a value:
int marks = 85;
String grade = switch (marks / 10) {
    case 10, 9 -> "A";
    case 8     -> "B";
    case 7     -> "C";
    default    -> "D";
};
System.out.println("Grade: " + grade);
Start condition true? true Execute if-block false Execute else-block Continue / End
Figure 4.1 — Decision flow for if / else-if / else: exactly one branch executes, then control continues.

💡 Placement & Interview Deep-Dive

  • Which data types are NOT permitted in a Java switch statement? float, double, and boolean cannot be used in a switch statement! Allowed types are byte, short, char, int, their corresponding wrapper classes (Byte, Short, Character, Integer), String, and enum constants.
  • What is the yield keyword in Java switch expressions? In multi-line code blocks within a Java 14+ switch expression, yield value; is used to explicitly return a value from that case branch, replacing the traditional break statement.

4.2 Loops: for, while, do-while, for-each

Loops allow repeated execution of a code block until a boolean termination condition evaluates to false. Java provides four loop structures:

  • Traditional for loop: Ideal when the number of iterations is known beforehand. Comprises initialization, test condition, and increment/update.
  • while loop: Entry-controlled loop. Evaluates condition before each iteration; executes 0 or more times.
  • do-while loop: Exit-controlled loop. Evaluates condition after the loop body executes, guaranteeing at least one execution regardless of condition.
  • Enhanced for-each loop: Iterates cleanly through arrays and Iterable collections without explicit loop counter indices:
    int[] scores = {90, 85, 78, 92};
    for (int s : scores) {
        System.out.println("Score: " + s);
    }
init; e.g. i = 0 i < n ? false Exit loop true Loop body (executes statements) update; i++
Figure 4.2 — Execution flow of a for-loop: init → test → body → update → repeat.

💡 Placement & Interview Deep-Dive

  • Can you modify an ArrayList while iterating over it in a for-each loop? NO! Calling list.add() or list.remove() inside an enhanced for-each loop throws a ConcurrentModificationException. The enhanced for loop uses an internal Iterator that checks modCount. To safely remove items during iteration, you must use an explicit Iterator and call iterator.remove().
  • Infinite loop idioms in Java: Both for(;;) and while(true) produce identical bytecode instructions (an unconditional jump goto).

4.3 break, continue, and Labeled Statements

Loop execution can be dynamically controlled using break and continue statements:

  • break: Immediately exits the nearest enclosing loop or switch block, resuming execution at the statement following the loop.
  • continue: Immediately skips the remainder of the current loop iteration and advances directly to the next iteration's test/update step.

The Structured Solution to Goto: Labeled Statements

Java has no goto statement. However, in deeply nested loops (e.g. searching a 2D matrix), breaking out of only the innermost loop requires tedious boolean flag management. Java provides labeled break and continue:

outer: // label definition
for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
        if (i == 1 && j == 1) {
            System.out.println("Breaking outer loop from inside!");
            break outer; // Breaks completely out of the outer loop
        }
        System.out.println("i=" + i + ", j=" + j);
    }
}
KEY TERM: Labeled Statements

A structured, readable alternative to goto — they let you control exactly which outer loop a break or continue applies to, instead of always affecting only the innermost one.

MODULE COMPREHENSIVE QUIZ

Module 4 Final Quiz

Test your understanding across all 3 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 4 Final Quiz →
Yukti Mascot