跳转到内容

C++ if Else

来自代码酷

C++ If Else[编辑 | 编辑源代码]

If Else 是 C++ 中用于条件判断的基本控制流结构之一,它允许程序根据不同的条件执行不同的代码块。本篇文章将详细介绍其语法、使用方式以及实际应用场景。

基本介绍[编辑 | 编辑源代码]

在编程中,经常需要根据不同的条件执行不同的操作。例如,判断一个数字是正数还是负数,或者根据用户的输入执行不同的逻辑。C++ 提供了 ifelse ifelse 关键字来实现这种条件分支逻辑。

语法结构[编辑 | 编辑源代码]

C++ 中的 If Else 语句有以下几种形式:

1. 单条件判断(只有 if):

if (condition) {
    // 当 condition 为 true 时执行的代码
}

2. 双条件判断(if + else):

if (condition) {
    // 当 condition 为 true 时执行的代码
} else {
    // 当 condition 为 false 时执行的代码
}

3. 多条件判断(if + else if + else):

if (condition1) {
    // 当 condition1 为 true 时执行的代码
} else if (condition2) {
    // 当 condition2 为 true 时执行的代码
} else {
    // 当所有条件均为 false 时执行的代码
}

代码示例[编辑 | 编辑源代码]

示例 1:判断数字的正负[编辑 | 编辑源代码]

#include <iostream>
using namespace std;

int main() {
    int num = -5;

    if (num > 0) {
        cout << "The number is positive." << endl;
    } else if (num < 0) {
        cout << "The number is negative." << endl;
    } else {
        cout << "The number is zero." << endl;
    }

    return 0;
}

输出:

The number is negative.

解释: - 程序检查变量 `num` 的值。 - 如果 `num > 0`,输出 "The number is positive."。 - 如果 `num < 0`,输出 "The number is negative."。 - 否则(即 `num == 0`),输出 "The number is zero."。

示例 2:判断成绩等级[编辑 | 编辑源代码]

#include <iostream>
using namespace std;

int main() {
    int score = 85;

    if (score >= 90) {
        cout << "Grade: A" << endl;
    } else if (score >= 80) {
        cout << "Grade: B" << endl;
    } else if (score >= 70) {
        cout << "Grade: C" << endl;
    } else if (score >= 60) {
        cout << "Grade: D" << endl;
    } else {
        cout << "Grade: F" << endl;
    }

    return 0;
}

输出:

Grade: B

解释: - 程序根据 `score` 的值输出对应的成绩等级。 - 由于 `85` 满足 `score >= 80` 但不满足 `score >= 90`,因此输出 "Grade: B"。

嵌套 If Else[编辑 | 编辑源代码]

If Else 语句可以嵌套使用,即在某个条件分支内部再包含另一个 If Else 结构。

示例 3:嵌套 If Else[编辑 | 编辑源代码]

#include <iostream>
using namespace std;

int main() {
    int num = 15;

    if (num > 0) {
        if (num % 2 == 0) {
            cout << "The number is positive and even." << endl;
        } else {
            cout << "The number is positive and odd." << endl;
        }
    } else {
        cout << "The number is not positive." << endl;
    }

    return 0;
}

输出:

The number is positive and odd.

解释: - 外层 If Else 判断 `num` 是否为正数。 - 如果是正数,内层 If Else 进一步判断它是偶数还是奇数。 - 由于 `15` 是正数且为奇数,因此输出 "The number is positive and odd."。

实际应用场景[编辑 | 编辑源代码]

If Else 在编程中应用广泛,例如: 1. **用户输入验证**:检查用户输入是否符合要求。 2. **游戏逻辑**:根据玩家的行为执行不同的游戏逻辑。 3. **算法决策**:在排序或搜索算法中,根据条件选择不同的处理方式。

示例 4:用户登录验证[编辑 | 编辑源代码]

#include <iostream>
#include <string>
using namespace std;

int main() {
    string username = "admin";
    string password = "12345";

    string inputUser, inputPass;
    cout << "Enter username: ";
    cin >> inputUser;
    cout << "Enter password: ";
    cin >> inputPass;

    if (inputUser == username && inputPass == password) {
        cout << "Login successful!" << endl;
    } else {
        cout << "Invalid username or password." << endl;
    }

    return 0;
}

输出(正确输入时):

Enter username: admin
Enter password: 12345
Login successful!

输出(错误输入时):

Enter username: user
Enter password: 00000
Invalid username or password.

解释: - 程序检查用户输入的用户名和密码是否与预设值匹配。 - 如果匹配,输出 "Login successful!",否则输出 "Invalid username or password."。

流程图[编辑 | 编辑源代码]

以下是一个简单的 If Else 逻辑流程图:

graph TD A[Start] --> B{condition} B -- true --> C[Execute if block] B -- false --> D[Execute else block] C --> E[End] D --> E

数学表达式[编辑 | 编辑源代码]

在某些情况下,If Else 可以用于实现数学条件逻辑。例如,分段函数的计算:

f(x)={x2if x0xif x<0

对应的 C++ 代码:

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double x = -3.5;
    double result;

    if (x >= 0) {
        result = pow(x, 2);
    } else {
        result = -x;
    }

    cout << "f(" << x << ") = " << result << endl;
    return 0;
}

输出:

f(-3.5) = 3.5

总结[编辑 | 编辑源代码]

- If Else 是 C++ 中用于条件分支的核心结构。 - 可以单独使用 if,也可以结合 else ifelse 实现多条件判断。 - 支持嵌套使用,适用于复杂的逻辑判断。 - 广泛应用于输入验证、游戏逻辑、算法决策等场景。

通过合理使用 If Else,可以编写出更加灵活和强大的程序逻辑。