Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Nested Switch Statement Fall Through

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Nested Switch Statement Fall Through

    Hello everyone, I'm attempting to create a simple choose your own adventure game using nested switch statements. The problem is that whenever I reach "the end" (death, etc.) the switch statements fall through, despite having "break;". This is an extremely simplified version of my code, just to show the issue. Press 1. to start the game, then 2. This will cause it to fall through to the alternate menu options.
    import java.util.Scanner;
     
    class ProblemExample {
     
    public static void main(String[] args) {
     
    Scanner scan = new Scanner (System.in);
     
    System.out.println("Welcome to The Game,");
    System.out.println("by **********");
    System.out.println("1.	Begin game");
    System.out.println("2.	Help");
    System.out.println("3.	Quit");
     
    int inputStartMenu = scan.nextInt();
     
    	switch (inputStartMenu) {case 1:  System.out.println("case 1 of switch(inputstartmenu)");
     
    					int inputA = scan.nextInt();
     
    						switch (inputA) {case 1: System.out.println("case 1 of switch(inputA)"); break;
    								case 2: System.out.println("case 2 of switch(inputA)"); break;}[B][U] break;[/U][/B]
     
    				case 2:	System.out.println("Instructions/Summary"); break;
     
    				case 3: System.out.println("Goodbye"); break;}
     
    }
    }
    Last edited by Nate; July 19th, 2012 at 12:29 PM. Reason: Edited code to have better formatting and show my solution (bold and underlined).


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Nested Switch Statement Fall Through

    Quote Originally Posted by Nate View Post
    ... fall through ...
    Maybe the path that the program takes would be more evident if you formatted your code to show the nested switch statements more clearly. Throw in some print statements that make the program tell you where it is at strategic points. Maybe even a couple of comments.

        public static void main(String[] args)
        {
     
            Scanner scan = new Scanner (System.in);
     
            System.out.println("Welcome to The Quarter Adventure,");
            System.out.println("by **********");
            System.out.println("1.  Begin game");
            System.out.println("2.  Help");
            System.out.println("3.  Quit");
            int inputStartMenu = scan.nextInt();
     
            System.out.println("inputStartMenu = " + inputStartMenu);
     
            switch (inputStartMenu)
            {
                case 1:
                    System.out.print("Case 1 of switch(inputStartMenu): Enter an integer (1 or 2): ");
                    int inputA = scan.nextInt();
     
                    System.out.println("inputA = " + inputA);
     
                    switch (inputA)
                    {
                        case 1:
                            System.out.println("F");
                            break; // Break out of switch(inputA)
                        case 2:
                            System.out.println("G");
                            break; // Break out of switch(inputA)
                    } // End of switch(InputA) in case1 of switch(inputStartMenu)
     
                case 2:
                    System.out.println("Case 2 of switch(inputStartMenu): Instructions abbreviated for debug purposes");
                    break; // Break out of switch(inpuStartMenu)
     
                case 3:
                    System.out.println("Case 3 of switch(inputStartMenu): Goodbye");
                    break; // Break out of switch(inpuStartMenu)
            } // End of switch(inputStartMenu)
        } // End of main()


    Cheers!


    Z
    Last edited by Zaphod_b; July 18th, 2012 at 10:14 PM.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Switch Statement Fall Through

    Thanks for the response. Sorry about the formatting, I swear it was formatted before. I did some fiddling with the code and got it to work by adding another break statement after the switch statement. Your comments saying "breaks out of switch" made me think of trying this. Just to be sure though, is this just a coincidence in my program, or does the break; actually break out of that switch statement (not how I thought it worked before)?

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Nested Switch Statement Fall Through

    For nested switch statements a "break" from the innermost statement only breaks out of that one. Same as for breaking out of any inner block of nested loops or nested combinations of loops and switches: Breaks out of that one, outer ones continue from that point.

    That is what I hoped would be illustrated by the print statements that I suggested.

    Quote Originally Posted by Nate
    ...coincidence...
    Hmmm...let's see:

    1. Didn't work.
    2. Made change according to rules of Java.
    3. Worked.

    Could it be a coincidence? Really?


    Cheers!

    Z
    Last edited by Zaphod_b; July 19th, 2012 at 12:38 PM.

  5. The Following User Says Thank You to Zaphod_b For This Useful Post:

    Nate (July 19th, 2012)

  6. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Switch Statement Fall Through

    Hah. You illustrated that point fairly well, I just didn't understand how the "break" worked, but now I do so its all good. As for the coincidence part, I couldn't think of a better way to describe it in a short amount of time, so I just used something that I figured would get my point across. (And I didn't know that those were the rules of java).

  7. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Nested Switch Statement Fall Through

    Quote Originally Posted by Nate View Post
    ...And I didn't know that those were the rules of java...
    The problem with interacting like this is that you couldn't see my tongues in my cheeks when I wrote that enumeration. I hope you didn't think I was disparaging you.

    Now...

    Even if you study all of the "Official" language reference material and tutorials, you might not see anything specifically about this. (Or you might; I don't have a ready "official" reference for a place that it does.)

    For example in the Oracle Tutorial:
    "Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block."

    See? It doesn't specifically say anything about nested switch statements, but you have to take it exactly as written: A "break" statement in a particular switch statement breaks out of that particular switch.

    I mean, you might search (and search and search) the Internet for examples, and maybe you can find something specific so that you wouldn't have to ask. Or, maybe you could find lots of stuff that didn't apply to your situation or didn't even work at all.

    [/begin Parenthetical SideNote]
    A few years ago my muse told me that she probably wouldn't be using the Internet very much because, as she so delicately put it, "Eight-five percent of the stuff on the Internet is crap." I said, "Well, 85% of everything is crap, so what's your point?"
    [/end Parenthetical SideNote]


    Anyhow...

    That's why it is so very useful for you to ask questions about things that puzzle you. I doubt that you will ever know how many folks you have helped by asking it here. (There are unbelievable numbers of lurkers who never have enough courage to post their own questions because they are too shy, or maybe they are afraid that someone will put them down or make fun of them or something.)

    My philosophy: It's always OK to ask. Really.


    Cheers!

    Z

  8. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Nested Switch Statement Fall Through

    Yea, that really is the problem with the internet, you can only judge people based on the words they write, not the way they say it, which made me think you were being entirely serious. Again, thanks for all the help!

Similar Threads

  1. Nested loop if statement help
    By CSUTD in forum Loops & Control Statements
    Replies: 7
    Last Post: November 8th, 2011, 02:05 PM
  2. Help simplifying huge nested if statement?
    By racecar333 in forum Loops & Control Statements
    Replies: 6
    Last Post: November 1st, 2011, 01:03 AM
  3. switch statement
    By Tank314 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 9th, 2011, 01:23 PM
  4. help with switch statement
    By robertsbd in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 12th, 2010, 12:52 PM
  5. Nested If Else Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 6th, 2010, 02:46 PM