break、continue、早期終了
ループを正確に制御します
「break、continue、早期終了」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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:
breakA 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:
breakSkip 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 += nThey 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():
breakOnly 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:
breakEarly 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 FalseGuard 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、早期終了」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。
「break、continue、早期終了」で何を学びますか?
ループを正確に制御します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Mojo Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「break、continue、早期終了」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMojo Academyレッスンでコードを書いて実行できますか?
はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- if/elseによる条件分岐
- whileによるループ
- forとrangeによる反復
- break、continue、早期終了