关于c#:分支语句C如何使用Switch语句用法示例

7次阅读

共计 3400 个字符,预计需要花费 9 分钟才能阅读完成。

在 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 statement
using 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 statement
using 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 statement
using 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 statement
using System;
  
public class GFG {
  
// Main Method
public 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 ;
         }
     }
}

输入如下:

Bonjour
Namaste
Entered 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
正文完
 0