0 / 8 页已完成

循环:whileLoops: while

6.1 为什么需要循环?6.1 Why Do We Need Loops?

想想你操控机器人的过程:Think about how you control a robot:

  1. 读摇杆的值Read the joystick value
  2. 根据摇杆值控制电机Control the motors based on the joystick
  3. 回到第 1 步,再读一次...Go back to step 1 and read again...

这个过程要一直重复,从比赛开始到比赛结束。你总不能把同样的代码复制一万遍吧?This process needs to keep repeating from the start of the match to the end. You can't copy-paste the same code ten thousand times, right?

这就是循环的用途 — 让一段代码反复执行。That's what loops are for — making a block of code run over and over.

一句话理解In One Sentence

循环就是"重复做一件事,直到某个条件不满足为止"。A loop is "repeating something until a condition is no longer true."

6.2 while 的基本写法6.2 Basic while Syntax

C++
while (条件) {
    // 条件成立时,反复执行这里的代码
}

执行流程:How it works:

  1. 检查条件 → 成立?进入大括号执行代码Check the condition → True? Enter the braces and run the code
  2. 执行完 → 回到第 1 步,再检查条件When done → Go back to step 1 and check the condition again
  3. 条件不成立? → 跳出循环,继续往下走Condition false? → Exit the loop and continue with the rest of the code

6.3 while(true) — 永远不停6.3 while(true) — Never Stop

true 的意思是"永远成立",所以 while(true) 就是无限循环 — 永远不会停。true means "always true," so while(true) is an infinite loop — it never stops.

这在机器人程序里非常常见!遥控器操控模式就是一个无限循环:This is extremely common in robot programs! Driver control mode is an infinite loop:

C++ (VEXcode)
int main() {
    while (true) {
        // 读摇杆,控制电机
        // ...每次循环都执行

        wait(20, msec);  // 休息 20 毫秒
    }
}
C++ (VS Code + VEX)
int main() {
    while (true) {
        // 读摇杆,控制电机
        // ...每次循环都执行

        wait(20, msec);  // 休息 20 毫秒
    }
}
C++ (PROS)
void opcontrol() {
    while (true) {
        // 读摇杆,控制电机
        // ...每次循环都执行

        pros::delay(20);  // 休息 20 毫秒
    }
}
循环里必须有 wait / delay!Loops Must Have wait / delay!

如果循环里不加等待,程序会以极快的速度疯狂转圈,占满 CPU 资源,导致程序卡死Without a delay, the loop runs at maximum speed, hogging all CPU resources and causing the program to freeze.

加一个 wait(20, msec)pros::delay(20),让程序每次循环休息一下。20 毫秒 = 每秒循环 50 次,对机器人来说完全够了。Add a wait(20, msec) or pros::delay(20) to let the program rest each loop. 20 milliseconds = 50 loops per second, which is plenty for a robot.

6.4 有条件的循环6.4 Conditional Loops

while(true) 是永远不停。但有时候你想"做 5 次就停"或"倒计时到 0 就停"。这时候用一个变量来控制:while(true) never stops. But sometimes you want "do it 5 times and stop" or "count down to 0 and stop." Use a variable to control it:

C++
int count = 0;

while (count < 5) {
    // 这里的代码会执行 5 次
    count++;  // count 加 1
}

执行过程:How it runs:

6.5 实战:屏幕倒计时6.5 Practice: Screen Countdown

让 Brain 屏幕显示 5、4、3、2、1 的倒计时:Make the Brain screen show a countdown from 5, 4, 3, 2, 1:

C++ (VEXcode)
int main() {
    int countdown = 5;

    while (countdown > 0) {
        Brain.Screen.clearScreen();
        Brain.Screen.setCursor(1, 1);
        Brain.Screen.print("Countdown: %d", countdown);

        wait(1000, msec);  // 等 1 秒
        countdown--;        // 减 1
    }

    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("GO!");

    return 0;
}
C++ (VS Code + VEX)
int main() {
    int countdown = 5;

    while (countdown > 0) {
        Brain.Screen.clearScreen();
        Brain.Screen.setCursor(1, 1);
        Brain.Screen.print("Countdown: %d", countdown);

        wait(1000, msec);  // 等 1 秒
        countdown--;        // 减 1
    }

    Brain.Screen.clearScreen();
    Brain.Screen.setCursor(1, 1);
    Brain.Screen.print("GO!");

    return 0;
}
C++ (PROS)
void opcontrol() {
    int countdown = 5;

    while (countdown > 0) {
        pros::lcd::clear();
        pros::lcd::set_text(1, "Countdown: " + std::to_string(countdown));

        pros::delay(1000);  // 等 1 秒
        countdown--;         // 减 1
    }

    pros::lcd::clear();
    pros::lcd::set_text(1, "GO!");
}

6.6 动手预测6.6 Predict the Output

看下面这段代码,先想想 sum 最后等于多少:Look at the code below and think about what sum equals at the end:

C++
int sum = 0;
int i = 1;

while (i <= 3) {
    sum = sum + i;
    i++;
}
预测输出Predict the Output

循环结束后,sum 等于多少?After the loop ends, what does sum equal?

检查点Checkpoint
下面这个循环执行几次?How many times does this loop run?
int n = 10;
while (n > 7) {
    n--;
}