Java Switch 语句Java Switch 语句使用switch语句从多个要执行的代码块中选择一个执行。 语法 switch(expression) { case x: // 代码块 break; case y: // 代码块 break; default: // 代码块 } 工作原理:switch 表达式计算一次 case 将表达式的值与每种情况的值进行比较 如果存在匹配项,则执行关联的代码块...
switch(expression){casevalue1:// code to be executed if expression equals value1;break;casevalue2:// code to be executed if expression equals value2;break;// you can have any number of case statements.default:// code to be executed if expression doesn't match any cases;} Java Copy The...
case 'D': return 3; case 'E': return 4; case 'F': return 5; case 'G': return 6; case 'H': return 7; case 'I': return 8; case 'J': return 9; case 'K': return 10; case 'L': return 11; case 'M': return 12; case 'N': return 13; case 'O': return 14; defaul...
The switch statement evaluates an expression. The value of the expression is then compared with the values of each case in the structure. If there is a match, the associated block of code is executed. The switch statement is often used together with a break or a default keyword (or both)...
When Java reaches abreakkeyword, it breaks out of the switch block. This will stop the execution of more code and case testing inside the block. When a match is found, and the job is done, it's time for a break. There is no need for more testing. ...
switch day { case 1: fmt.Println("Monday") case 2: fmt.Println("Tuesday") case 3: fmt.Println("Wednesday") case 4: fmt.Println("Thursday") case 5: fmt.Println("Friday") case 6: fmt.Println("Saturday") case 7: fmt.Println("Sunday") } } 结果: Thursday 亲自试一试 » default...
switch (expression) { case label1: //code block break; case label2: //code block; break; case label3: //code block break; default: //code block } This is how it works:The expression is evaluated once The value of the expression is compared with the values of each case If there ...
Theswitchstatement in Go is similar to the ones in C, C++, Java, JavaScript, and PHP. The difference is that it only runs the matched case so it does not need abreakstatement. Single-Case switch Syntax Syntax switchexpression{ casex: ...
case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!";} ?> Try it Yourself » Definiti...
case0: day ="Sunday"; break; case1: day ="Monday"; break; case2: day ="Tuesday"; break; case3: day ="Wednesday"; break; case4: day ="Thursday"; break; case5: day ="Friday"; break; case6: day ="Saturday"; } The result of day will be: ...