Character retry in java: what is it?

  When looking at the source code, retry: appears, what does this mean? I haven't touched it before, so let's take a simple example.

  public void testRetry() {
        int i = 0;
        retry:  //①
while (true) {        
            i++;
            System.out.println("i=" + i);
            int j = 0;
//            retry:   //②
for (; ; ) {            
                j++;
                System.out.println("j=" + j);
                if (j == 2) {
                    break retry;
                }
            }
        }
    }

The first thing to note is that retry: can be understood as a special tag in java, where retry can be replaced with any legal name.

a:, b: A13: .....all are possible


1. Open ①, close ② to print the result

i=1
j=1

j=2

2. Open ②, close ①, print the result

....

j=1
j=2
i=132348
j=1
j=2
i=132349
j=1
j=2
i=132350
j=1

j=2

... keep printing in a loop

retry is equivalent to a marker, which is only used in a loop, much like a goto statement, which breaks to the retry character. If the retry is not in the loop (for, while), when the retry is executed, the whole loop will be jumped out. If retry is in the loop, it can be understood as jumping to the keyword to execute, no matter how many loops are. The same is true for continue understanding.

Notice

retry: It needs to be declared before for, whlie, do...while, and the variable only follows break and continue.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325645196&siteId=291194637