• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

switch statement, question about default

 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • 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.

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • 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 Java Notepad
    • Likes 3
    • Mark post as helpful
    • send pies
    • 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: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • 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 Java Notepad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow, thanks Roel! 
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic