According to Java Tutorials, the Break Statement, break used in Java has two forms: labeled break and unlabeled break.
Unlabeled break
Normally, we knew unlabeled break used in switch-case statement, to break the following execution in the block.
Example:
int HOUR = 3;
switch(HOUR) {
case 1:
case 2:
case 3:
break;
case 4:
break;
default:
break;
}
Or used in for, while, do-while loop, for example:
for(int i=0; i<10; i++) {
System.out.print(i + " ");
if(i == 3) break;
}
(Which printed: [1 2 3 ])
Labeled break
There is another type of break called labeled break. Besides to use goto statement in Java (you know goto is reserved keyword in Java but it is not supported), we can use labeled break to break the following execution and transfer the flow of control back to the labeled statement immediately.
Example:
if(input > 4) break validation;
int input = 3;
validation: {
System.out.println("start");
if(input == 4) System.out.println("pass");
if(input < 4) break validation;
System.out.println("the input is not equal 4 and not smaller than 4");
System.out.println(“end”);
}
System.out.println("another end");
It should print:
start
another end
The program execution will break from the validation block (declared curly braces) and jump to the end followed by the block.
Another example, if you have a nested loop, and you want to break and returns to the outer loop, you can:
cat: {
for(int i=0; i<3; i++) {
for(int j=0; j<5; j++) {
System.out.println(i + " - " + j);
if(j == 3) break cat;
}
}
}
It should print:
0 – 0
0 – 1
0 – 2
0 – 3
In the example 4, the cat block has curly braces surrounds the inner nested-for loop code. But if, we make something like this:
cat:
for(int i=0; i<3; i++) {
for(int j=0; j<5; j++) {
System.out.println(i + " - " + j);
if(j == 3) break cat;
}
}
In the example 5, the statement followed by label cat is now the nested for-loop without curly brace. Now we said label cat is just for the nested for-loop, if you break or continue in the inner for-loop, it will return the outer for-loop.
The result will be the same as Example 4, but if we change the break into continue, (continue will be valid in this example, but invalid in previous example), it turned into magic, see:
cat:
for(int i=0; i<3; i++) {
for(int j=0; j<5; j++) {
System.out.println(i + " - " + j);
if(j == 3) continue cat;
}
}
The result will be:
0 – 0
0 – 1
0 – 2
0 – 3
1 – 0
1 – 1
1 – 2
1 – 3
2 – 0
2 – 1
2 – 2
2 – 3
Argument
But, this kind of feature is rarely found in most program code. Programmer who worked 16 years experience told us this.
Labeled break provides similar or equivalent feature just like goto do. The goto is considered evil to the program because it makes the code unreadable. That’s why Java is not supported it. But I think there is another reason. But you may see labeled break do just goto do, then why Java supports labeled break but not supports goto. So, there is a reason, they are still different semantically.
Consider:
x : {
int a = 5;
if(a == 3) break x;
if(a == 5) break y;
}
y: {
int b = 3;
if(b == 3) break y;
if(b == 5) break x;
}
In the first X block, if a equal to 5, it will not jump break for Y block, and compiler will also prompt error. Labeled break is not able to break forward, but backward only. That’s mean, if you are inside X block, you only can break from X block or said current block, but not another block. This is the difference between labeled break and goto.
It just provides another flexibility to Java programmer, make code easier in some cases when there is a need to jump out from nested loop, I think this is the most use cases can use labeled break.
Additionally, repeated labeled block with same name is valid. Example:
test : break test;
test : { break test;}
test : {
System.out.print("test");
break test;
}
It prints “test”.
Last
I think it is valuable knowledge to share with you. Wish happy coding in Java.
Related Links:
- http://www.java2s.com/Code/Java/Language-Basics/BreakWithLabelDemo.htm
- http://www.java2s.com/Code/Java/Language-Basics/Labelledbreaksbreaksoutofseverallevelsofnestedloopsinsideapairofcurlybraces.htm
- http://stackoverflow.com/questions/5099628/why-does-java-allow-for-labeled-breaks-on-arbitrary-statements
- http://stackoverflow.com/questions/4546925/is-goto-as-bad-as-people-say