0Pricing
Mojo Academy · 课时

使用 while 循环

重复执行,直到条件发生变化

使用 while 循环 是 CoddyKit 上的免费 Mojo Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Mojo Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Mojo Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Repeat Until Done

Sometimes you must run code again and again. A while loop repeats its block as long as a condition stays true. 🔁

The while Shape

Write while, a condition, a colon, then an indented block. Mojo rechecks the condition before every pass through the body.

while count < 3:
    print(count)

Change the Condition Inside

The body must eventually make the condition false. Usually you update a counter so the loop progresses toward stopping.

var count = 0
while count < 3:
    print(count)
    count += 1

Watch for Infinite Loops

If the condition never turns false, the loop runs forever. An infinite loop usually means you forgot to update the variable it checks.

Condition Checked First

Mojo tests the condition before the first pass. If it is already false, the body never runs even once.

while False:
    print("never shown")

Accumulate a Result

Loops shine when building up a value. Keep a running total in a variable and add to it on each pass.

var total = 0
var i = 1
while i <= 5:
    total += i
    i += 1

Counting Down

A while loop can move in any direction. Start high and decrement each pass to count down toward a stopping point.

var n = 3
while n > 0:
    print(n)
    n -= 1

Loop on a Flag

The condition need not be a number. A boolean flag works too, letting other logic decide when the loop should end.

var running = True
while running:
    running = step()

Combine with if

Inside a while body you can branch with if. This lets each pass make its own decision based on the current state.

while i < 10:
    if i % 2 == 0:
        print(i)
    i += 1

Pick while When Count Is Unknown

Use while when you do not know in advance how many passes you need, such as reading until a value appears.

Update Before You Forget

A reliable habit: put the update step right where the loop must make progress, so the condition can truly change each pass.

Quick Check

You wrote a while loop but it runs forever and never stops. What is the most likely cause?

Recap

A while loop repeats while its condition is true, checking before each pass. Update the condition inside the body to avoid looping forever. 🎯

常见问题解答

「使用 while 循环」课时是免费的吗?

是的 — 「使用 while 循环」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Mojo Academy 课程的其余内容,请升级到 CoddyKit PRO。 Mojo Academy 课程共包含 4 节课。

「使用 while 循环」这节课中我会学到什么?

重复执行,直到条件发生变化 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Mojo Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Mojo Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「使用 while 循环」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Mojo Academy 课中编写并运行代码吗?

能。每节 Mojo Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 if/else 做选择
  2. 使用 while 循环
  3. 使用 for 和 range 迭代
  4. break、continue 与提前退出
← 返回 Mojo Academy