question relates to block label and continue/break control flow

Discussion in 'Java' started by Shawn, Nov 29, 2006.

  1. Shawn

    Shawn Guest

    Hi,

    I ran into a problem. I need to label a block and use "continue" to
    control the program flow in certain conditions.

    mainForLoop:
    for(int i=1; i < 100; i++)
    {
    ...
    if(..)
    {
    ...
    continue mainForLoop;
    }
    else
    {
    ...
    }


    } //end of for loop, also end of the block mainForLoop

    The above code works fine and my program functions correctly. But I hope
    to add brackets for block1. The compiler says it is error. My code looks
    like:

    mainForLoop:
    {
    for(int i=1; i<100; i++)
    {
    ...
    if(..)
    {
    ...
    continue mainForLoop; //ERROR: continue cannot be used outside of a
    loop. Why?
    }
    else
    {
    ...
    }

    } //end of for loop
    } //end of block mainForLoop

    What is the problem? Ideally, I like the syntax of the second version,
    because it is clearer.

    Thank you.
     
    Shawn, Nov 29, 2006
    #1
    1. Advertising

  2. Shawn

    Shawn Guest

    To reiterate my problem, I just test a smaller code.
    This is OK:
    public class Demo2 {
    public static void main(String[] args)
    {
    mainForLoop:
    for(int i=1; i<=100;i++)
    {
    if(i%2 == 0) continue mainForLoop;
    System.out.println("i= " + i);
    }
    }
    }


    This is NOT OK:

    public class Demo2 {
    public static void main(String[] args)
    {
    mainForLoop:
    { //newly added
    for(int i=1; i<=100;i++)
    {
    if(i%2 == 0) continue mainForLoop;
    System.out.println("i= " + i);
    }
    } //newly added
    }
    }

    I cannot understand the error message: "continue cannot be used outside
    of a loop". Why an extra pair of brackets makes "outside of a loop"?
     
    Shawn, Nov 29, 2006
    #2
    1. Advertising

  3. Shawn wrote:
    ....
    > This is NOT OK:
    >
    > public class Demo2 {
    > public static void main(String[] args)
    > {
    > mainForLoop:
    > { //newly added
    > for(int i=1; i<=100;i++)
    > {
    > if(i%2 == 0) continue mainForLoop;
    > System.out.println("i= " + i);
    > }
    > } //newly added
    > }
    > }
    >
    > I cannot understand the error message: "continue cannot be used outside
    > of a loop". Why an extra pair of brackets makes "outside of a loop"?


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

    http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.16

    The continue target in the working version was a for statement, which is
    fine. In the new version the continue target is a block.

    The problem is that you have a continue with a target that is not a
    loop, not that the continue itself is outside a loop, so the message
    seems a bit confusing.

    Patricia
     
    Patricia Shanahan, Nov 29, 2006
    #3
  4. Shawn

    Oliver Wong Guest

    "Shawn" <> wrote in message
    news:ekksb3$qod$...
    > To reiterate my problem, I just test a smaller code.
    > This is OK:
    > public class Demo2 {
    > public static void main(String[] args)
    > {
    > mainForLoop:
    > for(int i=1; i<=100;i++)
    > {
    > if(i%2 == 0) continue mainForLoop;
    > System.out.println("i= " + i);
    > }
    > }
    > }
    >
    >
    > This is NOT OK:
    >
    > public class Demo2 {
    > public static void main(String[] args)
    > {
    > mainForLoop:
    > { //newly added
    > for(int i=1; i<=100;i++)
    > {
    > if(i%2 == 0) continue mainForLoop;
    > System.out.println("i= " + i);
    > }
    > } //newly added
    > }
    > }
    >
    > I cannot understand the error message: "continue cannot be used outside of
    > a loop". Why an extra pair of brackets makes "outside of a loop"?


    The label should be directly before the loop it's labeling:


    public class Demo2 {
    public static void main(String[] args)
    {
    { //newly added
    mainForLoop:
    for(int i=1; i<=100;i++)
    {
    if(i%2 == 0) continue mainForLoop;
    System.out.println("i= " + i);
    }
    } //newly added
    }
    }

    - Oliver
     
    Oliver Wong, Nov 29, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shawn
    Replies:
    2
    Views:
    278
  2. Shawn
    Replies:
    8
    Views:
    361
    Chris Uppal
    Dec 2, 2006
  3. Steve
    Replies:
    2
    Views:
    1,423
    Leonard Milcin
    May 1, 2008
  4. Wolfgang Nádasi-Donner
    Replies:
    0
    Views:
    126
    Wolfgang Nádasi-Donner
    May 31, 2007
  5. Knut Krueger
    Replies:
    8
    Views:
    282
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page