在本文中,您将学习使用不同形式的 if..else 语句在 C++ 程序中创建决策语句。
在计算机编程中,仅当满足特定条件时,才使用if语句运行块代码。
例如,根据学生获得的分数分配成绩(A,B,C)。
- 如果百分比高于 90,则将等级分配为 A
- 如果百分比高于 75,则指定等级 B
- 如果百分比高于 65,请指定等级 C
C++ 中有if...else语句的三种形式。
if语句if...else语句if...else if...else语句
C++ if语句
if语句的语法为:
if (condition) {// body of if statement}
if语句求值括号( )内的condition。
- 如果
condition求值为true,则将执行if主体内部的代码。 - 如果
condition求值为false,则将跳过if主体内部的代码。
注意:{ }内的代码是if语句的主体。

C ++ if语句的原理
示例 1:C++ if语句
// Program to print positive number entered by the user// If the user enters a negative number, it is skipped#include <iostream>using namespace std;int main() {int number;cout << "Enter an integer: ";cin >> number;// checks if the number is positiveif (number > 0) {cout << "You entered a positive integer: " << number << endl;}cout << "This statement is always executed.";return 0;}
输出 1
Enter an integer: 5You entered a positive number: 5This statement is always executed.
当用户输入5时,条件number > 0被求值为true,并且执行了if主体内部的语句。
输出 2
Enter a number: -5This statement is always executed.
当用户输入-5时,条件number > 0被求值为false,并且if主体内部的语句不被执行。
C++ if...else
if语句可以具有可选的else子句。 其语法为:
if (condition) {// block of code if condition is true}else {// block of code if condition is false}
if..else语句求值括号内的condition。

C++ if...else的原理
如果condition求值为true,
if体内的代码被执行else正文中的代码从执行中跳过
如果condition求值为false,
else体内的代码被执行if正文中的代码从执行中跳过
示例 2:C++ if...else语句
// Program to check whether an integer is positive or negative// This program considers 0 as a positive number#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
Enter an integer: 4You entered a positive integer: 4.This line is always printed.
在上面的程序中,我们具有条件number >= 0。 如果我们输入大于或等于0的数字,则条件为true。
在这里,我们输入4。 因此,条件为true。 因此,将执行if主体内部的语句。
输出 2
Enter an integer: -4You entered a negative integer: -4.This line is always printed.
在这里,我们输入-4。 因此,条件为false。 因此,将执行else主体内部的语句。
C++ if...else if...else语句
if...else语句用于在两个替代方案之间执行代码块。 但是,如果需要在两个以上的选择之间进行选择,则可以使用if...else if...else语句。
if...else if...else语句的语法为:
if (condition1) {// code block 1} else if (condition2){// code block 2} else {// code block 3}
这里,
- 如果
condition1求值为true,则执行code block 1。 - 如果
condition1求值为false,则求值condition2。 - 如果
condition2为true,则执行code block 2。 - 如果
condition2为false,则执行code block 3。

if...else if...else语句如何工作
注意:可以有多个else if语句,但只有一个if和else语句。
示例 3:C++ if...else if...else
// Program to check whether an integer is positive, negative or zero#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
Enter an integer: 1You entered a positive integer: 1.This line is always printed.
输出 2
Enter an integer: -2You entered a negative integer: -2.This line is always printed.
输出 3
Enter an integer: 0You entered 0.This line is always printed.
在此程序中,我们从用户那里获取一个号码。 然后,我们使用if...else if...else梯形图检查数字是正数,负数还是零。
如果该数字大于0,则执行if块内的代码。 如果该数字小于0,则执行else if块内的代码。 否则,将执行else块中的代码。
C++ if...else嵌套
有时,我们需要在另一个if语句内使用if语句。 这称为嵌套if语句。
将其视为if语句的多层。 有第一个外部if语句,在内部是另一个内部if语句。 其语法为:
// outer if statementif (condition1) {// statements// inner if statementif (condition2) {// statements}}
注意:
- 我们可以根据需要将
else和else if语句添加到内部if语句中。 - 内部
if语句也可以插入外部else或else if语句(如果存在)中。 - 我们可以嵌套
if语句的多层。
示例 4:C++ 嵌套if
// C++ program to find if an integer is even or odd or neither (0)// using nested if statements#include <iostream>using namespace std;int main() {int num;cout << "Enter an integer: ";cin >> num;// outer if conditionif (num != 0) {// inner if conditionif ((num % 2) == 0) {cout << "The number is even." << endl;}// inner else conditionelse {cout << "The number is odd." << endl;}}// outer else conditionelse {cout << "The number is 0 and it is neither even nor odd." << endl;}cout << "This line is always printed." << endl;}
输出 1
Enter an integer: 34The number is even.This line is always printed.
输出 2
Enter an integer: 35The number is odd.This line is always printed.
输出 3
Enter an integer: 0The number is 0 and it is neither even nor odd.This line is always printed.
在上面的示例中,
- 我们将一个整数作为用户的输入,并将其存储在变量
num中。 然后,我们使用
if...else语句检查num是否不等于0。- 如果为
true,则执行内部if...else语句。 - 如果为
false,则执行外部else条件内部的代码,其显示The number is 0 and it is neither even nor odd.。
- 如果为
内部
if...else语句检查输入的数字是否可被2整除。- 如果为
true,那么我们将打印一条声明该数字为偶数的语句。 - 如果
false,我们打印该数字为奇数。
- 如果为
请注意,0也可被2整除,但实际上不是偶数。 这就是为什么我们首先要确保在if外部条件下输入的数字不是0。
注意:如您所见,嵌套的if...else使您的逻辑变得复杂。 如果可能,您应始终避免嵌套if...else。
只有一个语句的if...else的主体
如果if...else的主体只有一个语句,则可以在程序中省略{ }。 例如,您可以替换
int number = 5;if (number > 0) {cout << "The number is positive." << endl;}else {cout << "The number is negative." << endl;}
与
int number = 5;if (number > 0)cout << "The number is positive." << endl;elsecout << "The number is negative." << endl;
两个程序的输出将相同。
注意:尽管如果if...else的主体只有一个语句,则不必使用{ },但使用{ }可使代码更具可读性。
有关决策的更多信息
在某些情况下,三元运算符可以替换if...else语句。 要了解更多信息,请访问 C++ 三元运算符。
如果我们需要根据给定的测试条件在多个选项之间进行选择,则可以使用switch语句。 要了解更多信息,请访问 C++ 开关。
查看以下示例以了解更多信息:
