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 5 of 5

Thread: Switch case problem

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Switch case problem

    Hi,

    Please check the following program, below program output is Exit 10, but i didnt get why it is like this? can you please explain?

    I traced the the program upto print Exit, after Exit again case 10: is there to execute but swich case dont go back to case 10 i think. So default will execute na? I have a confusion can you please clarify. If no case match then only default case will execute, already case 10 executed previously, then why default case is executing please clarify.


    public class SwitchProg1 {

    public static void main(String[] args) {
    int a=10;
    switch(a++){
    case 10:
    switch(a--){
    default:
    System.out.print("Exit ");
    case 10:
    }
    default:
    System.out.println(a);
    }
    }
    }


    Thank you


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Switch case problem

    You need a break; statement at the end of every switch case, or you get something called "fall-through". Basically, the switch cases continue to evaluate until you are broken out of the switch statement. Default will ALWAYS execute, unless you are broken out of the switch statement before it is reached.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Switch case problem

    public class SwitchProg1 {
     
    public static void main(String[] args) {
    	int a = 10;
    	switch(a++){
    		case 10:
    			switch(a--){
    				default:
    					System.out.print("Exit ");
    				case 10:
    			}
    			break;  // <----- Ends or "breaks" the switch statement so no other cases will execute
    		default:
    			System.out.println(a);
    		}
    	}
    }

  4. #4
    Member Andrew R's Avatar
    Join Date
    Jul 2013
    Location
    San Pedro Sula, Honduras
    Posts
    58
    My Mood
    Inspired
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Switch case problem

    Thanks Alex.

    Well I've never seen this before
    switch(a++)
    i am not sure what you are trying to achieve. But I guess the switch is to evaluate variable a. You can have a++ or a-- after the case.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Switch case problem

    It is simply using post increment. That is the switch will evaluate based upon the variables current value and then increment it. It saves a line of code by not doing the increment after the switch statement.
    Exactly the same if you used it in an if statement.
    int x = 42;
    System.out.println(x++);
    The value printed out will be 42 but afterwards the x variable will hold the value 43.
    Improving the world one idiot at a time!

  6. The Following User Says Thank You to Junky For This Useful Post:

    Andrew R (August 11th, 2013)

Similar Threads

  1. switch case
    By viyyapu harsha in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 24th, 2013, 12:03 AM
  2. Need help with switch case
    By niko25 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 18th, 2012, 12:10 AM
  3. Switch Case Problem help
    By Kikiam in forum What's Wrong With My Code?
    Replies: 13
    Last Post: March 18th, 2012, 06:11 PM
  4. Case Switch Help?
    By xionyus in forum What's Wrong With My Code?
    Replies: 17
    Last Post: October 19th, 2011, 08:59 PM
  5. Ranging Case Values in Switch Construct
    By WhiteSoup12 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 6th, 2011, 11:50 AM