Switch Statement in Java
Table of Contents
Switch Statement in Java
Switch Statement Java
The switch statement in Java is employed to execute a single statement based on a number of conditions. It resembles an if-then-else ladder. A switch statement has a default case as well as conditional-based cases. The expression in a switch statement can be of the byte, short, char, or int type. Enum and String can also be used in switch cases as of JDK-7.
Following are some of the rules while using the switch statement in Java:
- There can be one or N numbers of cases.
- Each statement of the case can have a break statement. It is optional.
- The values in the case must be unique.
Syntax:
Following is the syntax to declare the switch case in Java.
switch(expression)
{
case value1:
//code for execution;
break; //optional
case value2:
// code for execution
break; //optional
......
......
......
......
Case value n:
// code for execution
break; //optional
default:
code for execution when none of the case is true;
}
Data Flow Diagram Of switch Block
Example: Using integer value
So We are using an int type value to match cases in this example. So In this example, the numeric value determines the return day.
public class SwitchDemo1{
public static void main(String[] args)
{
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Today is Monday";
break;
case 2:
dayName = "Today is Tuesday";
break;
case 3:
dayName = "Today is Wednesday";
break;
case 4:
dayName = "Today is Thursday";
break;
case 5:
dayName = "Today is Friday";
break;
case 6:
dayName = "Today is Saturday";
break;
case 7:
dayName = "Today is Sunday";
break;
default:
dayName = "Invalid day";
break;
}
System.out.println(dayName);
}
}
Example using Enum in Switch statement
As previously mentioned, Java permits the use of Enum in switch cases. To use its components in switch case, we are creating an Enum of vowel alphabets.
public class SwitchDemo2{
public enumvowel{a, e, i, o, u}
public static void main(String args[])
{
vowel[] character= vowel.values();
for (vowel Now : character)
{
switch (Now)
{
case a:
System.out.println("'a' is a Vowel");
break;
case e:
System.out.println("'e' is a Vowel");
break;
case i:
System.out.println("'i' is a Vowel");
break;
case o:
System.out.println("'o' is a Vowel");
break;
case u:
System.out.println("'u' is a Vowel");
break;
default:
System.out.println("It is a consonant");
}
}
}
}
Example: String in switch case
Since Java has allowed to use string values in switch cases, so we are using string to create a string based switch case example.
public static void main(String[] args) {
String name = "Mango";
switch(name){
case "Mango":
System.out.println("It is a fruit");
break;
case "Tomato":
System.out.println("It is a vegitable");
break;
case "Coke":
System.out.println("It is cold drink");
}
}
}
It is a fruit
Example: without break switch case
The program’s current execution can be interrupted using the break statement. Break is use in switch cases to end switch case execution and hand off control to the outside world. The switch case does not require the use of break. Let’s therefore examine what transpires if we don’t take the break.
public class Demo{
public static void main(String[] args) {
String name = "Mango";
switch(name){
case "Mango":
System.out.println("It is a fruit");
case "Tomato":
System.out.println("It is a vegitable");
case "Coke":
System.out.println("It is cold drink");
}
}
}
It is a fruit It is a vegitable It is cold drink
See, if we don’t use break, it executes all the cases after matching case.