Lesson 26 of 50 – Java Break Statement
52%

Java Break Statement

The break statement in Java is used to stop a loop immediately. When Java executes break, it exits the nearest enclosing loop and continues with the statement after that loop.

1. What Is the Break Statement?

Suppose you want to print numbers from 1 to 10, but you want the loop to stop when the number reaches 5.

for (int i = 1; i <= 10; i++) {
    if (i == 5) {
        break;
    }

    System.out.println(i);
}

Output:

1
2
3
4
The number 5 is not printed because the loop stops as soon as the condition i == 5 becomes true.
2. Syntax of the Break Statement
break;

Example:

int i = 1;

while (i <= 10) {
    if (i == 4) {
        break;
    }

    System.out.println(i);
    i++;
}

Output:

1
2
3

The break statement exits the loop immediately when i becomes 4.

3. Break Statement in a For Loop

Print numbers from 1 to 10, but stop at 6.

public class Main {
    public static void main(String[] args) {

        for (int i = 1; i <= 10; i++) {

            if (i == 6) {
                break;
            }

            System.out.println(i);
        }
    }
}

Output:

1
2
3
4
5

When i becomes 6, the loop terminates. The remaining iterations are skipped.

4. Break Statement in a While Loop
int i = 1;

while (i <= 10) {

    if (i == 5) {
        break;
    }

    System.out.println(i);
    i++;
}

Output:

1
2
3
4

The while loop stops when the value of i becomes 5.

5. Break Statement in a Do-While Loop
int i = 1;

do {
    if (i == 4) {
        break;
    }

    System.out.println(i);
    i++;
} while (i <= 10);

Output:

1
2
3

The do-while loop stops when i == 4. The condition at the end is not checked after the break exits the loop.

6. Find a Number in an Array

The break statement is useful when searching for an item. Once the required number is found, the loop can stop.

public class Main {
    public static void main(String[] args) {

        int[] numbers = {10, 20, 30, 40, 50};
        int target = 30;

        for (int i = 0; i < numbers.length; i++) {

            if (numbers[i] == target) {
                System.out.println("Number found!");
                break;
            }
        }
    }
}

Output:

Number found!

The loop stops searching after finding the first matching number.

7. Stop When a Specific Character Is Found

Search for the letter 'a' in a word.

public class Main {
    public static void main(String[] args) {

        String word = "computer";
        boolean found = false;

        for (int i = 0; i < word.length(); i++) {

            if (word.charAt(i) == 't') {
                found = true;
                break;
            }
        }

        if (found) {
            System.out.println("Character found!");
        } else {
            System.out.println("Character not found!");
        }
    }
}

Output:

Character found!

The loop stops when the character 't' is found. The boolean variable records the search result.

8. Break in a Nested Loop

A nested loop is a loop inside another loop. An ordinary break statement exits only the nearest enclosing loop.

for (int i = 1; i <= 3; i++) {

    for (int j = 1; j <= 3; j++) {

        if (j == 2) {
            break;
        }

        System.out.println("i = " + i + ", j = " + j);
    }
}

Output:

i = 1, j = 1
i = 2, j = 1
i = 3, j = 1

Each inner loop stops when j becomes 2, but the outer loop continues.

9. Break Statement in a Switch Statement

In a traditional Java switch statement, break is commonly used to stop execution from falling through into the next case.

int day = 2;

switch (day) {
    case 1:
        System.out.println("Monday");
        break;

    case 2:
        System.out.println("Tuesday");
        break;

    case 3:
        System.out.println("Wednesday");
        break;

    default:
        System.out.println("Invalid day");
}

Output:

Tuesday

Here, break exits the switch statement rather than a loop.

10. Break with User Input

This program keeps asking the user to enter a number until the user enters zero.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        while (true) {
            System.out.print("Enter a number (0 to stop): ");
            int number = sc.nextInt();

            if (number == 0) {
                break;
            }

            System.out.println("You entered: " + number);
        }

        System.out.println("Program stopped.");
        sc.close();
    }
}

Example interaction:

Enter a number (0 to stop): 5
You entered: 5
Enter a number (0 to stop): 8
You entered: 8
Enter a number (0 to stop): 0
Program stopped.

The loop continues until the user enters zero. Then break terminates the loop.

11. Labeled Break Statement

Java supports a labeled break that can exit an enclosing labeled loop, including from inside a nested loop.

public class Main {
    public static void main(String[] args) {

        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 = " + i + ", j = " + j);
            }
        }

        System.out.println("Loops stopped.");
    }
}

Output:

i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1
Loops stopped.

The statement break outer; exits both loops by transferring control outside the labeled outer loop.

12. Difference Between Break and Continue
Break Continue
Terminates the nearest enclosing loop. Skips the current iteration and proceeds to the next one.
Execution continues after the loop. The loop continues if its condition allows.
Example: stop printing at 5. Example: skip printing 5 but continue with later numbers.

Break example:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;
    }
    System.out.println(i);
}

Output:

1
2

Continue example:

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;
    }
    System.out.println(i);
}

Output:

1
2
4
5
13. Practice Assignments

Write Java programs to complete these assignments.

  1. Print numbers from 1 to 20, but stop at 10.
  2. Print numbers from 1 to 100 and stop at 25.
  3. Search for a number in an integer array using break.
  4. Ask the user for numbers until zero is entered.
  5. Print numbers from 1 to 50, stopping when a number is divisible by 7.
  6. Use break in a switch statement for a simple menu.
  7. Write a nested loop that stops the inner loop when j equals 3.
  8. Use a labeled break to exit two nested loops.
  9. Print characters in a string until the letter 'a' is found.
  10. Create a menu-driven program that exits when the user selects Exit.
Tip: Use break when you need to stop a loop immediately after a particular condition is met.

🧠 Quick Quiz

1. What does the break statement do in a loop?




2. What is the output of this code?

for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;
    }
    System.out.println(i);
}



3. What does a labeled break allow you to do?