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