c#语言swith的用法,分支语句:C#如何使用Switch语句?用法示例

本文详细介绍了C#中的Switch语句,对比了其与if-else语句的使用场景,强调了Switch在处理多种条件下的优势,包括节省代码和提高可读性。通过实例演示了如何使用Switch、goto和default的情况。
摘要由CSDN通过智能技术生成

在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执行流程图:

bVcQRD9

要记住的要点:在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#相关的内容:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值