C++ else 关键字
定义和用法
else 语句指定了一段代码,当 if 语句中的条件为假时执行该代码块。
C++ 有以下条件语句:
实例
例子 1
使用 else 语句指定一段代码,当条件为假时执行。
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// 输出 "Good evening."
例子 2
跳出循环:
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// 输出 "Good evening."
相关页面
教程:C++ 条件