English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about the break statement, which is marked as the break statement in Java, through examples.
When using loops, sometimes you may want to skip certain statements within the loop or terminate the loop immediately without checking the test expression.
In this case, the break and continue statements will be used. You will learn about them in the next chapter.continue statement.
The break statement in Java immediately terminates the loop, and control of the program is passed to the next statement after the loop.
The break statement is almost always used with decision statements (Java if ... else statement)together.
This is the syntax of the break statement in Java:
break;
class Test { public static void main(String[] args) { // for loop for (int i = 1; i <= 10; ++i) { //If the value of i is5then the loop ends if (i == 5) { break; } System.out.println(i); } } }
Output:
1 2 3 4
In the above program, we use a for loop to print the value of i at each iteration. To understand how the for loop works, please visitJava for loop. Please note the following statement:
if (i == 5) { break; }
This means that when the value of i is equal to5when, the loop ends. Therefore, we get a value less than5output value.
The following program calculates the sum of the numbers entered by the user until the user enters a negative number.
To accept user input, we use this Scanner object. For more information on Scanner, please visitJava Scanner.
import java.util.Scanner; class UserInputSum { public static void main(String[] args) { Double number, sum = 0.0; //Create Scanner object Scanner input = new Scanner(System.in); while (true) { System.out.print("Enter a number: "); //Accept two inputs from the user number = input.nextDouble(); //If number is negative, the loop terminates if (number < 0.0) { break; } sum += number; } System.out.println("Sum = " + sum); } }
Output:
Enter a number: 3.2 Enter a number: 5 Enter a number: 2.3 Enter a number: 0 Enter a number: -4.5 Sum = 10.5
In the above program, the test expression of the while loop is always true. Here, please note this line,
if (number < 0.0) { break; }
This means that when the user enters a negative number, the while loop terminates.
ForNested loops,break statement terminates the innermost loop.
In this case, the break statement terminates the innermost while loop, and the control jumps to the outer loop.
So far, we have used the unlabeled break statement. It terminates the innermost loop and the switch statement. But, there is another form of break statement in Java called labeled break.
We can also use the labeled break statement to terminate the outermost loop.
As shown in the figure above, we use the label identifier to specify the outer loop. Now, please note how to use this break statement (break label;).
In this case, the break statement terminates the labeled statement (i.e., the outer loop). Then, the program jumps to the statement after the specified label.
This is another example:
while (testExpression) { // Code second: while (testExpression) { // Code while(testExpression) { // Code break second; } } //Control jumps here }
In the above example, when the statement break second; is executed; after execution, the while loop marked as 'Second' terminates. And, the control of the program is transferred to the statement after the second while loop.
class LabeledBreak { public static void main(String[] args) { //The for loop is marked as first first: for( int i = 1; i < 5; i++) { //The for loop is marked as second second: for(int j = 1; j < 3; j ++ ) { System.out.println("i = " + i + "; j = " +j); //The break statement terminates the first for loop if ( i == 2) break first; } } } }
Output:
i = 1; j = 1 i = 1; j = 2 i = 2; j = 1
In the above example, the labeled break statement is used to terminate the loop marked as 'first'. That is,
first: for(int i = 1; i < 5; i++) {...}
Here, if we change 'break first;' to 'break second;' the behavior of the program will be different. In this case, the for loop marked as second will be terminated. For example,}}
class LabeledBreak { public static void main(String[] args) { //The for loop is marked as first first: for( int i = 1; i < 5; i++) { //The for loop is marked as second second: for(int j = 1; j < 3; j ++ ) { System.out.println("i = " + i + "; j = " +j); //break statement terminates the loop marked as second if ( i == 2) break second; } } } }
Output:
i = 1; j = 1 i = 1; j = 2 i = 2; j = 1 i = 3; j = 1 i = 3; j = 2 i = 4; j = 1 i = 4; j = 2
NoteThe break statement is also used to terminate the case in the switch statement. For more information, please visitJava switch Statements.