File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes switch stmt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch stmt" Watch "switch stmt" New topic

switch stmt

anushree ari
Ranch Hand

Joined: Sep 30, 2002
Posts: 98
class switch1{
public static void main(String args[])
char ch='c';
case 'a':System.out.println("a");break;
case 'b':System.out.println("b");break;
it prints "a" how?
anybody ans me

shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
That is becoz of the position of default statement.There is no break statement after default, so the next case is considered & "a" is printed. Only when it finds break statement, the switch statement is left.
Change your program to the following and nothing will be printed.

Remember : Execution of the BREAK statement only transfers control out of the switch statement, else execution of the next label continues.

--Shweta<br />SCJP 1.4 <br />SCWCD
I agree. Here's the link:
subject: switch stmt
It's not a secret anymore!