Learn how to break in Java (in very simple language)
In this tutorial you will learn to use break to end a loop.
Suppose you are working with circles. Sometimes you need to skip some commands inside the loop or end the loop immediately without checking the condition.
In such cases, break and continue commands are used. The break statement terminates the loop immediately and program control goes after the loop. This is often used with decision statements (if… else).
The break structure is:
break;
How does the break command work?
Example 1: break in Java
- class Test {
- public static void main (String [] args) {
- for (int i = 1; i <= 10; ++ i) {
- if (i == 5) {
- break;
- }
- System.out.println (i);
- }
- }
- }
Output
1
۲
3
4
In the above program, when the value of i equals 5, we put the expression i == 5 in parentheses if. Then, the break command is executed and terminates the for loop.
Example 2: The phrase break in Java
The following program calculates the sum of the numbers entered by the user until he enters a negative number.
The Scanner object is used to get input from the user.
- import java.util.Scanner;
- class UserInputSum {
- public static void main (String [] args) {
- Double number, sum = 0.0;
- Scanner input = new Scanner (System.in);
- while (true) {
- System.out.print (“Enter a number:”);
- number = input.nextDouble ();
- 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 while loop condition is always true and runs until the user enters a negative number. If the user enters a negative number, the command is executed inside the if body, which terminates the while loop.
If there are nested loops, the break terminates the innermost loop.
Here, the term break terminates the inner while loop and the program control jumps to the outer loop.
Labeled break command
The break statement we have discussed so far is the unmarked form break, which terminates the innermost loops for, while, do… while, and switch. There is another form of break command that has a break tag that can be used to terminate the outer loop.
How does the labeled break command work?
Here, the label is an identifier. When you run break, the labeled command ends, and the program control jumps to after the labeled command.
Another example:
- while (testExpression) {
- // codes
- second:
- while (testExpression) {
- // codes
- while (testExpression) {
- // codes
- break second;
- }
- }
- // control jumps here
- }
When running break, the program control jumps to the phrase labeled second.
Example 3: Labeled break in Java structure
- class LabeledBreak {
- public static void main (String [] args) {
- first:
- for (int i = 1; i <5; i ++) {
- second:
- for (int j = 1; j <3; j ++) {
- System.out.println (“i =” + i + “; j =” + j);
- if (i == 2)
- break first;
- }
- }
- }
- }
Output
i = 1; j = 1
i = 1; j = 2
i = 2; j = 1
Here is another method of the above program. break In the following program, terminates the second tagged phrase and the program control jumps to the second tagged phrase.
- class LabeledBreak {
- public static void main (String [] args) {
- first:
- for (int i = 1; i <5; i ++) {
- second:
- for (int j = 1; j <3; j ++) {
- System.out.println (“i =” + i + “; j =” + j);
- 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
The break command is also used to terminate the switch.