• 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

switch statement, question about default

 
Ranch Hand
Posts: 36
2
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Jeanne's&Scott's OCA 8 book about default case of the switch statement is stated the following:

There is no requirement that the case or default statements be in a particular
order, unless you are going to have pathways that reach multiple sections of the switch
block in a single execution.



Here http://www.tutorialspoint.com/java/switch_statement_in_java.htm is said that

A switch statement can have an optional default case, which must appear at the end of the switch.



I wrote several examples with default case in different places of the switch statement. Everything worked perfectly.
I also checked the Java documentation. There is nothing about the order of default case.
Seems like they're wrong in TutorialsPoint.

So how is it with the default case? (Just want to be sure.)

Thanks for your help.

 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Aleksandra Pestova wrote:Seems like they're wrong in TutorialsPoint.

Yes they are when they say "must appear at the end of the switch." Seems they just copied from cpp page here is link. Here is more wrong information

http://www.tutorialspoint.com/ wrote:A switch statement can have an optional default case, which must appear at the end of the switch. No break is needed in the default case.

Click here to see
  • In that case, also no break is needed for any case label  It will run successfully. Check below program.
  • See it is also not compulsary that case should be followed by break. It relies on your need. Suppose you wrote program to buy no of bags. If user enter 1 quantity then 1 bag is given. If you have discount that has condition, If user buy 3 bags you will give one bag free in that situation your case 3 may need to go into another case as here default. It can be anything depends on your requirement.
  • Example:Output:
    You bought three bags!
    You got one more bag free!!!
  • There is no compulsary particular order of case or default labels. It is good if you keep in order so will be easy to understand else you can see above program, how weird it look  
  • But break is needed in default case as it is needed in normal case, only in exceptional situation we may not need break as shown in above example but we can't say No break is needed in default case. Sometimes default need to have break.

  • Example of why default may need break:
    In above example, when user buy 3 bags then I want only case 3 and default(which is for offer) to execute. If you remove break after default then case 2 also gets executed so user gets 6 bags which I don't want.
    Output when you write default without break in above program:
    You bought three bags!
    You got one more bag free!!!
    You bought two bags
    Then user would be like  
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Eclipse IDE MySQL Database Tomcat Server Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
  • Forgot to tell you, you can have only one default in a switch i.e. non-nested switch but you can have more than one default in nested switch. Like below example
  • Since Java7, you can use a String object in the switch statement's expression. Like below

  • Hope you understood, still has doubts, always welcome
     
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Aleksandra Pestova,

    First of all, a warm welcome to CodeRanch!

    Aleksandra Pestova wrote:So how is it with the default case? (Just want to be sure.)


    The OCAJP8 study guide by Jeanne & Scott is spot-on, TutorialsPoint is wrong! A switch statement can have one (optional) default case and there is no particular requirement about the position/order of this case (or any other case). And the default case will behave exactly similar like any other case, meaning if you don't have a break statement at the end of the default case, you will have a fall-through (unless the default case appears at the end of the switch statement). Here is an illustrative code snippet of many fall-throughsOutput:
    number is 0
    number is 1 3 5 7 9 odd


    Hope it helps!
    Kind regards,
    Roel
     
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Notepad Java
    • Likes 3
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you very much for your help, Ganish and Roel!
    It helped me a lot.

    For better understanding I created a block diagram of the switch statement execution.
    Is it the way Java executes it?

    Here I assumed that:
    1. switch and case values are of the allowed type,
    2. switch and case values are of the same type.
    switchexe.JPG
    [Thumbnail for switchexe.JPG]
    switch execution
     
    Roel De Nijs
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Aleksandra Pestova wrote:For better understanding I created a block diagram of the switch statement execution.
    Is it the way Java executes it?


    Yes, that's exactly how a switch statement is executed in Java Have a cow for creating such a nice diagram!
     
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Notepad Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Wow, thanks Roel!  
     
    reply
      Bookmark Topic Watch Topic
    • New Topic