C++ else 关键字
示例
使用 else 语句来指定当条件为 false 时要执行的代码块。
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."
定义和用法
else 语句指定当 if 语句中的条件为 false 时要执行的代码块。
C++ 包含以下条件语句:
- 使用
if指定当指定条件为真时要执行的代码块 - 使用
else指定当相同条件为假时要执行的代码块 - 使用
else if指定当第一个条件为假时要测试的新条件 - 使用
switch指定要执行的多个备选代码块
更多示例
示例
跳出 while 循环:
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
// Outputs "Good evening."
相关页面
请阅读我们的C++ 条件语句教程,了解更多关于条件语句的内容。

