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,程式將列印“白天”。