aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switch statement 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 statement " Watch "Switch statement " New topic
Author

Switch statement

Kasak Tahilramani
Ranch Hand

Joined: Apr 06, 2006
Posts: 45
Can you use break and continue with labels in switch ?Because in Javabelt mock exams it says that you cannot use continue with labels in switch .
Please provide some example and solve this confusion .
Tommaso Nuccio
Ranch Hand

Joined: Dec 11, 2006
Posts: 66
Hi,

a switch statement is nothing else then
if()
else if()
else if()
else

Thus, labels with break and continue only make sense in loops, i.e. for, while.


Ciao,<br /> Tommaso<br /> <br />~*~*~*~<br />There are 10 types of people, those who understand binary and those who don't.
Kasak Tahilramani
Ranch Hand

Joined: Apr 06, 2006
Posts: 45
But in exam cram book page 131 it says you can use labels with continue and break in switch.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If only there were some way that we could write programs ourselves to discover which of these statements is true.


"I'm not back." - Bill Harding, Twister
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
If only there were some way that we could write programs ourselves to discover which of these statements is true.




Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
Hi kasak,

See the code below……


class Code1{
public static void main (String[] args){
level1 :
for(int i=0 ; i <= 10 ;i++){
if(i == 9)continue level1;
System.out.println("Count is " + i);
}
}
}



class Code2{
public static void main (String[] args){
int i=0;
level1 :
for(; i <= 10 ;i++){
switch(i){
case 1 :
if(i == 9)continue level1;
System.out.println("Count is " + i);
default:
System.out.println("Default " + i);
break ;
}
}
}
}

We’ve got two forms of break & continue (one is labeled & another is unlabeled).We can use break in switch & in loop causes control coming out of current context.Whereas we can use continue in loops only whether they are labeled or unlabeled.
Try these codes …..
Hope this will help

Regards.....

Maneesh Saxena


SCJP 1.4, SCWCD 1.5
Maneessh saxena
Ranch Hand

Joined: Oct 03, 2006
Posts: 125
Originally posted by raunak saxena:
We can use break in switch & in loop causes control coming out of current context.
Hope this will help



In addition we can use labeled form of break also causes control coming out of label specfied....
Kasak Tahilramani
Ranch Hand

Joined: Apr 06, 2006
Posts: 45
Thanks a lot ranchers !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Switch statement
 
Similar Threads
Error while Running Code
switch statements
ATTENTION. Study few of the given rules. Will help in passing exam.
continue?
Declaration cannot be labelled