Summary: in this tutorial, you will learn how to use Java break statement to terminate a loop prematurely.
Introduction to the Java break statement
The break statement allows you to terminate a loop prematurely. You can use the break statement inside a while, do while, and for loop:
break;Code language: JavaScript (javascript)When Java encounters the break statement within a loop, it terminates the loop immediately. In practice, you use the break statement with an if statement to exit the loop based on a condition like this:
if(condition) {
break;
}Code language: JavaScript (javascript)Using break in a for loop
Here’s the syntax for using the break statement in a for loop:
for(initialization; condition, update) {
// code to be executed
// ...
if(exitCondition) {
break;
}
// code to be executed
// ...
}Code language: Java (java)The following example illustrates how to use the break statement inside a for loop:
public class App {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
System.out.println(i);
}
}
}Code language: Java (java)Output:
0
1
2
3
4Code language: plaintext (plaintext)In this example, we use a for loop to display the integers from 0 to 9. In each iteration, we increase the variable i by one.
When the value of the variable i becomes 5, the condition of the if statement evaluates to true true, and the break statement terminates the loop.
So instead of displaying 10 numbers from 0 to 9, the program shows 5 numbers from 0 to 4. The loop was stopped prematurely.
When you use the break statement inside a nested loop, it terminates the innermost loop only. For example:
public class App {
public static void main(String[] args) {
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= 5; j++) {
// exit the inner loop only
if (j > i) {
break;
}
// print numbers each row
System.out.print(j + " ");
}
System.out.println();
}
}
}Code language: Java (java)Output:
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5Code language: plaintext (plaintext)Using break in a while loop
The while loop statement executes a block of code repeatedly as long as a condition is true.
By using the break statement with an if statement inside a while loop, you can create another condition that terminates the loop:
while (condition)
{
if (exitCondition)
{
break;
}
}Code language: Java (java)The following example illustrates how to use the break statement inside a while loop to terminate the loop when the count is 5:
public class App {
public static void main(String[] args) {
int count = 0;
while (count <= 10) {
// Exit the loop when count equals 5
if (count == 5) {
break;
}
System.out.println(count);
count++;
}
}
}Code language: Java (java)Output:
0
1
2
3
4In this program, the loop will terminate when the count equals 5 and will print numbers from 0 to 4.
Using break in do while loop
Similar to the while loop, you can use the break statement in a do while loop to exit the loop prematurely:
do
{
if (exitCondition)
{
break;
}
} while (condition);Code language: Java (java)For example, the following program illustrates how to use the break statement in a do while loop:
public class App {
public static void main(String[] args) {
int count = 0;
do {
if (count == 5) {
break;
}
System.out.println(count);
count++;
} while (count < 10);
}
}Code language: Java (java)In this program, the loop will terminate when the count equals 5 and will print numbers from 0 to 4.
Summary
- Use the Java
breakstatement to terminate a loop prematurely.