File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "switch stmt" Watch "switch stmt" New topic
Author

switch stmt

anushree ari
Ranch Hand

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


anushree
shweta mathur
Ranch Hand

Joined: Sep 23, 2002
Posts: 109
Anushree,
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: http://aspose.com/file-tools
 
subject: switch stmt