| Author |
continue statement example
|
Rajiv Goyal
Greenhorn
Joined: Jul 28, 2003
Posts: 25
|
|
see the code: public class Rainbow { 2. public static void main(String[] args) { 3. int count = 0; 4. String[] colours = {"red", "orange", "yellow", "green", 5. "blue", "indigo", "violet"}; 6. start: for (int i = 0; i < 5; i++) { 7. switch (i) { 8. case 0: 9. count--; 10. break; 11. case 3: 12. continue start; 13. case 4: 14. count--; 15. break start; 16. default: 17. count++; 18. break; 19. } // switch end 20. count++; 21. } 22. System.out.println(colours[count]); 23. } 24.} 1 Standard output will display 'red'. 2 Standard output will display 'green'. 3 Standard output will display 'indigo'. 4 Standard output will display 'violet'. 5 An ArrayIndexOutOfBoundsException will be thrown, halting the program Answer is green(2) -Lost somewhere in making out flow in continue statement. Can someway explain the flow clearly with values in each iterations? thanks, rajiv
|
 |
Arun Subbu
Greenhorn
Joined: Aug 30, 2003
Posts: 25
|
|
Hi, This is the flow. hope this helps count = 0; i = 0 count = -1; count = 0 (Outside the switch) i =1 count = 1; count = 2(Outside the switch) i =2 count = 3; count =4 (Outside the switch) i =3 ,tries to transfer the control to start of the loop,since there is no break,the code falls to case i =4 count =3,now the break start breaks the for loop itself. so colours[count] evalutes to colours[3] which is green.
|
 |
Arun Subbu
Greenhorn
Joined: Aug 30, 2003
Posts: 25
|
|
Hi Rajiv, sorry about the previous post. i misunderstood the code. this is right execution sequence. count = 0; i = 0 count = -1; count = 0 (Outside the switch) i =1 count = 1; count = 2(Outside the switch) i =2 count = 3; count =4 (Outside the switch) i =3 ,transfers the control to start of the loop, I = 4 count = 3; break start breaks the for loop. so colours[count] evalutes to colours[3] which is green.
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
Rajiv> make your code more clear by using UBB code, it'll allow more people to help you. Arun> the break statement in the switch clause doesn't break the loop, it only breaks the switch statement... i see you've missed the count++ after the switch. here are the correct values for each iteration: itr 1)i = 0 count = 0 -1 0 itr 2)i = 1 count = 0 1 2 itr 3)i = 2 count = 2 3 4 itr 4)i = 3 count = 4 // no vars are changes here except for i itr 5)i = 4 count = 4 3 // the for loop breaks as you can see final count value is 3, therfore printing green. [ September 04, 2003: Message edited by: Vicken Karaoghlanian ] [ September 04, 2003: Message edited by: Vicken Karaoghlanian ]
|
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
|
 |
Rajiv Goyal
Greenhorn
Joined: Jul 28, 2003
Posts: 25
|
|
vicken,Arun thanks for your great effort, rajiv
|
 |
Arun Subbu
Greenhorn
Joined: Aug 30, 2003
Posts: 25
|
|
Hi Vicken, I believe you are wrong. break start will break the loop for sure.i am debugging the code. change break start to break,now count will become 4 and then it will go to the begining of the loop,the conition fails and it will print blue colours[4] = blue. correct me if i am wrong.
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
hi arun, i was refering to break NOT break start break will get you out of the switch, increment count (count++), then precede to the next iteration of the loop. break start will get you out of the switch stat. AND also the for loop, in this case count is not incremented (count++). in your first iteration of the the loop count becomes 0 not -1, because the break stat. in case0 will break the switch then it'll procede down to count++ and then finally incrementing the index i. i beleive my output is correct, if you don't think so please explain why. [ September 04, 2003: Message edited by: Vicken Karaoghlanian ]
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
Arun... i don't no why are we arguing about!!! the code you posted is the same as mine... the only problem is that i've just noticed that i misread your output. i believe we are both correct.
|
 |
 |
|
|
subject: continue statement example
|
|
|