Win a copy of Machine Learning with TensorFlow this week in the Artificial Intelligence and Machine Learning forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

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).]


 
I'm sure glad that he's gone. Now I can read this tiny ad in peace!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic