简体   繁体   中英

I need a help to understand this code

Actually, this is the first time I see a code like this:

class A
{
    public static void main(String args[])
    {
        outer : for(int i=0;i<10;i++)
        {
            for(int j=0;j<10;j++)
            {
                if(j > i)
                {
                    System.out.println();
                    continue outer;
                }
                System.out.print("  "  +( i *j ));
            }
        }
        System.out.println();
    }
}

two lines I don't understand:

outer : for(int i=0;i<10;i++) // this seems similar to 'for each'?

continue outer; // I know that 'continue' will break the loop and continue the next turn, but what will do in this situaton?

The outer: part is a label. It's basically labelling the loop. The loop itself works just as normal.

The continue outer; means "continue back to the start of the body of the loop labelled outer " (after incrementing and testing i of course). It's sort of like having a break; statement to get out of the inner loop, and then immediately having a normal continue; statement to continue with the next step of the outer loop.

outer : for(int i=0;i<10;i++) 

defines a label for the outer loop, called outer

continue outer; 

means, go to next iteration of the loop labeled outer

outer is a label, when continue outer; is called, the code will jump to the outer label and continue from there. The reason behind it in this case is that the coder wants to exit the inner loop when j>i and continue the outer loop.

Outer: is a label, instead of continuing the inner loop, you are continuing the outer loop by specifying the label

See also http://www.janeg.ca/scjp/flow/labels.html

outer是一个标签,而continue outer基本上是一个goto但是在循环中继续而不是再次启动它。

outer: defines a label "outer" which you can use in break and continue . It's only allowed in front of loop constructs ( for , while , do ).

So continue outer means "continue with the outer loop" or "apply the continue to the loop with the matching label".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM