C Else 语句
else 语句
使用 else 语句来指定当条件为 false 时要执行的代码块。
语法
if (condition) {
// 如果条件为真,则要执行的代码块
} else {
// 如果条件为假,则要执行的代码块
}
示例
int time = 20;
if (time < 18) {
printf("Good day.");
} else {
printf("Good evening.");
}
// Outputs "Good evening."
亲自试一试 »
示例详解
在上面的例子中,时间 (20) 大于 18,因此条件为 false。正因如此,我们进入 else 条件,并在屏幕上打印"Good evening"。如果时间小于 18,程序将打印"Good day"。

