Loop Control

Breaking Out of Loops

The break statement immediately exits the loop, moving execution to the first line after the loop block.

Using Break Statement:

// Counting from 1 to 5 with break
int number = 1;

while (true) {
    System.out.println(number);
    
    if (number >= 5) {
        break;  // Exit when we reach 5
    }
    
    number = number + 1;
}

System.out.println("Ready!");
// Output: 1, 2, 3, 4, 5, Ready!

Continue Statement

The continue statement skips the rest of the current loop iteration and jumps back to the beginning of the loop to check the condition again.

Using Continue Statement:

Scanner scanner = new Scanner(System.in);

while (true) {
    System.out.println("Insert positive integers");
    int number = Integer.valueOf(scanner.nextLine());
    
    if (number <= 0) {
        System.out.println("Unfit number! Try again.");
        continue;  // Skip to beginning of loop
    }
    
    if (number == 999) {
        break;  // Exit condition
    }
    
    System.out.println("Your input was " + number);
}

// Robot example: Process valid sensor readings only
while (true) {
    System.out.println("Enter sensor reading (0-100, -1 to exit)");
    int reading = Integer.valueOf(scanner.nextLine());
    
    if (reading == -1) {
        break;
    }
    
    if (reading < 0 || reading > 100) {
        System.out.println("Invalid reading! Must be 0-100");
        continue;  // Skip invalid readings
    }
    
    System.out.println("Processing sensor reading: " + reading);
}

Loop Execution Timing

Important: Loop conditions are only checked at the beginning of each iteration and when the loop first starts. Changes to variables inside the loop don't immediately stop the loop.

Loop Timing Example:

int number = 1;

while (number != 2) {
    System.out.println(number);  // prints 1
    number = 2;                  // Now number equals 2
    System.out.println(number);  // prints 2
    number = 1;                  // Back to 1 - loop continues!
}
// This loop runs forever!

// The condition (number != 2) is only checked:
// 1. When loop starts
// 2. After reaching the closing }

// For loop example:
for (int i = 0; i != 100; i++) {
    System.out.println(i);       // prints current i
    i = 100;                     // Set i to 100
    System.out.println(i);       // prints 100
    i = 0;                       // Reset i to 0
}  // At this point, i++ makes i = 1, loop continues
// This loop also runs forever!

Loop Control Best Practices:

Important Guidelines:

  • Always update loop variables: Ensure conditions can eventually become false
  • Use break wisely: Provide clear exit conditions for infinite loops
  • Continue for validation: Skip invalid input without exiting the loop
  • Single clear task: Each if-statement should have one specific purpose

Try It Yourself:

Exercise 1: Loop Control Practice

  • Create a number validator that uses continue for invalid input
  • Build a menu system that uses break to exit
  • Make a sensor monitoring system with both break and continue
  • Create a robot safety system with emergency break conditions
Practice using break and continue statements:

Open full interactive app