This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
 
Similar Threads
Selection Statements
Jqplus
switch statement
XML file parsing in Java using SAX
Help with english to morse code program, I have no idea what I'm doing