Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can "continue" be used in a switch statement?

 
lucy hu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following code:

1. float f=2.14f;
2. outer: switch( (int)f )
3. {
4. case 2 :switch((int)f)
5. {
6. case 2: System.out.println("value is 2");
7. case 3 : System.out.println("value can be 3");continue outer
8. case 4 : break;
9. }
10. case 5: System.out.println("value is 5");break;
11. case 6: System.out.println("value is 6");break;
12. }

what will be the output:
 
N Mukherjee
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
good question.Output is
****************
value is 2 value can be 3 followed by value is 5
****************
Hope i am correct
regards,
NM
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
code will not compile cause continue can only be used in do-while, while-do and for-loops.....
cheers
Oliver
 
Bruce Lee
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Where is the continue? I dont see it here.
 
Oliver Grass
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 7....
 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Oliver pointed out, it won't compile. And here is the exact message:

Topic005889.java:12: 'continue' must be in loop.
continue outer;
^

I cut and pasted your code in a file/class named by this topic.
Note, you were missing a ";" at the end of the continue statement.
------------------
Hope this helps.
Have a good day.
[This message has been edited by Serge Plourde (edited November 29, 2000).]
 
Danish Shaukat
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The labelled continue statement can only be used with repitition structures.
Regards
Danish Shaukat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic