C# else 语句
else 语句
使用 else
语句指定如果条件为 False
时要执行的代码块。
语法
if (condition)
{
// block of code to be executed if the condition is True
}
else
{
// block of code to be executed if the condition is False
}
示例
int time = 20;
if (time < 18)
{
Console.WriteLine("Good day.");
}
else
{
Console.WriteLine("Good evening.");
}
// Outputs "Good evening."
示例说明
在上面的示例中,时间 (20) 大于 18,因此条件为 False
。因此,我们执行 else
条件,并在屏幕上打印“晚上好”。如果时间小于 18,程序将打印“白天”。