Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Switch/Label

suresh kamsa
Ranch Hand
Posts: 149

My analysis gave me after first System.out.println 3 it goes back to label3 which increments i++. Thats why I am getting 4 next time.
Why is it printing 3,5,9, why not 3,4,5,9 where am I going wrong.?
Thanks.
[ August 21, 2002: Message edited by: Thomas Paul ]

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
Because the continue pushes you to the end of the do-while loop where i gets incremented.

suresh kamsa
Ranch Hand
Posts: 149
case 3: continue label3;
can you be more specific please. It will not go to label3? still variable i will be incremented by 1 so the value should become 2 previous values j is 1 and k is 1, which is i+j+k = 4.
How come it is 5?

Ranch Hand
Posts: 70

[ August 21, 2002: Message edited by: Megan Adal ]

Dan Chisholm
Ranch Hand
Posts: 1865
Suresh,
It appears that your analysis has assumed that the "continue" statement behaves like a "goto" statement. In reality, the continue statement causes control to be passed to the "loop-continuation point of an iteration statement". In the case of a "do-while" loop, the continuation point is the "while" statement. As a result, the boolean expression that controls the loop is evaluated. In the case of this question, the boolean expression for the inner most loop has the side effect of incrementing the value of variable i.

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
Originally posted by suresh kamsa:
case 3: continue label3;
can you be more specific please. It will not go to label3? still variable i will be incremented by 1 so the value should become 2 previous values j is 1 and k is 1, which is i+j+k = 4.
How come it is 5?
The continue pushes you to the end of the do-loop and then you go to the label.

Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
An IDE with step debugging would help make this clear.

suresh kamsa
Ranch Hand
Posts: 149
class Gray {
public static void main (String args[]) {
int i = 0;
int j = 0;
int k = 0;
label1:
do {
k++;
label2:
do {
j++;
label3:
do {
i++;
System.out.print((i + j + k + ", "));
switch ((i+j+k)) {
default: break label1;
case 3: break label3;
case 4: break label3;
case 5: break label2;
case 6: continue label3;
case 7: continue label2;
case 8: continue label2;
case 9: continue label1;
}
} while (i++ < 2);
} while (j++ < 2);
} while (k++ < 2);
}
}
Its printing output 3 6 12.I wanted to know how.

May Pat
Ranch Hand
Posts: 32
Suresh,
Try this. I added some more codes to the program to help see what happens when it executes so you can get a better idea of how continue and break work. -- Got the idea from Dan Chrisholm's exams.

Dan Chisholm
Ranch Hand
Posts: 1865
May,
Thank you for posting your suggestion.
Suresh,
I just updated the remark for that question. Please let me know if the new remark is helpful or just a little too long.

A break statement with a label will transfer control outside of the enclosing statement with the matching label. Furthermore, the enclosing statements complete abruptly. In the case of a do-while loop, the boolean expression that controls the loop is not evaluated as control transfers out of the loop. However, if the break statement had been executed inside a try-catch block, then the finally clause would have been processed. The first iteration of this code example executes the case statement with the value 3. As a result, the statement "break label3" is executed causing the inner most loop to complete abruptly and control to be transferred out of the inner most loop to the second nested loop. The boolean expression of the second loop is evaluted and variable j is incremented as a side effect. The boolean expression evalutes as true and a second iteration begins. Variable j is incremented again at the top of the loop and varible i is incremented at the top of the inner most loop. The switch expression evaluates as six. A continue statement without a label will cause the current iteration of the enclosing loop to end and the next iteration to begin. As a result, the boolean expression that controls the loop is evaluated as the next iteration begins. Similarly, a continue statement that has a label transfers control to the next iteration of the enclosing loop that has the same label. If the continue statement causes control to break out of a nested loop to transfer control to an outer loop with a matching label, then execution of the nested loop is completed abruptly as control transfers to the outer loop. Furthermore, the boolean expression that controls the nested loop is not evaluated as control is abruptly transferred to the labeled outer loop.

suresh kamsa
Ranch Hand
Posts: 149
case 6: System.out.println(" continue label3;");
continue label3;
At this point i=2,j=3,k=1 when the statement continue label3 executes,when it comes out of the while of that condition(i value) 2 < 2. I would like to know where the control goes from here. Is it will go to next while loop, if so ( j value) 3 < 2. This condition also not satisfied. Is it goes to third while condition (k value) k++ < 2.
I am not sure still whats going on here.

suresh kamsa
Ranch Hand
Posts: 149
never mind I got the result.