C# 中的决策制定
csharpprogrammingserver side programming更新于 2024/11/24 5:24:00
决策制定结构要求程序员指定一个或多个要由程序评估或测试的条件,以及在确定条件为真时要执行的一个或多个语句,以及在确定条件为假时要执行的其他语句(可选)。
C# 中的决策制定包括 if 语句、if-else 语句、switch 语句等。
让我们看一个 C# 中 if 语句的示例。
示例
using System; namespace Demo { class Program { static void Main(string[] args) { int x = 5; // if 语句 if (x < 20) { /* 如果条件为真,则打印以下内容 */ Console.WriteLine("x 小于 20"); } Console.WriteLine("x 的值为 : {0}", x); Console.ReadLine(); } } }
输出
x 小于 20 x 的值为 : 5
让我们看一个 C# 中 if-else 语句的示例。在此,如果第一个条件为假,则将检查 else 语句中的条件。
示例
using System; namespace Demo { class Program { static void Main(string[] args) { int x = 100; /* 检查布尔条件 */ if (x < 20) { /* 如果条件为真,则打印以下内容 */ Console.WriteLine("x 小于 20"); } else { /* 如果条件为假,则打印以下内容 */ Cohttp://tpcg.io/HoaKexnsole.WriteLine("x 不小于 20"); } Console.WriteLine("a 的值为:{0}", x); Console.ReadLine(); } } }
输出
x 不小于 20 a 的值为:100