How to use labels in java code?

26,496

Solution 1

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.

Solution 2

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.

Solution 3

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;
                 }
    }
Share:
26,496
Param
Author by

Param

Updated on September 15, 2021

Comments

  • Param
    Param almost 3 years

    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");
              }