-6
import java.util.Scanner;
class testing
{
    int i ; int j ;
    Scanner sc = new Scanner(System.in) ;
    void lolwa()
    {
        out:
        for(i = 0 ; i <= 6 ; i++)
        {
            System.out.println(i);
        }
        System.out.println("Enter 1 to restart loop");
        System.out.println("Enter 2 to continue till 10");
        j = sc.nextInt();
        if(j == 1)
        {
            continue out ;
        }
        if(j == 2)
        {
            for(i = 7 ; i <= 10 ; i++)
            {
                System.out.println(i);
            }
        }
    }
}

I compiled it in BlueJ and it's saying: "Undefined label out". I just learnt using continue statement, so I can't figure out the problem

Michael
  • 41,989
  • 11
  • 82
  • 128
Rishava
  • 3
  • 1
  • a weird question: why don't you simply call `lolwa()` instead of using a continue? – Turtle Aug 10 '17 at 15:09
  • 2
    your continue comes after the loop with the label out has already finished. Did you misplace your braces by any chance? – OH GOD SPIDERS Aug 10 '17 at 15:09
  • 4
    You should indent your code properly - `continue` needs to be in a loop. – assylias Aug 10 '17 at 15:10
  • The `continue` statement must be enclosed in a loop (which it isn't) and the label referenced must be declared outside the loop enclosing the `continue` statement (which again, it isn't). Also using labels is hardly ever necessary in Java. – Mena Aug 10 '17 at 15:11
  • 2
    @assylias: I don't see any indentation issues above...? (*Other* issues, yeah, but...) – T.J. Crowder Aug 10 '17 at 15:11

2 Answers2

5

Your out label "annotates" this for-loop:

out:
for(i = 0 ; i <= 6 ; i++)
{
    System.out.println(i);
}

So in order to continue with the label out, it would need to be within that for-loop. It doesn't make sense to use it after that for-loop has ended. This will compile, for example:

out:
for(int i = 0; i <= 6; i++)
{
    System.out.println(i);
    continue out;
}

In this case, you do not need a labeled continue statement, however. There is no ambiguity. There's only one loop that can be "continued" out of.

The Oracle tutorial explains it nicely.


It's worth mentioning that I can't even remember the last time I had to use labeled continue - maybe never. I would avoid them unless you have almost no other choice.

Turtle
  • 1,626
  • 16
  • 26
Michael
  • 41,989
  • 11
  • 82
  • 128
  • 2
    I can only assume it's a remnant of the JSR command (for example) from assembly. I've used them before because there's literally no other option. IMO using a label to control logic flow in a high level language like java is indicative of bad design in virtually every single case. – tnw Aug 10 '17 at 15:20
  • There are limited nested-loops use cases where the alternative (a flag variable and/or redundant checks) unnecessarily complicates the code. They're few and far between. :-) – T.J. Crowder Aug 10 '17 at 15:23
  • @Michael I don't know why you're demonstrating it with a break statement though. TO is using a continue. The demonstration apllies the same way, but still :p – Turtle Aug 10 '17 at 15:58
1

I think you don't get the use of continue label: it must be used in a loop, and is used to jump to your specific label. an if statement is not a loop. Just replace your continue out by a call to lolwa(), and you'll get the desired bahaviour: you'll get sent to the start of your function.

Turtle
  • 1,626
  • 16
  • 26