Java – continue statement marked in the while loop

The following simple example causes compile - time errors But it is 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

However, the following functions are normal:

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 control to be transferred to while, for, or do statements? It is not stated in JLS It actually says:

Solution

Continuing means going to the beginning of the cycle Therefore, when you continue to use the label, the label must be in a circular state (this is not a goto statement...)

Actually, it is

This is what JLS 14.6 (Java 8 Revision) really says:

(bold in original text!)

The bold sentence says that the statement attached to the label (called "continue target") must be some kind of circular statement

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>