Core Java Fundamentals ยท Loops

Break Statement

Learn how to prematurely exit loops and switch statements using the break keyword.

BreakDemo.java

public class BreakDemo {
    public static void main(String[] args) {
        int[] numbers = { 10, 25, 34, 48, 56, 77, 82 };
        int numberToFind = 34;
        boolean found = false;

        System.out.println("Searching for " + numberToFind + "...");

        for (int i = 0; i < numbers.length; i++) {
            System.out.println("Checking index " + i + ": value " + numbers[i]);
            if (numbers[i] == numberToFind) {
                found = true;
                System.out.println("Number found!");
                break;
            }
        }

        System.out.println("Loop finished.");
        if (found) {
            System.out.println("The number " + numberToFind + " was in the array.");
        } else {
            System.out.println("The number " + numberToFind + " was not in the array.");
        }
    }
}

Step by step

  1. The program starts. We define an integer array numbers with some values.
  2. An integer variable numberToFind is declared and initialized to 34. This is the value we will search for.
  3. A boolean flag found is initialized to false. We will use this to track if the number was found after the loop finishes.
  4. A message is printed to the console indicating the start of the search.
  5. The for loop begins. The loop control variable i is initialized to 0.
  6. The loop condition i < numbers.length (0 < 7) is checked. It is true, so the loop body executes.
  7. We print the current index and value being checked.
  8. The if condition checks if numbers[0] == numberToFind (10 == 34). The condition is false.
  9. The loop proceeds to the update statement. i is incremented to 1.
  10. The loop condition i < numbers.length (1 < 7) is checked. It is true, so the loop body executes again.
  11. We print the current index and value being checked.
  12. The if condition checks if numbers[1] == numberToFind (25 == 34). The condition is false.
  13. The loop proceeds to the update statement. i is incremented to 2.
  14. The loop condition i < numbers.length (2 < 7) is checked. It is true, so the loop body executes.
  15. We print the current index and value being checked.
  16. The if condition checks if numbers[2] == numberToFind (34 == 34). The condition is TRUE!
  17. The if block is executed. The found flag is set to true.
  18. A success message is printed to the console.
  19. The break statement is executed. This immediately terminates the for loop. The update statement (i++) and the condition check will NOT be executed again. Control transfers to the first statement after the loop.
  20. Execution continues after the loop. A message is printed to indicate the loop is over.
  21. This if statement checks the found flag. Since found is true, the if block will be executed.
  22. The final message is printed, confirming that the number was found in the array.