Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Labeled continue statement within while loop

Tags:

java

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

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? It doesn't say in the 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; that statement, which is called the continue target, then immediately ends the current iteration and begins a new one.

like image 970
St.Antario Avatar asked Oct 14 '14 11:10

St.Antario


People also ask

Can you use continue in a while loop?

continue passes control to the next iteration of a for or while loop. It skips any remaining statements in the body of the loop for the current iteration. The program continues execution from the next iteration. continue applies only to the body of the loop where it is called.

What is Labelled continue statement?

Labeled continue Statement It includes the label of the loop along with the continue keyword. For example, continue label; Here, the continue statement skips the current iteration of the loop specified by label .

Can Continue statement be used in do-while?

The continue statement passes control to the next iteration of the nearest enclosing do , for , or while statement in which it appears, bypassing any remaining statements in the do , for , or while statement body.

Can you use continue in a while loop Java?

Java contains a continue command which can be used inside Java while (and for ) loops. The continue command is placed inside the body of the while loop. When the continue command is met, the Java Virtual Machine jumps to the next iteration of the loop without executing more of the while loop body.


1 Answers

A continue means go to the start of a loop. So when you continue to a label, the label has to be on a loop. (It is not a goto statement ...)


Does it allow to tranfer control to the only while, for or do statement? It doesn't say in the JLS.

Actually, it does say.

Here's what JLS 14.6 (Java 8 revision) really says:

"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.

To be precise, a continue statement with label Identifier always completes abruptly, the reason being a continue with label Identifier.

The continue target must be a while, do, or for statement, or a compile-time error occurs."

(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.

like image 92
Stephen C Avatar answered Oct 11 '22 06:10

Stephen C