Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch stmt

 
anushree ari
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
shweta mathur
Ranch Hand
Posts: 109
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic