C# 中的决策语句是什么?
csharpprogrammingserver side programming更新于 2025/9/26 19:37:17
决策语句要求程序员指定程序要评估或测试的一个或多个条件,以及在条件确定为真时要执行的一个或多个语句,以及(可选)在条件确定为假时要执行的其他语句。
以下是决策语句的类型 −
| Sr.No | 语句 &说明 |
|---|---|
| 1 | if 语句 if 语句由一个布尔表达式后跟一个或多个语句组成。 |
| 2 | if...else 语句 if 语句后面可以跟一个可选的 else 语句,该语句在布尔表达式为假时执行。 |
| 3 | 嵌套 if 语句 您可以在一个 if 或 else if 语句中使用另一个 if 或 else if 语句。 |
| 4 | switch 语句 switch 语句允许测试变量与值列表的相等性。 |
| 5 | 嵌套 switch 语句 您可以在一个 switch 语句中使用另一个 switch 语句。 |
让我们看一个 C# 中 if-else 决策语句的示例。
示例
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int a = 50;
if (a < 10) {
Console.WriteLine("a is less than 10");
} else {
Console.WriteLine("a is not less than 10");
}
Console.WriteLine("a = {0}", a);
Console.ReadLine();
}
}
}
输出
a is not less than 10 a = 50

