0Pricing
Mojo Academy · 课时

break、continue 与提前退出

精确控制循环

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

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

Steer a Loop Precisely

Sometimes you must leave a loop early or skip a single pass. Mojo gives you break and continue for exactly that control. 🎛️

Stop Early with break

The break statement exits the loop immediately. No further passes run, and execution jumps to the code after the loop.

for i in range(10):
    if i == 3:
        break

A Classic Search

break shines when searching: scan until you find the target, then stop so you do not waste passes on the rest.

for name in names:
    if name == target:
        break

Skip a Pass with continue

The continue statement skips the rest of the current pass and jumps straight to the next iteration of the loop.

for i in range(5):
    if i == 2:
        continue
    print(i)

break vs continue

Keep them straight: break leaves the loop entirely, while continue only ends the current pass and keeps looping.

Filter While Looping

Use continue to filter. Skip the values you do not care about and let only the interesting ones reach the rest of the body.

for n in nums:
    if n < 0:
        continue
    total += n

They Work in while Too

break and continue are not just for for loops. They behave the same inside a while loop, controlling it the same way.

while True:
    if done():
        break

Only the Inner Loop

In nested loops, break and continue affect just the innermost loop they sit in. Outer loops keep running normally.

for row in grid:
    for cell in row:
        if cell == 0:
            break

Early Exit from a Function

For leaving a whole function early, use return. It ends the function on the spot and hands back a value to the caller.

fn find(x: Int) -> Bool:
    if x < 0:
        return False

Guard Clauses Read Well

Returning early on bad input, a guard clause, keeps the happy path clean and avoids deep nesting in your functions.

fn process(n: Int):
    if n == 0:
        return
    work(n)

Use Sparingly, Clearly

break, continue, and return are powerful but can hide flow. Reach for them when they make intent clearer, not just to save a line.

Quick Check

Inside a loop you hit a value you want to ignore, but you still want the loop to keep going. Which statement fits?

Recap

break leaves a loop, continue skips one pass, and return exits a function early. Use each to make your flow clearer. 🎯

常见问题解答

「break、continue 与提前退出」课时是免费的吗?

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

「break、continue 与提前退出」这节课中我会学到什么?

精确控制循环 你通过在浏览器中直接运行的动手代码来练习 Mojo Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Mojo Academy 需要有经验吗?

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

「break、continue 与提前退出」课时需要多长时间?

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

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

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

此课程中的所有课时

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