aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switch/Label Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Switch/Label" Watch "Switch/Label" New topic
Author

Switch/Label

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
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

Joined: May 05, 2000
Posts: 13974
Because the continue pushes you to the end of the do-while loop where i gets incremented.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
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?
Meg Adal
Ranch Hand

Joined: Mar 12, 2002
Posts: 70

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

Joined: Jul 02, 2002
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
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

Joined: May 05, 2000
Posts: 13974
An IDE with step debugging would help make this clear.
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
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

Joined: Jul 01, 2002
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.


May P.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
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

Joined: Jul 30, 2001
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

Joined: Jul 30, 2001
Posts: 149
never mind I got the result.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch/Label
 
Similar Threads
Regarding Continue Keyword
pls consider this program i don't know how h value is incremented in Label1:
what is the diffrence b/W i and i++ and explain inner loops with conditions
Alone I do not obtain.....
Dan Chisholm's question from chapter 5