This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

switch stmt

 
anushree ari
Ranch Hand
Posts: 98
  • 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
  • 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.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!