Labeled Statement block in Java?

后端 未结 4 1203
后悔当初
后悔当初 2020-11-29 11:39

I was browsing through some of the base Java objects when I found a section of code surrounded by a scan: {} block. The following code is from the toLowerCase()

相关标签:
4条回答
  • 2020-11-29 11:43

    It is a labeled block. where scan: is a label. It is commonly used when breaking/continue in case you have multiple loops. In this case break scan; simply breaks outta the labeled block(scan) when executed.

    0 讨论(0)
  • 2020-11-29 12:00

    Here, scan: is simply a label. The break <label> syntax allows one to break out of outer loops, and to simulate some forms of the goto statement. The syntax is documented in the JLS:

    A break statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; this statement, which is called the break target, then immediately completes normally. In this case, the break target need not be a switch, while, do, or for statement.

    0 讨论(0)
  • 2020-11-29 12:02

    You can set a label to break / or continue from within multiple loops deep.

    Example

     outer:
     for(int i=...){
       for(int j=..){
         ...
         break outer; // leaves both loops   
    
       } 
    
     }
    
    0 讨论(0)
  • 2020-11-29 12:09

    It is a label. It is a indicator for flow control.

    If you look at your code, you see below

     break scan;
    

    When this happens, the flow exits completely the scan block.

    By the way, it can be any identifier, scan is not a keyword at all.

    0 讨论(0)
提交回复
热议问题