Switch case Control flow statement doubt | Core Java Forum
I
Indhuja Posted on 08/04/2019

In the switch case example below:

There is no break statement;
The output will be : 
A
B
C

But the input we gave is A.

So isnt it basd on the input we give that the case is chosen by the compiler?

So why does it execute all the statements?

 

class example
{
public static void main (String args[])
{
char val='a';
func(val);
} 

public static void func(char value)
{
case 'a':
System.out.println("A");
case 'b':
System.out.println("B");
case 'c':
System.out.println("C");
}
}

 


Y
Yogesh Chawla Replied on 08/04/2019

Firstly, switch keyword was not there in the code so adding this, the code will look like this:

public class Example {
public static void main(String args[]) {
char val = 'a';
func(val);
}

public static void func(char value) {
switch (value) {
case 'a':
System.out.println("A");
case 'b':
System.out.println("B");
case 'c':
System.out.println("C");
}
}
}


Then when we pass 'A' as an input so ofcourse case 'A' should only execute and other cases should not but since break keyword is not used with all cases,
that's the reasons, all other cases are also getting executed so the output coming is 'A B C'

 

And when we use break keyword properly with cases like in the code below and give 'A' as an input so then only case 'A' will execute and
produce only 'A' as an output

public class Example {
public static void main(String args[]) {
char val = 'a';
func(val);
}

public static void func(char value) {
switch (value) {
case 'a':
System.out.println("A");
break;
case 'b':
System.out.println("B");
break;
case 'c':
System.out.println("C");
break;
}
}
}

Remember:
When the switch statement executes, it compares the value of the controlling expression to the values of each
case label. The program will select the value of the case label that equals the value of the controlling
expression and branch down that path to the end of the code block. If none of the case label values match,
then none of the codes within the switch statement code block will be executed. Java includes a default label
to use in cases where there are no matches. We can have a nested switch within a case block of an outer switch.


Y
Yogesh Chawla Replied on 08/04/2019

Firstly, switch keyword was not there in the code so adding this, the code will look like this:

public class Example {
	public static void main(String args[]) {
		char val = 'a';
		func(val);
	}

	public static void func(char value) {
		switch (value) {
		case 'a':
			System.out.println("A");
		case 'b':
			System.out.println("B");
		case 'c':
			System.out.println("C");
		}
	}
}


Then when we pass 'A' as an input so ofcourse case 'A' should only execute and other cases should not but since break keyword is not used with all cases, 
that's the reasons, all other cases are also getting executed so the output coming is 'A B C'



And when we use break keyword properly with cases like in the code below and give 'A' as an input so then only case 'A' will execute and 
produce only 'A' as an output

public class Example {
	public static void main(String args[]) {
		char val = 'a';
		func(val);
	}

	public static void func(char value) {
		switch (value) {
		case 'a':
			System.out.println("A");
			break;
		case 'b':
			System.out.println("B");
			break;
		case 'c':
			System.out.println("C");
			break;
		}
	}
}

Remember:
When the switch statement executes, it compares the value of the controlling expression to the values of each 
case label. The program will select the value of the case label that equals the value of the controlling 
expression and branch down that path to the end of the code block. If none of the case label values match, 
then none of the codes within the switch statement code block will be executed. Java includes a default label 
to use in cases where there are no matches. We can have a nested switch within a case block of an outer switch.