Question regarding using label for break, continue flow control

Discussion in 'Java' started by Shawn, Oct 25, 2006.

  1. Shawn

    Shawn Guest

    Hi,

    I need to use break and continue to transfer my program flow. I hope to
    use labels to explicitly state where the control flow will be, as
    suggested by somebody in the newsgroup. I have noticed something strange:

    block1:
    { //compiler doesn't allow "{" here
    for (int i = 0; ..)
    {
    if (conditionSatisfied) continue block1; //this will move control to
    the beginning of for loop, for loop will continue to run

    } //end of for loop
    } //compiler doesn't allow "}" here

    block2:
    { //now compiler allows "{" here
    ...//more code
    for (int i = 0; ..)
    {
    if (conditionSatisfied) break block2; //this will move control to the
    end of block2 immediately
    }
    ...//mode code

    } //end of block2, break block2 will bring control to the line below
    this line

    My questions:
    1) Why block1 cannot have a pair of {} while block2 can?
    2) Is my understanding of the control flow using break or continue
    correct in this demo?

    Thank you very much.
     
    Shawn, Oct 25, 2006
    #1
    1. Advertising

  2. Shawn

    Oliver Wong Guest

    "Shawn" <> wrote in message
    news:ehoesj$quf$...
    > Hi,
    >
    > I need to use break and continue to transfer my program flow. I hope to
    > use labels to explicitly state where the control flow will be, as
    > suggested by somebody in the newsgroup. I have noticed something strange:
    >
    > block1:
    > { //compiler doesn't allow "{" here
    > for (int i = 0; ..)
    > {
    > if (conditionSatisfied) continue block1; //this will move control to the
    > beginning of for loop, for loop will continue to run
    >
    > } //end of for loop
    > } //compiler doesn't allow "}" here
    >
    > block2:
    > { //now compiler allows "{" here
    > ...//more code
    > for (int i = 0; ..)
    > {
    > if (conditionSatisfied) break block2; //this will move control to the end
    > of block2 immediately
    > }
    > ...//mode code
    >
    > } //end of block2, break block2 will bring control to the line below this
    > line
    >
    > My questions:
    > 1) Why block1 cannot have a pair of {} while block2 can?
    > 2) Is my understanding of the control flow using break or continue correct
    > in this demo?


    "continue" only makes sense when you're iterating over something (e.g. a
    for loop, do loop or while loop). Did you try applying the "block1:" label
    directly onto the forloop, rather than onto a block containing the for-loop?
    Alternatively, you could use "continue" without a label at all, and the
    compiler will infer you mean that you want to continue the inner-most
    iteration.

    (Your understanding of "break" seems to be correct).

    - Oliver
     
    Oliver Wong, Oct 25, 2006
    #2
    1. Advertising

  3. Shawn

    Bart Guest

    Shawn wrote:
    <snip>
    > My questions:
    > 1) Why block1 cannot have a pair of {} while block2 can?


    The block itself is correct. You can't 'continue' to block1 because
    it's not a loop block.

    > 2) Is my understanding of the control flow using break or continue
    > correct in this demo?


    I'm not sure why you're doing all this. You seem to be trying to jump
    around the code in a "goto" fashion instead of structuring it
    correctly.

    <evil>
    You could wrap block1 in a do..while(false) loop but this is similar to
    using exceptions for jumping to a different part of the code. Don't do
    it!
    </evil>

    Regards,
    Bart.
     
    Bart, Oct 26, 2006
    #3
    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. Jianli Shen
    Replies:
    3
    Views:
    2,058
  2. Shawn
    Replies:
    3
    Views:
    331
    Oliver Wong
    Nov 29, 2006
  3. Jack Dowson
    Replies:
    0
    Views:
    456
    Jack Dowson
    May 7, 2007
  4. Steve
    Replies:
    2
    Views:
    1,413
    Leonard Milcin
    May 1, 2008
  5. Knut Krueger
    Replies:
    8
    Views:
    280
    Knut Krueger
    Aug 20, 2013
Loading...

Share This Page