C++ if...else 语句

一个 if 语句 后可跟一个可选的 else 语句 ,else 语句在布尔表达式为假时执行。

语法

C++ 中 if...else 语句的语法:

if (boolean_expression) {
   // 如果布尔表达式为真将执行的语句
} else {
   // 如果布尔表达式为假将执行的语句
}

如果布尔表达式为 true ,则执行 if 块内的代码。如果布尔表达式为 false ,则执行 else 块内的代码。

流程图

pic001.jpg

实例

#include <bits/stdc++.h>
using namespace std;
 
int main() {
   // 局部变量声明
   int a = 100;
 
   // 检查布尔条件
   if (a < 20) {
       // 如果条件为真,则输出下面的语句
       cout << "a 小于 20" << endl;
   } else {
       // 如果条件为假,则输出下面的语句
       cout << "a 大于 20" << endl;
   }
   cout << "a 的值是 " << a << endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

a 大于 20
a 的值是 100

if...else if...else 语句

一个 if 语句后可跟一个可选的 else if...else 语句,这可用于测试多种条件。

当使用 if...else if...else 语句时,以下几点需要注意:

语法

C++ 中的 if...else if...else 语句的语法:

if (boolean_expression 1) {
   // 当布尔表达式 1 为真时执行
} else if (boolean_expression 2) {
   // 当布尔表达式 2 为真时执行
} else if(boolean_expression 3) {
   // 当布尔表达式 3 为真时执行
} else {
   // 当上面条件都不为真时执行
}

实例

#include <bits/stdc++.h>
using namespace std;
 
int main() {
   // 局部变量声明
   int a = 100;
 
   // 检查布尔条件
   if (a == 10) {
       // 如果 if 条件为真,则输出下面的语句
       cout << "a 的值是 10" << endl;
   } else if (a == 20) {
       // 如果 else if 条件为真,则输出下面的语句
       cout << "a 的值是 20" << endl;
   } else if (a == 30) {
       // 如果 else if 条件为真,则输出下面的语句
       cout << "a 的值是 30" << endl;
   } else {
       // 如果上面条件都不为真,则输出下面的语句
       cout << "没有匹配的值" << endl;
   }
   cout << "a 的准确值是 " << a << endl;
 
   return 0;
}

当上面的代码被编译和执行时,它会产生下列结果:

没有匹配的值
a 的准确值是 100