This week's book giveaways are in the Refactoring and Agile forums.We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Understanding continue...label do...while

Kalabaw moo
Ranch Hand
Posts: 61
Hi, this is my first post. With the code posted below, I have the following output:

First Loop 1
Second Loop 1
First Loop 2
Second Loop 2

I'm trying to understand how did I have the output shown above.
If you have corrections, I'll appreciate it.

I have the "first" Label.
The do statement will execute the statement
And display: First Loop 1
then the "x" will be incremented to 1 because of ( x++ ) - so x is now 2
I have the "second" Label.
The do statement will execute the statement
And display: Second Loop 1
then the "y" will be incremented to 1 because of ( y++ ) - so y is now 2
the if statement will check if "y is equals to 2" - it's true so
The statement "continue first;" will go to the first label?, if so, is it going to check
the expression "while( y <= 2 );" first or is it going directly to first label?

fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Try changing your println statements to this:

System.out.println( "First Loop x is " + x + " y is " + y );

and

System.out.println( "SecondLoop x is " + x + " y is " + y );

and run it again. It might make more sense

vicky Aggarwal
Ranch Hand
Posts: 40

Try this way when if condition if( y == 2 ) succeed , next line executed is while( x <= 2 ) before moving to first.
it skip the condition of while( y <= 2 )
If you set while( x < 2 ) after continue there will be no more output

Kalabaw moo
Ranch Hand
Posts: 61

I changed the code to what fred has advised. What I wanted to understand is how did I have the output as to my first post. Now after changing the code I have this output:

First Loop x is 1 y is 1
SecondLoop x is 2 y is 1
First Loop x is 2 y is 2
SecondLoop x is 3 y is 2

When the inner loop is entered, the value of y is changed from 1 to 2. The condition that I have below resulted true-so the "continue first;" statement is executed. will it go to the label "first" right away? or will it test the "while( y <= 2 )" first before letting it go back to the label "first"?

Try this way when if condition if( y == 2 ) succeed , next line executed is while( x <= 2 ) before moving to first.
it skip the condition of while( y <= 2 )
If you set while( x < 2 ) after continue there will be no more output

What do you mean it'll skip the condition of "while( y <= 2 )"? If it's going to skip the condition while, therefore the loop will not finish because of the "continue label" that has the capability to skip over the statements.

Campbell Ritchie
Sheriff
Posts: 48424
56
This shows that labelled continue is a difficult construct to understand. You will find another opinion about un-labelled continue statements here. Think how much more confusing labelled continue is. Good excuse for not using it.

But you can see clearly whether your program goes into an infinite loop or not.

Kalabaw moo
Ranch Hand
Posts: 61
I truly understand that using continue must not be used coz it might cause spaghetti code. But I just wanted to understand the code I posted, how did it produce the output, but thank you for giving that link.

Campbell Ritchie
Sheriff
Posts: 48424
56
You're welcome

fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
if you want to go back to your original code:

assuming x = y = 1 before we start...
you get to line 4, with x = 1, y = 1. you print "First loop 1" since x = 1
line 5: increment x to 2, y still 1
when we get to line 9, we print "Second loop 1" since y = 1
line 10 - increment y to 2, so now x = 2, y = 2
line 12 - y does equal 2, so we enter the if-condiction on line 13
line 13: we IMMEDIATELY continue through the loop labeld 'first'. That means we get ready for the next iteration. We have to check the condition. Since x is <= 2, we do antother pass through. Nothing happens until...

line 4: we print "First loop 2" since x is still 2
line 5: x gets incremented to 3. y is still 2
we step down until line 9
line 9: we print "Second loop 2" since y is 2
we increment y to 3, leaving x at 3.

we fall into the 'else' block, where we continue the loop labeled 'second'. We have to test our condition. y <=2 is false, so the loop terminates. we drop back to the outer loop, and test it's conditions. x <=2 is false, so that loop terminates.

Kalabaw moo
Ranch Hand
Posts: 61
This is much clearer... that's what I understand actually, but I just wanted to make sure that my understanding is correct... But I was just curious why the second label didn't go to the label "second:" - whereas the continue first went back to the label "first" - as far as I understand the "continue....label" has the capability to skip the statements.???

fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
"continue label" basically means "stop everything and go to the next iteration of whatever loop has the 'label' attached to it".

And, after the first time through a do loop, the first thing that always gets done is the test to see if the loop should process again.

I guess you could say the 'continue' doesn't go to the label itself, but to the loop attached to the label. it is not a 'goto this line' kind of statement.

Kalabaw moo
Ranch Hand
Posts: 61
Thanks...