• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

continue statement example

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Rajiv Goyal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
vicken,Arun
thanks for your great effort,
rajiv
 
Arun Subbu
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic