掌握 C++ 条件语句:if、if-else、嵌套 if 与多重分支全解析
C++ 条件语句完全指南
通过实例掌握 C++ if、if-else、if-else if-else 及嵌套条件语句,构建清晰的决策逻辑。
程序设计中,if...else 语句根据条件执行不同代码块。例如按分数分配等级:
- 百分比 > 90 → A
- 百分比 > 75 → B
- 百分比 > 65 → C
C++ 有三种 if...else 形式:
if语句if...else语句if...else if...else语句
if 语句
语法:
if (condition) {
// 条件为真时执行
}
计算 condition:
true→ 执行大括号内代码false→ 跳过大括号内代码
{ } 内为 if 语句体。
示例 1:if 语句
// 输入正数则打印,负数则跳过
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
输出 1(输入 5)
Enter an integer: 5 You entered a positive integer: 5 This statement is always executed.
输出 2(输入 -5)
Enter an integer: -5 This statement is always executed.
if...else 语句
可选 else 分支:
if (condition) {
// 条件真
} else {
// 条件假
}

- 条件真 → 执行
if块,跳过else块 - 条件假 → 执行
else块,跳过if块
示例 2:if...else
// 判断正负(0 视为正)
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
} else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}
输出 1(输入 4)
Enter an integer: 4 You entered a positive integer: 4 This line is always printed.
输出 2(输入 -4)
Enter an integer: -4 You entered a negative integer: -4 This line is always printed.
if...else if...else 多重分支
两个以上分支选择时使用:
if (condition1) {
// 代码块 1
} else if (condition2) {
// 代码块 2
} else {
// 代码块 3
}
执行逻辑:
condition1真 → 执行代码块 1condition1假 → 判断condition2condition2真 → 执行代码块 2condition2假 → 执行代码块 3

else if,但只能有一个 if 和一个 else。示例 3:正负零三分支
#include <iostream>
using namespace std;
int main() {
int number;
cout << "Enter an integer: ";
cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
} else if (number < 0) {
cout << "You entered a negative integer: " << number << endl;
} else {
cout << "You entered 0." << endl;
}
cout << "This line is always printed.";
return 0;
}
输出 1(1)
Enter an integer: 1 You entered a positive integer: 1 This line is always printed.
输出 2(-2)
Enter an integer: -2 You entered a negative integer: -2 This line is always printed.
输出 3(0)
Enter an integer: 0 You entered 0. This line is always printed.
嵌套 if...else
在 if 内部再写 if,形成多层决策:
if (condition1) {
// 外层语句
if (condition2) {
// 内层语句
}
}
注意:
- 内层可加
else/else if - 内层也可置于外层
else或else if中 - 可嵌套多层,但建议尽量扁平化
示例 4:嵌套 if 判断正负零
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter an integer: ";
cin >> num;
// 外层 if
if (num != 0) {
// 内层 if
if (num > 0) {
cout << "The number is positive." << endl;
} else {
cout << "The number is negative." << endl;
}
} else {
cout << "The number is 0 and it is neither positive nor negative." << endl;
}
cout << "This line is always printed." << endl;
return 0;
}
输出 1(35)
Enter an integer: 35 The number is positive. This line is always printed.
输出 2(-35)
Enter an integer: -35 The number is negative. This line is always printed.
输出 3(0)
Enter an integer: 0 The number is 0 and it is neither positive nor negative. This line is always printed.
if...else 会使逻辑复杂化。能用 else if 链或卫语句(早期返回)扁平化时,优先扁平化。单语句体可省略大括号
if...else 体仅一条语句时,可省略 { }:
int number = 5;
if (number > 0)
cout << "The number is positive." << endl;
else
cout << "The number is negative." << endl;
相关决策结构
- 三元运算符 (
?:):简单二选一场景可替代if...else。详见 C++ 三元运算符。 - switch 语句:基于单一表达式的多值分支,比长
else if链更清晰。详见 C++ switch 语句。
练习推荐
- 判断奇偶数
- 判断字符是元音/辅音
- 三数求最大值
C语言
- 掌握 C# 条件逻辑:if、if-else、if-else if 与嵌套 if 语句完全指南
- C++ while 与 do-while 循环:重复控制流完全掌握
- 掌握 C++ break 语句:跳出循环与 switch 的核心控制流
- C++类与对象:面向对象编程实战指南
- C++ 动态内存管理:new 与 delete 专家级实战指南
- C++ 三种继承方式深度解析:public、protected 与 private
- C++ 友元函数与友元类:突破访问控制的利器
- C++日期时间完全指南:ctime库、tm结构体与格式化输出
- C++重载机制深度解析:函数重载与运算符重载全攻略
- C++文件I/O完全指南:fstream读写、打开模式、定位指针与完整实例
- Python语句、缩进与注释:清晰指南
- 精通 Java 嵌套类与内部类:类型、示例与最佳实践