The control statement that allows us to make a decision from the number of choices is called a switch, or more correctly a switch-case-default.
The Switch executes the case where a match is found and then all subsequent cases till end, so stop that fall through we use break; statement in each case.
A switch works with the byte, short, char, and int primitive data types.
It also works with enumerated types , the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer.
switch(expression){
case value1 :
statement(s);
break; //optional
case value2 :
statement(s);
break; //optional
.....
/* you can have any number of case statements. */
default : //Optional
statement(s);
/* code to be executed if all cases are not matched */
}
Example:
Java Program to give choice to user from menu to perform action.import java.util.Scanner;
class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int x;
System.out.println("MENU\n----------");
System.out.println("1. for Hello");
System.out.println("2. for Bye");
System.out.println("-----------");
System.out.print("Enter Your Choice: ");
x = sc.nextInt();
switch (x) {
case 1:
System.out.println("HELLO USER!!");
break;
case 2:
System.out.println("BYE USER!!");
break;
default:
System.out.println("INVALID!!");
}
}
}
OUTPUT 1:
MENU
-----------
1. for Hello
2. for Bye
-----------
Enter Your Choice: 1
HELLO USER!!
From Java SE 7 , you can use a String object in the switch statement's expression. The following code example, StringSwitchDemo, displays the number of the month based on the value of the String named month:
public class StringSwitchDemo {
public static void main(String[] args) {
String month = "august";
switch (month) {
case "january":
System.out.print("1st Month");
break;
case "february":
System.out.print("2nd Month");
break;
case "march":
System.out.print("3rd Month");
break;
case "april":
System.out.print("4th Month");
break;
case "may":
System.out.print("5th Month");
break;
case "june":
System.out.print("6th Month");
break;
case "july":
System.out.print("7th Month");
break;
case "august":
System.out.print("8th Month");
break;
case "september":
System.out.print("9th Month");
break;
case "october":
System.out.print("10th Month");
break;
case "november":
System.out.print("11th Month");
break;
case "december":
System.out.print("12th Month");
break;
default:
System.out.print("wrong Month");
break;
}//switch closed
}//main closed
}//class closed
OUTPUT :
8th Month
A nested switch statement is a switch statement that appears inside another switch statement's case block. It allows for more complex decision-making scenarios by evaluating multiple levels of conditions.
Example :
public class NestedSwitchExample {
public static void main(String[] args) {
int category = 2;
int item = 1;
switch (category) {
case 1:
switch (item) {
case 1:
System.out.println("Category: 1, Item: 1");
break;
case 2:
System.out.println("Category: 1, Item: 2");
break;
default:
System.out.println("Invalid Item for Category 1");
break;
}
break;
case 2:
switch (item) {
case 1:
System.out.println("Category: 2, Item: 1");
break;
case 2:
System.out.println("Category: 2, Item: 2");
break;
default:
System.out.println("Invalid Item for Category 2");
break;
}
break;
default:
System.out.println("Invalid Category");
break;
}
}
}
Topic: Introduction | Language: Java