Java Break Statement

The `break` statement in Java is a powerful tool for controlling the flow of your program. It is used to exit a loop or switch statement prematurely. It allows us to customize the behavior of code based on specific conditions.

When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop.

The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop.

For example, consider a situation if we are searching for a specific value in an array using a for loop. We can use the break statement to exit the loop as soon as we find the value, like this:

In this example, as soon as the value 3 is found in the array, the break statement is executed, and the loop is terminated. Without the break statement, the loop would continue iterating through the remaining elements of the array that is unnecessary once the value has been found.

Overall, the break statement is a versatile tool that allows us to customize the flow of Java programs, making them more efficient and easier to read. However, it is important to use break judiciously and understand its impact on the flow of code to avoid unintended consequences.

Use of break Statement

We can use Java break statement in all types of loops such as for loop, while loop and do-while loop.

Syntax:

Flowchart of break Statement

The flowchart of a break statement starts with the condition check of the loop. If the condition is true, the loop body is executed, and then the condition is checked again. If the condition becomes false, the loop is exited normally, and the program continues with the statement after the loop.

However, if a break statement is encountered inside the loop body, the control jumps to the statement immediately after the loop, bypassing the normal exit condition check. This behavior allows us to prematurely exit a loop based on certain conditions, even if the loop's condition is still true.

It is important to note that a break statement only affects the innermost loop that contains it. If we have nested loops and we use a break statement in the inner loop, only that inner loop will be exited, and the outer loops will continue executing normally unless they also have their own break statements.

java break statement flowchart

Java Break Statement with Loop

Example:

BreakExample.java

Output:

1
2
3
4

Java Break Statement with Inner Loop

It breaks inner loop only if you use break statement inside the inner loop.

Example:

BreakExample2.java

Output:

1 1
1 2
1 3
2 1
3 1
3 2
3 3

Java break Statement with Labelled for Loop

We can use break statement with a label. The feature is introduced since JDK 1.5. So, we can break any loop in Java now whether it is outer or inner loop.

Example:

BreakExample3.java

Output:

1 1
1 2
1 3
2 1

Java break Statement with while loop

Example:

BreakWhileExample.java

Output:

1
2
3
4

Java break Statement with do-while loop

Example:

BreakDoWhileExample.java

Output:

1
2
3
4

Java Break Statement with Switch

The break statement can be utilized alongside switch statement in Java for successfully exiting

Let's understand about the usage of break statement in Java along with switch keyword in the below mentioned example program.

Filename: JavaBreak1.java

Output:

Choose an option:
1. Option 1
2. Option 2
3. Option 3
4. Exit
Enter your choice: 1
You chose Option 1
Enter your choice: 2
You chose Option 2
Enter your choice: 3
You chose Option 3
Enter your choice: 4
Exiting...

To understand the example of break with switch statement, go with the link: Java Switch Statement.

Uses of Java break Statement

The break statement in Java is commonly used in various applications to control the flow of a program and manage loop iterations or switch cases. One common application is in searching algorithms, where break can be used to exit a loop once a desired condition is met. For example, in linear search, the loop can be terminated early if the target element is found, saving unnecessary iterations.

Another application is in error handling, where break can be used to exit a loop if an error condition is encountered. It can prevent the program from continuing to execute potentially faulty code and causing further issues. Additionally, break can be used in switch statements to exit the switch block once a specific case is matched, improving the efficiency of the code.

In user input validation, break can be used to exit a loop that prompts the user for input until a valid input is provided. Once the valid input is received, the loop can be exited using break, and the program can continue with the next steps. It helps in ensuring that the program does not proceed with invalid or unexpected input.

Let's understand about the break statement of Java with the help of an example program that demonstrates the usage of break statement in all types of scenarios in a program.

Filename: JavaBreak.java

Output:

Using break in a for loop:
Enter a number (or -1 to exit): 6
You entered: 6
Enter a number (or -1 to exit): 5
You entered: 5
Enter a number (or -1 to exit): 1
You entered: 1
Enter a number (or -1 to exit): 2
You entered: 2
Enter a number (or -1 to exit): 36
You entered: 36
Using break in a while loop:
Enter a number (or -1 to exit): 5
You entered: 5
Enter a number (or -1 to exit): 2
You entered: 2
Enter a number (or -1 to exit): 2
You entered: 2
Enter a number (or -1 to exit): 2
You entered: 2
Enter a number (or -1 to exit): 2
You entered: 2
Using break in a do-while loop:
Enter a number (or -1 to exit): 2
You entered: 2
Enter a number (or -1 to exit): 2
You entered: 2
Enter a number (or -1 to exit): 2





Latest Courses