Reputation:
I'm busy studying for my certification and I stumbled upon a concept I've never even heard before - "Labeled Statements". e.g:
'label' : 'statement'
L1: while(i < 0){
L2: System.out.println(i);
}
So my question is.. why? How is this useful and when would one want to use something like this?
Upvotes: 17
Views: 4703
Reputation: 66243
As other answers have stated, labels are a seldom used part of the Java language.
But in your case some other things should be considered:
The labels are quite "generic" and are in fact line numbers: L1
, L2
, ...
The labels are not used in the code.
You are studying material for a certification.
This means, that the L1
, L2
labels are simply line numbers. I assume, that the text explaining the code refers to that line numbers. In the same way some books and papers enumerate all mathematical terms just for referencing them in the text or to make citations easier.
Upvotes: 0
Reputation: 533492
It can be used to avoid the need for a "not found" flag.
FOUND: {
for(Type t: list)
if (t.isTrue())
break FOUND;
// handle not found.
}
This is perhaps a misuse of labels, but you can use them to break without a loop.
LABEL: {
if(condition)
break LABEL;
// do something
}
It can also be used to confuse people, which is a good reason to avoid it. ;)
http://www.google.com
while(true) break http;
Upvotes: 22
Reputation: 78316
I think that they are required so that you can write Fortran while pretending to write Java. Without them, the aphorism Real programmers write in Fortran whatever language they are using might be invalidated.
Upvotes: 2
Reputation: 12056
Here is an example of inordinate break
that is likely to be missed out by the rest of the replies. It allows to break a loop within switch{} statement:
loop: for(;;){
int c=in.read();
switch(c){
case -1:
case '\n':
break loop;
case 'a':
processACommand();
break;
case ...
default:
break;
}
}
Upvotes: 2
Reputation: 6381
I used to use those as comment statements :) Jokes aside, it is like the Go to statements in basic, which allows you to jump to a line of code, ie during a deep looping structure...
Usage:
scan: {
int c;
for (firstUpper = 0 ;
firstUpper < count ;
firstUpper += Character.charCount(c)) {
c = codePointAt(firstUpper);
if (c != Character.toLowerCase(c)) {
break scan;
}
}
return this;
}
Upvotes: 2
Reputation: 103777
The only use that I'm aware of is that you can use labels in break
or continue
statements. So if you have nested loops, it's a way to break out of more than one level at a time:
OUTER: for (x : xList) {
for (y : yList) {
// Do something, then:
if (x > y) {
// This goes to the next iteration of x, whereas a standard
// "continue" would go to the next iteration of y
continue OUTER;
}
}
}
As the example implies, it's occasionally useful if you're iterating over two things at once in a nested fashion (e.g. searching for matches) and want to continue - or if you're doing normal iteration, but for some reason want to put a break/continue in a nested for
loop.
I tend to only use them once every few years, though. There's a chicken-and-egg in that they can be hard to understand because they're a rarely-used construct, so I'll avoid using labels if the code can be clearly written in another way.
Upvotes: 26