• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

break in switch

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this from the MArcus Green tutorial
public class MySwitch{
public static void main(String argv[]){
MySwitch ms= new MySwitch();
ms.amethod();
}
public void amethod(){
int k=10;
switch(k){
default: //Put the default at the bottom, not here
System.out.println("This is the default output");
break;
case 10:
System.out.println("ten");
case 20:
System.out.println("twenty");
break;
}
}
}

Tha ans is it prints out ten and twenty as there is no break after the case10 condition.But I thought that even if it falls through to case 20 as it is not satisfied(as k is 10) it wont print out twenty.Please clarify.If so if we dont give break and the default is specified shouldnt it always printout?
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mindy -
Once the condition is satisfied (in this case printing "ten"), the program will not exit the switch statment until it reaches a break or the end of the switch statement. Therefore, it will enter the other options and print the additional output (in this case "twenty").
As for the default statement it will not always print. Rather, the output will be printed in two instances. One, the expression evaluates to false, thus the default prints. Second, the expression evaluates to true but the break is missing. In this case, if the default statement is located AFTER the statement evaluated to true the program could fall through to it, unless it reaches a break first (for instance, if the default and case 20 swapped places in this example, the default output would print).
Hope that helps!
Chris

[This message has been edited by Chris Cingrani (edited January 09, 2001).]
 
Mindy Hudson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chris.That makes it really clear...I was confused earlier

Originally posted by Chris Cingrani:
Mindy -
Once the condition is satisfied (in this case printing "ten"), the program will not exit the switch statment until it reaches a break or the end of the switch statement. Therefore, it will enter the other options and print the additional output (in this case "twenty").
As for the default statement it will not always print. Rather, the output will be printed in two instances. One, the expression evaluates to false, thus the default prints. Second, the expression evaluates to true but the break is missing. In this case, if the default statement is located AFTER the statement evaluated to true the program could fall through to it, unless it reaches a break first (for instance, if the default and case 20 swapped places in this example, the default output would print).
Hope that helps!
Chris
[This message has been edited by Chris Cingrani (edited January 09, 2001).]


 
Good night. Drive safely. Here's a tiny ad for the road:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic