1

I have written small code in java 6

public class TestSwitch{ public static void main(String... args){ int a = 1; System.out.println("start"); switch(a){ case 1:{ System.out.println(1); case 3: System.out.println(3); case 4: System.out.println(4); } case 2:{ System.out.println(2); case 5: System.out.println(5); case 7: System.out.println(7); } } System.out.println("end"); } } 

Output: start 1 2 end

My editor is showing orphaned case for 'case 3' and 'case 5'.Still it is running and showing output.

  • Does Nastated cases like concept is there in Java?

  • And Why it is giving above output? rather i thought it will be 'start 1 end'

    Your response will be greatly appreciated!!

8 Answers 8

2

Switch replaces if else's but switch syntax != If else syntax.

You forgot to put break after each case.

So conditions under fall through.

Example:

case 0: mColor.setText("#000000"); break; 

You can find that in docs

The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

public static void main(String... args){ int a = 1; System.out.println("start"); switch(a){ case 1: System.out.println(1); break; case 2: System.out.println(2); break; case 3: System.out.println(3); break; case 4: System.out.println(4); break; case 5: System.out.println(5); break; case 7: System.out.println(7); break; default: System.out.println("nothing"); } 
Sign up to request clarification or add additional context in comments.

5 Comments

But a have value 1 ...still it will go in case 2? what about nasted case? Is it bad coding style OR no such case nasting in java?
@PrashantShilimkar It's it's highly kills readability. Why you are doing in such a way ? You can do it in simple switch right?
Yes u r right BUT This was just example.My intention is to know the all behaviors of switch case. Thank for edited answer.
@PrashantShilimkar What you wrote is wrong, I'm saying. For example. If a =1, Then all other cases will fail. it never ever satisfy any other case like case2. I hope you get it
Got it :) . . . Errors is a great Teacher in IT :D
2
switch(a){ case 1:{ System.out.println(1); case 3: 

You cannot nest cases like this. Switch should look either like :

 switch(a){ case 1: System.out.println(1); break; case 3: .... 

or like this :

switch(a){ case 1: System.out.println(1); switch(a) { case 3: //... break; case 5 : //... 

And if you don't add break at the end of a case, the execution will continue after. You should add a break at the end of each cases if they should be executed separately.

Comments

1

You have wrong closing braces before case 2. case 3,4 are interpreted as labels not cases.

Comments

1

Your code will give compilation errors as we can't use curly brace after case : Exact code is:

public static void main(String... args){ int a = 1; System.out.println("start"); switch(a){ case 1: System.out.println(1); case 3: System.out.println(3); case 4: System.out.println(4); case 2: System.out.println(2); case 5: System.out.println(5); case 7: System.out.println(7); } System.out.println("end"); } } 

and output will be start 1 3 4 2 5 7 end because you have not use "break" after each case.

2 Comments

i have run this code and given output...there is no error.I will like to know the working flow of above code :)
See if a=1 then your case 1 will work then 1 will pe printed if as we have not using break after case 1 so all cases are working in flow so output is coming like this if you want to execute only one case at one time then you have to put break after one case like
1

As their no break statement in case 1: the execution directly jumps to case 2: and ends up printing "start 1 2 end"..

Comments

1

You have not added break statement before case 2.

Refer this to know more about fall through.

Each break statement terminates the enclosing switch statement. Control flow continues with the first statement following the switch block. The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered. 

Comments

1
int a = 1; System.out.println("start"); switch (a) { case 1: { System.out.println(1); break; } case 3: { System.out.println(3); break; } case 4: { System.out.println(4); break; } case 2: { System.out.println(2); break; } case 5: { System.out.println(5); //no break will fall through and print 7 too } case 7: { System.out.println(7); break; } default:{ System.out.println("none"); } } 

Comments

0
 See if a=1 then your case 1 will work then 1 will pe printed if as we have not using break after case 1 so all cases are working in flow so output is coming like this if you want to execute only one case at one time then you have to put break after one case like switch(a){ case 1: System.out.println(1); break; case 3: System.out.println(3); break; case 4: System.out.println(4); break; 

Then it will break out of the switch case on encountering break statement

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.