在C#中, Switch语句是多路分支语句。它提供了一种无效的形式, 能够依据表达式的值将执行转移到代码的不同局部。开关表达式是整数类型, 例如int, char, byte或short, 或者是枚举类型, 或者是字符串类型。查看表达式的不同状况, 而后执行一次匹配。
语法如下:
switch (expression) {case value1: // statement sequence break;case value2: // statement sequence break;...case valueN: // statement sequence break;default: // default statement sequence}
switch执行流程图:
要记住的要点:
- 在C#中, 不容许反复的大小写值。
- 开关中变量的数据类型和案例的值必须是同一类型。
- 案例的值必须是常量或文字。不容许应用变量。
- break in switch语句用于终止以后序列。
- 默认语句是可选的, 它能够在switch语句中的任何地位应用。
- 不容许应用多个默认语句。
例子:
// C# program to illustrate// switch case statementusing System; public class GFG { // Main Method public static void Main(String[] args) { int nitem = 5; switch (nitem) { case 1: Console.WriteLine( "case 1" ); break ; case 5: Console.WriteLine( "case 5" ); break ; case 9: Console.WriteLine( "case 9" ); break ; default : Console.WriteLine( "No match found" ); break ; } }}
输入如下:
case 5
为什么咱们应用Switch语句而不是if-else语句?
咱们应用switch语句而不是if-else语句, 因为if-else语句仅实用于值的大量逻辑求值。如果对更多可能的条件应用if-else语句, 则将破费更多工夫来编写并且也变得难以浏览。
例子:应用if-else-if语句
// C# program to illustrate// if-else statementusing System; class GFG { // Main Method public static void Main(String[] args) { // taking two strings value string topic; string category; // taking topic name topic = "Inheritance" ; // using compare function of string class if ((String.Compare(topic, "Introduction to C#" ) == 0) || (String.Compare(topic, "Variables" ) == 0) || (String.Compare(topic, "Data Types" ) == 0)) { category = "Basic" ; } // using compare function of string class else if ((String.Compare(topic, "Loops" ) == 0) || (String.Compare(topic, "If Statements" ) == 0) || (String.Compare(topic, "Jump Statements" ) == 0)) { category = "Control Flow" ; } // using compare function of string class else if ((String.Compare(topic, "Class & Object" ) == 0) || (String.Compare(topic, "Inheritance" ) == 0) || (String.Compare(topic, "Constructors" ) == 0)) { category = "OOPS Concept" ; } else { category = "Not Mentioned" ; } System.Console.Write( "Category is " + category); }}
输入如下:
Category is OOPS Concept
阐明:如下面的程序所示, 代码并不过分, 然而看起来很简单, 破费了更多的工夫来编写。因而, 咱们应用switch语句来节省时间并编写优化的代码。应用switch语句将提供更好的代码可读性。
例子:应用switch语句
// C# program to illustrate// switch statementusing System; class GFG { // Main Method public static void Main(String[] args) { // taking two strings value string topic; string category; // taking topic name topic = "Inheritance" ; // using switch Statement switch (topic) { case "Introduction to C#" : case "Variables" : case "Data Types" : category = "Basic" ; break ; case "Loops" : case "If Statements" : case "Jump Statements" : category = "Control Flow" ; break ; case "Class & Object" : case "Inheritance" : case "Constructors" : category = "OOPS Concept" ; break ; // default case default : category = "Not Mentioned" ; break ; } System.Console.Write( "Category is " + category); }}
输入如下:
Category is OOPS Concept
在Switch语句中应用goto
你也能够应用去语句代替switch语句中的break。通常, 咱们应用break语句从switch语句退出。然而在某些状况下, 须要执行默认语句, 因而咱们应用goto语句。它容许在switch语句中执行默认条件。 goto语句还用于跳转到C#程序中的标记地位。
例子:
// C# program to illustrate the// use of goto in switch statementusing System; public class GFG { // Main Methodpublic static void Main(String[] args) { int greeting = 2; switch (greeting) { case 1: Console.WriteLine( "Hello" ); goto default ; case 2: Console.WriteLine( "Bonjour" ); goto case 3; case 3: Console.WriteLine( "Namaste" ); goto default ; default : Console.WriteLine( "Entered value is: " + greeting); break ; } }}
输入如下:
BonjourNamasteEntered value is: 2
阐明:在下面的程序中, goto语句在switch语句中应用。首先是案例2, 即你好因为开关蕴含greeting的值为2而被打印, 而后因为goto语句的存在, 管制转移到状况3, 所以它打印Namaste, 最初将管制转移到默认条件并打印Entered值为:2 。
留神:如果你的switch语句是循环的一部分, 则也能够应用continue代替break语句中的break语句, 那么continue语句将使执行立刻返回到循环的开始。
更多C#开发相干内容请参考:lsbin - IT开发技术:https://www.lsbin.com/
查看以下更多C#相干的内容:
- C#匿名办法:https://www.lsbin.com/3226.html
- C# HashSet用法:https://www.lsbin.com/3133.html
- C#办法重载:https://www.lsbin.com/474.html