What is switch statement ?
Switch statement is used to allow the value of a variable to change the control flow of program execution.
How switch case statement works ?
When the variable is equal to one of the cases, the statements for that case is executed.
How switch case is used ?
1. Duplicate case values are not allowed.
2. The value of the variable and the value of the case should be of same data-type.
3. The value for a case must be a constant or a literal. Variables are not allowed.
Switch-Case :
Syntax :
switch (expression){
case value1 :
Statements...
break;
case value2 :
Statements...
break;
case value3 :
Statements...
break;
case value4 :
Statements...
break;
default :
Statements...
}
Break statement :
The break statement is used inside the switch to terminate a statement sequence, it is an optional statement.
Default statement :
The default statement is optional, and it must appear at the end of the switch.
1. For Integer values
It checks for each cases and if a particular case matches the values, it executes the statements under that case value.
Example 1 :
public class Eg1{
public static void main(String args[])
{
int a = 5;
switch(a)
{
case 1:
{
System.out.println("The case value is 1");
}
break;
case 2:
{
System.out.println("The case value is 2");
}
break;
case 3:
{
System.out.println("The case value is 3");
}
break;
case 4:
{
System.out.println("The case value is 4");
}
break;
case 5:
{
System.out.println("The case value is 5");
}
break;
case 6:
{
System.out.println("The case value is 6");
}
break;
default:
{
System.out.println("The case value is 1");
}
break;
}
}
}
Output :
The case value is 5
2. For Character values
The char value should be enclosed with single quotes ”.
Example 1 :
public class Eg1{
public static void main(String args[])
{
char a = 'E';
switch(a)
{
case 'A':
{
System.out.println("The case value is A");
}
break;
case 'B':
{
System.out.println("The case value is B");
}
break;
case 'C':
{
System.out.println("The case value is C");
}
break;
case 'D':
{
System.out.println("The case value is D");
}
break;
case 'E':
{
System.out.println("The case value is E");
}
break;
case 'F':
{
System.out.println("The case value is E");
}
break;
default:
{
System.out.println("The case value is not found");
}
break;
}
}
}
Output :
The case value is E
3. For String values
The String value should be enclosed within double quotes “”.
Example 1 :
public class Eg1{
public static void main(String args[])
{
String Name = "Abdul";
switch(Name)
{
case "Ramesh":
{
System.out.println("The case value is Ramesh");
}
break;
case "Suresh":
{
System.out.println("The case value is Suresh");
}
break;
case "Arun":
{
System.out.println("The case value is Arun");
}
break;
case "Balaji":
{
System.out.println("The case value is Balaji");
}
break;
case "Abdul":
{
System.out.println("The case value is Abdul");
}
break;
case "Kalam":
{
System.out.println("The case value is Kalam");
}
break;
default:
{
System.out.println("The case value is not found");
}
break;
}
}
}
Output :
The case value is Abdul
Nested Switch-Case
It is defined as switch-case statement executed inside switch-case statement.
Syntax :
switch (expression){
case value1 :
Statements...
break;
case value2 :
Statements...
break;
case value3 :
Statements...
switch (expression){
case valueA :
Statements...
break;
case valueB :
Statements...
break;
case valueC :
Statements...
break;
case valueD :
Statements...
break;
default :
Statements...
}
break;
case value4 :
Statements...
break;
default :
Statements...
}
Example 1 :
public class Eg1{
public static void main(String args[])
{
String Name = "Abdul";
int a = 4;
switch(Name)
{
case "Ramesh":
{
System.out.println("The case value is Ramesh");
}
break;
case "Suresh":
{
System.out.println("The case value is Suresh");
}
break;
case "Arun":
{
System.out.println("The case value is Arun");
}
break;
case "Balaji":
{
System.out.println("The case value is Balaji");
}
break;
case "Abdul":
{
System.out.println("The case value is Abdul");
switch(a)
{
case 1:
{
System.out.println("the value inside nested switch-case is 1");
}
break;
case 2:
{
System.out.println("the value inside nested sitch-case is 2")
}
break;
case 3:
{
System.out.println("the value inside nested sitch-case is 3");
}
break;
case 4:
{
System.out.println("the value inside nested sitch-case is 4");
}
break;
default:
{
System.out.println("default value for a = 0");
}
break;
}
}
break;
case "Kalam":
{
System.out.println("The case value is Kalam");
}
break;
default:
{
System.out.println("The case value is not found");
}
break;
}
}
}
Output :
The case value is Abdul
the value inside nested switch-case is 4