Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# loop question with labeled continue

Bob Young
Ranch Hand
Posts: 65
This example is from Bruce Eckel, Thinking in Java p. 138.

The output is:
i= 0
k = 0
k = 1
k = 2
k = 3
continue inner
i= 1
k = 0
k = 1
k = 2
k = 3
continue inner
i= 2
continue
i= 3
break
i= 4
k = 0
k = 1
k = 2
k = 3
continue inner
i= 5
k = 0
k = 1
k = 2
k = 3
continue inner
i= 6
k = 0
k = 1
k = 2
k = 3
continue inner
i= 7
continue outer
i= 8
break outer
My confusion is why is there a difference in behavior between
loop #2 and loop #3 when a labeled continue is used?
After leaving loop #3 via the continue inner statement, the loop #3 always starts anew whenever it is revisited (as can be seen with k= 0...3). However this same behavior is not seen in loop #2 when program flow leaves the loop via the continue outer statement. The loop does not start anew the next time. Why?
[ Jess added UBB [CODE] tags, adjusted white space and disabled smilies ]
[ August 29, 2002: Message edited by: Jessica Sant ]

Jose Botella
Ranch Hand
Posts: 2120
Hello Bob.
Your assumption isn't correct as this output shows:
"
continue inner
i= 2
"
I mean continue inner and continue outer aren't behaving differently: the flow of the program is due the way is written.
The point in this exercise is to show that continue and break will affect the inmediately enclosing loop. And if you want these instructions to affect other loop you must use a label. Review the flow taking this in account and the output will be clear.

Bob Young
Ranch Hand
Posts: 65
I need some more help with this one. I have a serious case of flawed logic! Let me try to explain what is going on and somebody tell me where I have the wrong concept.
As I see it:
When i = 0, flow continues to loop #3, which eventually encounters the continue inner statement. At this point the program flow leaves loop #3 and flows to the inner label. When i = 1, flow eventually get to loop #3 again, however the for loop has started fresh(ie k = 0).
Now when i = 7, flow is interrupted in loop #2, via the continue outer statement. At this point the program flow leaves loop #2 and flows to the outer label. When program flow moves back through loop #2 the loop counter does not start at i = 0! This is where I am confused. The two do not seem to be behaving the same. In both cases the current loop has been interrupted to a label outside the current loop. Yet one loop (loop #3) gets a new counter while the other (loop #2) gets the next increment. Why? What do I have wrong?

Jose Botella
Ranch Hand
Posts: 2120
Bob I can see it now.
continue doesn't jump back to the label as a goto to label would do. continue will jump the remaining instructions in the loop, as a goto to the end of a the loop would do. Now the third and second expressions in for would execute. While there is an increment i++ in inner loop, there's none in outer. This respond your question.
This is also the reason why there's a i++ just before break and other before continue outer:
a)breaking inner won't increment i and the next interation of outer would have i with the same value if i++ not present.
b)the same can be said about continue outer

JOSEPH BIH
Ranch Hand
Posts: 44
What if we change the statement when i is 7 and 8 just as
continue rather than continue outer and