Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java break Statement
Exit Loops Early



The break statement in Java is used to prematurely exit loops or switch-case blocks. It's a flow control mechanism used when you want to stop the current loop execution and jump out of it completely, regardless of the loop’s condition.

Why Use break?

Imagine you're searching for a specific value in a list. Once it's found, there’s no reason to continue looping through the rest of the items. This is where break shines — it enhances performance and avoids unnecessary operations.

Syntax of break

break;

The break statement is written on its own and ends with a semicolon. It can be placed inside for, while, or do-while loops, as well as switch blocks.

Using break in a for Loop

Let’s look at a simple example where we loop through numbers but stop as soon as we hit number 5.

public class BreakExample1 {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            if (i == 5) {
                break;
            }
            System.out.println("i = " + i);
        }
        System.out.println("Loop exited using break.");
    }
}
i = 1
i = 2
i = 3
i = 4
Loop exited using break.

Explanation

The loop prints numbers starting from 1. When i reaches 5, the condition i == 5 becomes true. The break statement executes, exiting the loop immediately. Any statements after the loop continue to execute as normal.

Using break in a while Loop

This time, we use a while loop and break out of it when a condition is met.

public class BreakExample2 {
    public static void main(String[] args) {
        int i = 1;
        while (i <= 10) {
            if (i == 6) {
                break;
            }
            System.out.println("i = " + i);
            i++;
        }
    }
}
i = 1
i = 2
i = 3
i = 4
i = 5

Using break in a switch Statement

In switch statements, break is used to terminate a case block and prevent fall-through.

public class BreakInSwitch {
    public static void main(String[] args) {
        int day = 3;
        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("Another day");
        }
    }
}
Wednesday

Break in Nested Loops

When break is used inside nested loops, it only exits the innermost loop by default.

public class BreakNested {
    public static void main(String[] args) {
        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);
            }
        }
    }
}
i = 1, j = 1
i = 2, j = 1
i = 3, j = 1

Breaking Out of Multiple Loops (Labeled Break)

To break out of multiple nested loops at once, you can use a labeled break. Here's how:

public class LabeledBreak {
    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);
            }
        }
    }
}
i = 1, j = 1
i = 1, j = 2
i = 1, j = 3
i = 2, j = 1

When Should You Use break?

Common Mistakes to Avoid

Summary

The break statement in Java is your exit gate from loops and switch blocks. It gives you the flexibility to stop a loop when it’s no longer needed, saving processing time and simplifying logic. However, use it wisely to keep your code clean and maintainable.

QUIZ

Question 1:What happens when a break; statement is encountered in a loop?

Question 2:The break statement can be used in both loops and switch-case constructs in Java.

Question 3:Consider the following code snippet:
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;
    }
    System.out.print(i + " ");
}
What is the output?

Question 4:Which of the following statements about break in Java are correct?

Question 5:Using break inside a switch statement prevents execution from falling through to the next case.

Question 6:Given the code below:
outer:
for (int i = 1; i <= 2; i++) {
    for (int j = 1; j <= 3; j++) {
        if (j == 2) {
            break outer;
        }
        System.out.println("i = " + i + ", j = " + j);
    }
}
How many lines are printed?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M