NET 中C#的switch语句的语法
来源:互联网 发布:steam mac中文游戏 编辑:程序博客网 时间:2024/05/21 20:47
NET 中C#的switch语句的语法
.NET 中C#的switch语句的语法如下(switch,case和default是关键字):
switch ( controllingExpression )
{
case constantExpression :
statements
break;
case constantExpression :
statements
break;
...
default :
statements
break;
}
controllingExpression(控制表达式)只求值一次。然后,从其constantExpression值等于controllingExpression值的case开始,它下方的所有语句都会一直运行,直到遇到一个break为止。随后,switch语句将结束,程序从switch结束大括号之后的第一个语句继续执行,并忽略其他case。
假如任何一个constantExpression值都不等于controllingExpression的值,就运行可选标签default之下的语句。
注意 假如controllingExpression的值和任何一个case标签都不匹配,同时没有发现一个default标签,程序会跳过整个switch语句,从它的结束大括号之后的第一个语句继续执行。
例如,前面的嵌套if语句可以改写为以下switch语句:
switch (day)
{
case 0 :
dayName = "Sunday";
break;
case 1 :
dayName = "Monday";
break;
case 2 :
dayName = "Tuesday";
break;
...
default :
dayName = "Unknown";
break;
}
遵守switch语句规则
switch语句非常有用,但在使用时必须谨慎。所写的任何switch语句都必须遵循以下规则:
只能针对基本数据类型使用switch,这些类型包括int和string等待。对于其他类型,则必须使用if语句。
case标签必须是常量表达式(constantExpression),如42或者"42"。如果需要在运行时计算case标签的值,必须使用if语句。
case标签必须是惟一性的表达式;也就是说,不允许两个case具有相同的值。
可以连续写下一系列case标签(中间不能间插额外的语句),从而指定自己希望在多种情况下都运行相同的语句。如果像这样写,那么最后一个case标签之后的代码将适用于所有case。然而,假如一个标签关联了一个或多个语句,又没有使用break来跳出,那么执行就不能贯穿到后续的标签,而且编译器会报告一个错误。例如:
switch (trumps)
{
case Hearts :
case Diamonds : // 允许贯穿 — 标签之间无额外代码
color = "Red"; // 针对Hearts和Diamonds这两种情况都会执行的代码
break;
case Clubs :
color = "Black";
case Spades : // 出错 — 标签之间有额外代码,而且没有使用break来跳出
color = "Black";
break;
}
注意 break语句是用来阻止贯穿的最常见的方式,但也可以用一个return语句或者一个throw语句来替代它。throw语句的详情将在以后讨论。
不准贯穿
由于C#存在不准贯穿(fall through)的规则,所以可以自由地安排一个switch语句的各个区域,而不至于影响其含义(其中包括default标签,它传统意义上是最后一个标签,但并非一定如此)。
C和C++程序员要注意的是,在使用C#编程时,必须为switch语句中的每个case(包括default case)都提供一个break语句。这个要求是一件好事情,C和C++程序员经常忘记添加break语句,造成执行自动贯穿到后续的标签,并造成很难发现的bug。
然而,如果你真的喜欢,也可以在C#中模拟C++的贯穿行为,方法是使用一个goto语句来转到下一个case或者default标签。但这种用法是不推荐的,本书也不打算介绍具体细节!
goto 语句将程序控制直接传递给标记语句。
goto 的一个通常用法是将控制传递给特定的 switch-case 标签或 switch 语句中的默认标签。
goto 语句还用于跳出深嵌套循环。
示例
下面的示例演示了 goto 在 switch 语句中的使用。
复制代码
// statements_goto_switch.cs
using System;
class SwitchTest
{
static void Main()
{
Console.WriteLine("Coffee sizes: 1=Small 2=Medium 3=Large");
Console.Write("Please enter your selection: ");
string s = Console.ReadLine();
int n = int.Parse(s);
int cost = 0;
switch (n)
{
case 1:
cost += 25;
break;
case 2:
cost += 25;
goto case 1;
case 3:
cost += 50;
goto case 1;
default:
Console.WriteLine("Invalid selection.");
break;
}
if (cost != 0)
{
Console.WriteLine("Please insert {0} cents.", cost);
}
Console.WriteLine("Thank you for your business.");
}
}
输入
2
示例输出
Coffee sizes: 1=Small 2=Medium 3=Large
Please enter your selection: 2
Please insert 50 cents.
Thank you for your business.
下面的示例演示了使用 goto 跳出嵌套循环。
复制代码
// statements_goto.cs
// Nested search loops
using System;
public class GotoTest1
{
static void Main()
{
int x = 200, y = 4;
int count = 0;
string[,] array = new string[x, y];
// Initialize the array:
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
array[i, j] = (++count).ToString();
// Read input:
Console.Write("Enter the number to search for: ");
// Input a string:
string myNumber = Console.ReadLine();
// Search:
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (array[i, j].Equals(myNumber))
{
goto Found;
}
}
}
Console.WriteLine("The number {0} was not found.", myNumber);
goto Finish;
Found:
Console.WriteLine("The number {0} is found.", myNumber);
Finish:
Console.WriteLine("End of search.");
}
}
输入
44
示例输出
Enter the number to search for: 44
The number 44 is found.
End of search.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumControl
{
public enum EnumControlStateValue
{
Oline = 0,
OneLol = 1,
Omote = 2
}
class Program
{
static void Main(string[] args)
{
int CtrlModeValue = Convert.ToInt32(Console.ReadLine());
switch (CtrlModeValue)
{
case (int)EnumControlStateValue.Oline:
//Console.WriteLine(";0");
//break;
case (int)EnumControlStateValue.OneLol:
Console.WriteLine(";1");
//break;
goto case (int)EnumControlStateValue.Omote;
case (int)EnumControlStateValue.Omote:
Console.WriteLine(";2");
return;//break;
default:
Console.WriteLine(";3");
break;//return;
}
Console.WriteLine("Bye~");
}
}
}
- NET 中C#的switch语句的语法
- C#的switch语句
- C#的switch语句
- C#的switch语句(2)
- C#中switch语句
- 关于C#中switch语句的一点注意
- 在SQL中如何实现类似C#的switch语句
- C#中一种替换switch语句更优雅的写法
- C#对switch语句的优化
- C#疑惑篇-变调的switch语句!
- C#对switch语句的优化
- javascript的switch语句与c#有所不同
- C# switch 语句的简单应用
- .net中switch的使用
- C#中switch语句注意
- C#中switch语句注意
- C 语言中switch语法的用处
- C 语言中switch语法的用处
- iOS中一些共存混编的问题
- 如何判断用户用哪种终端程序打开页面
- leetcode Increasing Triplet Subsequence
- Linux上根据进程名kill掉指定的进程
- delphi6网络编程 socket编程练习
- NET 中C#的switch语句的语法
- Python爬虫(一)——爬取糗事百科
- 核心层,汇聚层,接入层,交换机性能差别
- 哈理工OJ 1431 摞盘子【水题】
- AndroidStudio解决unspecifiedonprojectappresolvestoanAPKarchivewhichisnotsupported
- 原生App 与 移动Web App
- C#设计模式06-适配器模式
- android 自定义控件 使用declare-styleable进行配置属性(源码角度)
- 前端性能测试