I am getting error while using break statements in labels in java code. This is showing undefined label
. It is wrong to write code like this. Please assist me in using it correctly. Thanks in advance.
while (true)
{
label149: if (!localIterator2.hasNext());
while (true)
{
i++;
break;
HashMap localHashMap2 = (HashMap)localIterator2.next();
if (!((String)localHashMap1.get("name")).equalsIgnoreCase((String)localHashMap2.get("emotion")))
break label149;
if (!((String)localHashMap2.get("IS_paid")).equalsIgnoreCase("1"))
break label246;
((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "1");
}
label246: ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "0");
}
A Label object is a component for placing text in a container. A label displays a single line of read-only text. The text can be changed by the application, but a user cannot edit it directly.
Java does not support goto, it is reserved as a keyword just in case they wanted to add it to a later version. Unlike C/C++, Java does not have goto statement, but java supports label. The only place where a label is useful in Java is right before nested loop statements.
Java provides two types of branching statements namely, labelled and unlabelled.
A break
with a label is not the same as a goto
statement. Java does not have a goto
statement.
A label marks the statement that follows it. You can use it to break out of that statement, and only out of that statement. Control of flow will always transfer to the end of the labeled statement.
So what do you have here?
label149: if (!localIterator2.hasNext());
Because of the semicolon after the if, this is in fact the entire labeled statement. So your break label149
is not within its scope at all. If you did not have that semicolon, the if
block would include the while
, and then the break label149
would work. But control would be transferred to the line after the end of the while
block.
label246: ((HashMap)Saved.this.keynamedata.get(i)).put("is_paid", "0");
This is the statement marked by label246. Again, the break label246
is not inside it, so it is not in its scope, and you can`t break out of a statement you are not inside of.
With Break and continue, you could use label where you have complicated loops within your application. But its always advisable to avoid making use of labels as it becomes hard to read the code and resembles a bad design altogether (remember goto (still a keyword in java) in other languages?). An example of label would be like:
abc: for (;;) {
for (;; i++) {
if (i == 255) {
break abc;
}
}
}
System.out.println("Noww i am back");
This says that i need to break of loop which i have labeled as abc. So it will come out of outer for loop. If i would have omitted the abc there then it would have come out of the inner most loop i.e. the second loop from which i said it to break from. You can't expect label to be anywhere in the code.
A label break statement terminates the current loop and proceeds to the first statement that follows the loop that is labeled by the identifier that follows the keyword break.
Eg:
for (int i=0; i<3; i++)
{
resume:
for (……………)
{
……..
if (……..) break resume;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With