0 / 8 页已完成

条件判断:if / elseConditionals: if / else

5.1 机器人也要"看情况"5.1 Robots Need to "Decide"

想象你的机器人正在场上跑,突然电池快没电了。你会希望它怎么做?Imagine your robot is running on the field and the battery is getting low. What would you want it to do?

这就是条件判断 — 让程序根据不同情况,做不同的事。This is a conditional — letting the program do different things based on different situations.

一句话理解In One Sentence

条件判断就是程序里的"如果...就..."。就像你过马路:如果是绿灯,走;否则,就等。A conditional is the "if...then..." of programming. It's like crossing the road: if the light is green, then walk; otherwise, wait.

5.2 最简单的 if5.2 The Simplest if

if 的意思是"如果条件成立,就执行大括号里的代码"。if means "if the condition is true, run the code inside the curly braces."

C++ (VEXcode)
int main() {
    int battery = Brain.Battery.capacity();

    if (battery < 20) {
        Brain.Screen.print("Warning: Low Battery!");
    }

    return 0;
}
C++ (VS Code + VEX)
int main() {
    int battery = Brain.Battery.capacity();

    if (battery < 20) {
        Brain.Screen.print("Warning: Low Battery!");
    }

    return 0;
}
C++ (PROS)
void opcontrol() {
    int battery = pros::battery::get_capacity();

    if (battery < 20) {
        pros::lcd::set_text(1, "Warning: Low Battery!");
    }
}

解读:Explanation:

5.3 if-else:二选一5.3 if-else: Choose One of Two

有时候不只是"做或不做",而是"做 A 还是做 B"。这时候用 elseSometimes it's not just "do or don't" — it's "do A or do B." That's when you use else.

C++ (VEXcode)
int main() {
    int battery = Brain.Battery.capacity();

    if (battery < 20) {
        Brain.Screen.print("Battery LOW!");
    } else {
        Brain.Screen.print("Battery OK!");
    }

    return 0;
}
C++ (VS Code + VEX)
int main() {
    int battery = Brain.Battery.capacity();

    if (battery < 20) {
        Brain.Screen.print("Battery LOW!");
    } else {
        Brain.Screen.print("Battery OK!");
    }

    return 0;
}
C++ (PROS)
void opcontrol() {
    int battery = pros::battery::get_capacity();

    if (battery < 20) {
        pros::lcd::set_text(1, "Battery LOW!");
    } else {
        pros::lcd::set_text(1, "Battery OK!");
    }
}

if-else 就是"二选一":条件成立走 if,不成立走 else。永远只走其中一个。if-else is "pick one of two": if the condition is true, run the if block; otherwise, run the else block. Only one ever runs.

5.4 多条件:if - else if - else5.4 Multiple Conditions: if - else if - else

如果情况不止两种呢?比如电量分三个等级:What if there are more than two cases? For example, battery with three levels:

C++ (VEXcode)
int main() {
    int battery = Brain.Battery.capacity();

    if (battery < 20) {
        Brain.Screen.print("DANGER! Almost dead!");
    } else if (battery < 50) {
        Brain.Screen.print("Battery getting low...");
    } else {
        Brain.Screen.print("Battery is good!");
    }

    return 0;
}
C++ (VS Code + VEX)
int main() {
    int battery = Brain.Battery.capacity();

    if (battery < 20) {
        Brain.Screen.print("DANGER! Almost dead!");
    } else if (battery < 50) {
        Brain.Screen.print("Battery getting low...");
    } else {
        Brain.Screen.print("Battery is good!");
    }

    return 0;
}
C++ (PROS)
void opcontrol() {
    int battery = pros::battery::get_capacity();

    if (battery < 20) {
        pros::lcd::set_text(1, "DANGER! Almost dead!");
    } else if (battery < 50) {
        pros::lcd::set_text(1, "Battery getting low...");
    } else {
        pros::lcd::set_text(1, "Battery is good!");
    }
}

规则很简单:从上往下检查,哪个条件先成立就执行哪个,后面的全跳过。The rule is simple: check from top to bottom. Whichever condition is true first gets executed, and the rest are skipped.

顺序很重要!Order Matters!

如果你把 battery < 50 放在前面,那电量 10% 的时候也会走 < 50 这个分支,而不会走 < 20。所以要先检查最严格的条件If you put battery < 50 first, then even at 10% battery it would match < 50 instead of < 20. So always check the strictest condition first.

5.5 比较运算符5.5 Comparison Operators

条件里要用到比较,这些是 C++ 的比较符号:Conditions use comparisons. Here are the C++ comparison operators:

符号Symbol 意思Meaning 例子Example
==等于Equal toscore == 100
!=不等于Not equal tocolor != "red"
<小于Less thanbattery < 20
>大于Greater thanspeed > 100
<=小于或等于Less than or equaltime <= 15
>=大于或等于Greater than or equalage >= 12
最常见的坑:= 和 == 的区别Most Common Mistake: = vs ==

=赋值(把右边的值存到左边)。==比较(看两边是不是相等)。= is assignment (stores the right value into the left). == is comparison (checks if both sides are equal).

if (x = 5) 不会报错,但意思完全不对!应该写 if (x == 5)Writing if (x = 5) won't cause an error, but it means something completely wrong! You should write if (x == 5).

5.6 逻辑运算符:组合条件5.6 Logical Operators: Combining Conditions

有时候一个条件不够,需要组合多个条件:Sometimes one condition isn't enough — you need to combine multiple conditions:

符号Symbol 意思Meaning 说明Description
&&而且(AND)AND两个条件成立才是 trueBoth conditions must be true
||或者(OR)OR任何一个成立就是 trueEither one being true is enough
!取反(NOT)NOTtrue 变 false,false 变 trueFlips true to false and false to true

例子:机器人只有在电量够而且没被禁用时才能跑:Example: The robot can only run when the battery is sufficient and it's not disabled:

C++
bool disabled = false;
int battery = 80;

if (battery > 20 && !disabled) {
    // 可以跑!
}

if (battery < 10 || disabled) {
    // 电量太低 或者 被禁用了,停下来
}

5.7 动手预测5.7 Predict the Output

看下面这段代码,先在脑子里想想屏幕会显示什么,再点开答案验证。Look at the code below. Think about it first — what will the screen show? Then click to check your answer.

C++
int score = 75;

if (score >= 90) {
    // 显示 "A"
} else if (score >= 80) {
    // 显示 "B"
} else if (score >= 60) {
    // 显示 "C"
} else {
    // 显示 "D"
}
预测输出Predict the Output

score 是 75,屏幕会显示什么?score is 75 — what will the screen show?

检查点Checkpoint
下面这段代码,x 是 15,屏幕会显示什么?In the code below, x is 15. What will the screen show?
int x = 15;
if (x > 20) {
    // 显示 "big"
} else if (x > 10) {
    // 显示 "medium"
} else {
    // 显示 "small"
}