Java中的switch语句
switch 语句是多路分支语句。简而言之, Java switch 语句从多个条件执行一个语句。它就像一个 if-else-if 阶梯语句。它提供了一种简单的方法,可以根据表达式的值将执行分派到代码的不同部分。基本上,表达式可以是 byte、short、char 和 int 原始数据类型。它基本上测试变量与多个值的相等性。
Note: Java switch expression must be of byte, short, int, long(with its Wrapper type), enums and string. Beginning with JDK7, it also works with enumerated types (Enums in java), the String class, and Wrapper classes.
Switch 语句的一些重要规则
- 可能有任意数量的案例只是强加条件检查,但请记住不允许重复的案例/秒值。
- case 的值必须与 switch 中的变量具有相同的数据类型。
- case 的值必须是常量或字面量量。不允许使用变量。
- break 语句用于在 switch 内部终止语句序列。
- break 语句是可选的。如果省略,则执行将继续到下一个案例。
- 默认语句是可选的,可以出现在 switch 块内的任何位置。 in case,如果不在末尾,则必须在 default 语句之后保留一个 break 语句,以省略下一个 case 语句的执行。
Switch-Case 语句流程图
语法:开关盒
// switch statement
switch(expression)
{
// case statements
// values must be of same type of expression
case value1 :
// Statements
break; // break is optional
case value2 :
// Statements
break; // break is optional
// We can have any number of case statements
// below is default statement, used when none of the cases is true.
// No break is needed in the default case.
default :
// Statements
}
Note: Java switch statement is a fall through statement that means it executes all statements if break keyword is not used, so it is highly essential to use break keyword inside each case.
示例:考虑以下Java程序,它声明了一个名为 day 的 int,其值表示一天(1-7)。该代码使用 switch 语句根据当天的值显示当天的名称。
Java
// Java program to Demonstrate Switch Case
// with Primitive(int) Data Type
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
int day = 5;
String dayString;
// Switch statement with int data type
switch (day) {
// Case
case 1:
dayString = "Monday";
break;
// Case
case 2:
dayString = "Tuesday";
break;
// Case
case 3:
dayString = "Wednesday";
break;
// Case
case 4:
dayString = "Thursday";
break;
// Case
case 5:
dayString = "Friday";
break;
// Case
case 6:
dayString = "Saturday";
break;
// Case
case 7:
dayString = "Sunday";
break;
// Default case
default:
dayString = "Invalid day";
}
System.out.println(dayString);
}
}
Java
// Java Program to Demonstrate Switch Case
// with Multiple Cases Without Break Statements
// Class
public class GFG {
// main driver method
public static void main(String[] args)
{
int day = 2;
String dayType;
String dayString;
// Switch case
switch (day) {
// Case
case 1:
dayString = "Monday";
break;
// Case
case 2:
dayString = "Tuesday";
break;
// Case
case 3:
dayString = "Wednesday";
break;
case 4:
dayString = "Thursday";
break;
case 5:
dayString = "Friday";
break;
case 6:
dayString = "Saturday";
break;
case 7:
dayString = "Sunday";
break;
default:
dayString = "Invalid day";
}
switch (day) {
// Multiple cases without break statements
case 1:
case 2:
case 3:
case 4:
case 5:
dayType = "Weekday";
break;
case 6:
case 7:
dayType = "Weekend";
break;
default:
dayType = "Invalid daytype";
}
System.out.println(dayString + " is a " + dayType);
}
}
Java
// Java Program to Demonstrate
// Nested Switch Case Statement
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input string
String Branch = "CSE";
int year = 2;
// Switch case
switch (year) {
// Case
case 1:
System.out.println(
"elective courses : Advance english, Algebra");
// Break statement to hault execution here
// itself if case is matched
break;
// Case
case 2:
// Switch inside a switch
// Nested Switch
switch (Branch) {
// Nested case
case "CSE":
case "CCE":
System.out.println(
"elective courses : Machine Learning, Big Data");
break;
// Case
case "ECE":
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 : Optimization");
}
}
}
}
Java
// Java Program to Illustrate Use of Enum
// in Switch Statement
// Class
public class GFG {
// Enum
public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
// Main driver method
public static void main(String args[])
{
// Enum
Day[] DayNow = Day.values();
// Iterating using for each loop
for (Day Now : DayNow) {
// Switch case
switch (Now) {
// Case 1
case Sun:
System.out.println("Sunday");
// break statement that hault further
// execution once case is satisfied
break;
// Case 2
case Mon:
System.out.println("Monday");
break;
// Case 3
case Tue:
System.out.println("Tuesday");
break;
// Case 4
case Wed:
System.out.println("Wednesday");
break;
// Case 5
case Thu:
System.out.println("Thursday");
break;
// Case 6
case Fri:
System.out.println("Friday");
break;
// Case 7
case Sat:
System.out.println("Saturday");
break;
}
}
}
}
Friday
省略 break 语句
break 语句是可选的。如果我们省略了中断,执行将继续到下一个案例。有时需要多个 case 之间没有 break 语句。例如,让我们考虑上述程序的更新版本,它还显示一天是工作日还是周末。
例子:
Java
// Java Program to Demonstrate Switch Case
// with Multiple Cases Without Break Statements
// Class
public class GFG {
// main driver method
public static void main(String[] args)
{
int day = 2;
String dayType;
String dayString;
// Switch case
switch (day) {
// Case
case 1:
dayString = "Monday";
break;
// Case
case 2:
dayString = "Tuesday";
break;
// Case
case 3:
dayString = "Wednesday";
break;
case 4:
dayString = "Thursday";
break;
case 5:
dayString = "Friday";
break;
case 6:
dayString = "Saturday";
break;
case 7:
dayString = "Sunday";
break;
default:
dayString = "Invalid day";
}
switch (day) {
// Multiple cases without break statements
case 1:
case 2:
case 3:
case 4:
case 5:
dayType = "Weekday";
break;
case 6:
case 7:
dayType = "Weekend";
break;
default:
dayType = "Invalid daytype";
}
System.out.println(dayString + " is a " + dayType);
}
}
Tuesday is a Weekday
嵌套的 Switch Case 语句
我们可以使用 switch 作为外部 switch 语句序列的一部分。这称为嵌套开关。由于 switch 语句定义了自己的块,因此内部 switch 中的 case 常量与外部 switch 中的 case 常量之间不会发生冲突。
例子:
Java
// Java Program to Demonstrate
// Nested Switch Case Statement
// Class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input string
String Branch = "CSE";
int year = 2;
// Switch case
switch (year) {
// Case
case 1:
System.out.println(
"elective courses : Advance english, Algebra");
// Break statement to hault execution here
// itself if case is matched
break;
// Case
case 2:
// Switch inside a switch
// Nested Switch
switch (Branch) {
// Nested case
case "CSE":
case "CCE":
System.out.println(
"elective courses : Machine Learning, Big Data");
break;
// Case
case "ECE":
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 : Optimization");
}
}
}
}
elective courses : Machine Learning, Big Data
例子:
Java
// Java Program to Illustrate Use of Enum
// in Switch Statement
// Class
public class GFG {
// Enum
public enum Day { Sun, Mon, Tue, Wed, Thu, Fri, Sat }
// Main driver method
public static void main(String args[])
{
// Enum
Day[] DayNow = Day.values();
// Iterating using for each loop
for (Day Now : DayNow) {
// Switch case
switch (Now) {
// Case 1
case Sun:
System.out.println("Sunday");
// break statement that hault further
// execution once case is satisfied
break;
// Case 2
case Mon:
System.out.println("Monday");
break;
// Case 3
case Tue:
System.out.println("Tuesday");
break;
// Case 4
case Wed:
System.out.println("Wednesday");
break;
// Case 5
case Thu:
System.out.println("Thursday");
break;
// Case 6
case Fri:
System.out.println("Friday");
break;
// Case 7
case Sat:
System.out.println("Saturday");
break;
}
}
}
}
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday