简体   繁体   English

while循环中标记的continue语句

[英]Labeled continue statement within while loop

The following simple examples cause compile-time error. 以下简单示例会导致编译时错误。 But It's not clear why. 但目前尚不清楚为什么。

public static void main (String[] args) throws java.lang.Exception
{
    int i = 0;
    d:
    {
        System.out.println("d");
    }
    while(i < 10){

        i++;
        continue d;

    }
}

--and-- - 和 -

public static void main (String[] args) throws java.lang.Exception
{
    int i = 0;
    d:
    {
        System.out.println("d");
        while(i < 10){
            i++;
            continue d;
        }
    }
}

DEMO DEMO

But the following works fine: 但以下工作正常:

public static void main (String[] args) throws java.lang.Exception
{
    int i = 0;
    d:
    while(i < 10){
        {
            System.out.println("d");
        }
        i++;
        continue d;

    }
}

Does it allow to tranfer control to the only while , for or do statement? 它是否允许将控制转移到whilefordo语句? It doesn't say in the JLS . 它没有在JLS中说。 What it actual says is: 实际说的是:

A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; 带有标签Identifier的continue语句尝试将控制转移到与其标签具有相同标识符的封闭标签语句(第14.7节); that statement, which is called the continue target, then immediately ends the current iteration and begins a new one. 该语句称为继续目标,然后立即结束当前迭代并开始新的迭代。

A continue means go to the start of a loop. continue意味着转到循环的开始。 So when you continue to a label, the label has to be on a loop. 因此,当您continue使用标签时,标签必须处于循环状态。 (It is not a goto statement ...) (这不是goto声明......)


Does it allow to tranfer control to the only while, for or do statement? 它是否允许将控制转移到while,for或do语句? It doesn't say in the JLS. 它没有在JLS中说。

Actually, it does say. 实际上,确实如此。

Here's what JLS 14.6 (Java 8 revision) really says: 以下是JLS 14.6 (Java 8修订版)的真实内容

"A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; that statement, which is called the continue target, then immediately ends the current iteration and begins a new one. “带有标签标识符的continue语句尝试将控制转移到与其标签具有相同标识符的封闭标记语句(第14.7节);该语句称为继续目标,然后立即结束当前迭代并开始新的迭代。

To be precise, a continue statement with label Identifier always completes abruptly, the reason being a continue with label Identifier. 确切地说,带有标签Identifier的continue语句总是突然完成,原因是继续使用标签Identifier。

The continue target must be a while, do, or for statement, or a compile-time error occurs. continue目标必须是while,do或for语句,否则会发生编译时错误。 "

(Bolded in original!) (用原文加粗!)

The bolded sentence says that the statement that the label is attached to (referred to as the "continue target") has to be a loop statement of some kind. 粗体句子说标签所附的声明(称为“继续目标”)必须是某种循环声明。

JLS 14.16 JLS 14.16

A continue statement may occur only in a while, do, or for statement (iteration statements) ; continue语句只能在while,do或for语句中发生(迭代语句) ;

The continue target must be a while, do, or for statement, or a compile-time error occurs. continue目标必须是while,do或for语句,否则会发生编译时错误。


A continue statement with label Identifier attempts to transfer control to the enclosing labeled statement (§14.7) that has the same Identifier as its label; 带有标签Identifier的continue语句尝试将控制转移到与其标签具有相同标识符的封闭标签语句 (第14.7节) ; that statement, which is called the continue target, then immediately ends the current iteration and begins a new one. 该语句称为继续目标, 然后立即结束当前迭代并开始新的迭代。


A continue statement must refer to a label within the immediately enclosing method, constructor, or initializer. continue语句必须引用直接封闭的方法,构造函数或初始化程序中的标签。 There are no non-local jumps. 没有非本地跳跃。 If no labeled statement with Identifier as its label in the immediately enclosing method, constructor, or initializer contains the continue statement, a compile-time error occurs . 如果在立即封闭的方法,构造函数或初始化程序中没有标识符作为其标签的带标签的语句包含continue语句, 则会发生编译时错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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