This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch/Label

 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the continue pushes you to the end of the do-while loop where i gets incremented.
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Meg Adal
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just added comments to show flow of control hope this will help you
[ August 21, 2002: Message edited by: Megan Adal ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An IDE with step debugging would help make this clear.
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your explanation.
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);
}
}
Can you explain me about this?
Its printing output 3 6 12.I wanted to know how.
 
May Pat
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
never mind I got the result.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic