Java Switch Keyword
In this article we are going to learn the concept of a java switch keyword.
Generally, java case keyword is used with the switch statements or keyword.Switch keyword is implemented in conditional labeling by the support of the switch statements.
The block of code which contains a label of several cases on which it is runned or executed only when the given value to the switch matches with the provided cases?
To execute the label of the case which is branched with the switch statement the code of the program should pass the arguments in the switch case.There is no limitation for the case block, as it doesn’t have any internal ending point.The switch statement can provide with an multiple case labels.
Each label can hold the various values like integers or strings or statements.The block of code can also contain the break statements which help in terminating the flow of execution from that block of switch statements.
NOTE: The values of Boolean type, float type, double type cannot be hold by the case label.
Syntax of switch keyword:
switch(block) {
case number1:
//block of code or statements.
break; //optional
case value2:
//block of code that should be executed.
break; //optional
default:
The default code is executed if any of the cases are not matched or executed.
}
Examples of Java switch keyword:
Example 1: Printing the string values using the switch label.
public class SwitchEx1 {
public static void main (String [] s) {
String No._of_weeks_in_month="Four”.
switch (No._of_weeks_in_month)
{
case "Four": System.out.println("Four");
}
}
}
Output of the program is:
Four
Example 2: Using multiple case labels in the java program.
public class SwitchEx2 {
public static void main (String [] args) {
String Scales="SA";
switch (Scales)
{
case "SA":
System.out.println("SA").
break.
case "RE":
System.out.println("RE").
break.
case "GA":
System.out.println("GA").
break.
case "MA":
System.out.println("MA").
break;
case "PA":
System.out.println("PA");
break;
case "DA":
System.out.println("DA");
break.
case "NE":
System.out.println("NE");
break.
default:
System.out.println("nothing printed");
}
}
}
Output:
SA
Example 3: printing the value of int using the switch label
public class SwitchEx3 {
public static void main (String [] args) {
int age=50;
switch(age)
{
case 5:
System.out.println("The age is 5");
break;
case 35:
System.out.println("The age is 35");
break;
case 50:
System.out.println("The age is 50");
break;
default:
System.out.println("No age found");
}
}
}
Output:
The age is 50.
Example 4: case label holding switch statement
public class switchEx4 {
public static void main (String [] args) {
String clg_name="bvrit”.
int dept_id=189.
switch(clg_name)
{
case "bvrit":
System.out.println("bvrit");
switch(dept_id)
{
case 185:
System.out.println("information technology departement ");
break;
case 186:
System.out.println("Computer science Department");
break;
case 187:
System.out.println("Civil Department");
break;
case 188:
System.out.println("mechanical Department");
break;
case 189:
System.out.println("electronics Department");
break;
case 190:
System.out.println("Electrical Department");
break;
}
break;
case "JNTU":
System.out.println("JNTU");
switch(dept_id)
{
case 101:
System.out.println("Mech Department");
break;
case 102:
System.out.println("Computer science Department");
break;
case 103:
System.out.println("Civil Department");
break;
}
break;
case "Lpu":
System.out.println("Lpu");
switch(dept_id)
{
case 115:
System.out.println("Mec Department");
break;
case 118:
System.out.println("Computer science Department");
break;
case 201:
System.out.println("Civil Department");
break;
}
break;
default :
System.out.println("Try once again");
}
}
}
Output:
Lpu
Computer science Department
Example 5: Calculate the weekday name using switch label:
int day = 3;
switch (day of the week) {
case 1:
System.out.println("Mon");
break;
case 2:
System.out.println("Tue");
break;
case 3:
System.out.println("Wed");
break;
case 4:
System.out.println("Thur");
break;
case 5:
System.out.println("Fri");
break;
case 6:
System.out.println("Sat");
break;
case 7:
System.out.println("Sun");
break;
}
Output:
" Wed " (day 3)
Example 6: Nested switch keyword Example:
to Demonstrate
// Switch Case Statement using the nested
// Class
public class nested {
public static void main (String [] args)
{
// input value
String Group = "IT";
int semester = 1;
// Switch case
switch (semester)
case 1:
// Switch inside a switch
// Nested Switch
switch (Group) {
// Nested case
case "IT":
case "ECE":
System.out.println(
"Elective courses: Electronics, Mtlab");
break.
// Case
case "EEE":
System.out.println(
"Elective courses: Antenna Engineering");
break.
// default case
// It will execute if above cases does not
// execute
default:
// Print statement
System.out.println(
"Elective courses: answering or optimization");
}
}
}
}
Output:
elective courses: Electronics, Mtlab.