← Back to Chapters

Java Loop Control Flow

? Java Loop Control Flow

? Quick Overview

Loop control flow statements in Java allow you to change the normal execution of loops. They help you skip iterations, exit loops early, or control nested loop behavior efficiently.

? Key Concepts

  • break – exits the loop immediately
  • continue – skips the current iteration
  • labeled break – exits an outer loop
  • labeled continue – continues an outer loop

? Syntax / Theory

Loop control statements are typically used inside for, while, or do-while loops to manage execution flow based on conditions.

? Code Example(s)

? View Code Example
// Using break and continue in a for loop
for(int i = 1; i <= 5; i++) {
    if(i == 3) {
        continue;
    }
    if(i == 5) {
        break;
    }
    System.out.println(i);
}
? View Code Example
// Using labeled break to exit outer loop
outer:
for(int i = 1; i <= 3; i++) {
    for(int j = 1; j <= 3; j++) {
        if(i == 2 && j == 2) {
            break outer;
        }
        System.out.println(i + "," + j);
    }
}

? Live Output / Explanation

The continue statement skips printing when i == 3. The break statement stops the loop completely when i == 5.

Labeled break exits the outer loop directly when the condition is met.

✅ Tips & Best Practices

  • Use break carefully to avoid unreadable logic
  • Prefer continue for skipping specific cases
  • Use labeled loops only when necessary

? Try It Yourself

  • Modify the loop to skip even numbers
  • Practice labeled continue in nested loops
  • Convert a loop with break into a condition-based loop