Iteration: WHILE and REPEAT
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · このページには最新のブラウザ(SharedArrayBuffer対応)が必要です。Chrome、Edge、Firefox、Safariを最新バージョンに更新してください。
English
WHILE tests before each pass
- A
WHILEloop checks its test before each pass. - If the test is false at the start, the body runs zero times.
- The block ends at
ENDWHILE.
日本語
WHILE は各反復前にテストする
WHILEループは各反復の 前に テストを確認します。- 最初からテストが偽であれば、本体は 0回 実行されません。
- ブロックは
ENDWHILEで終了します。
Count ← 1
WHILE Count <= 3 DO
OUTPUT Count // 1, 2, 3
Count ← Count + 1
ENDWHILE
English
REPEAT tests after each pass
- A
REPEATloop checks its test after each pass. - So the body always runs at least once.
- It ends at
UNTIL.
日本語
REPEAT は各反復後にテストする
REPEATループは各反復の 後に テストを確認します。- そのため、本体コードは少なくとも一度は実行されます。
UNTILで終了します。
N ← 0
REPEAT
N ← N + 2
OUTPUT N // 2, 4, 6
UNTIL N >= 6
Explore · 探索
REPEAT runs, then tests
WHILE tests before (may run 0 times); REPEAT tests after (runs at least once).
English
Watch out
- Something inside the loop must move the test toward stopping, or it runs forever.
WHILEmay run 0 times;REPEATalways runs at least 1 time.
日本語
注意
- ループ内部でテストが停止方向に進むよう変化させない限り、無限に実行されます。
WHILEは 0回 実行される可能性がありますが、REPEATは必ず 少なくとも1回 実行されます。
English
Common mistakes
WHILEtests the condition first;REPEAT ... UNTILtests after, so it runs at least once.- Change something inside, or the loop never ends.
日本語
よくあるミス
WHILEは条件を最初にテストし、REPEAT ... UNTILは後にテストするため、少なくとも一度は実行されます。- 内部の何かを変えると、ループが終了しません。
English
Now you try
- Pick
WHILEorREPEATfor each task. - Press Check answer to test it.
日本語
あなたも試してみよう
- 各タスクに対して
WHILEまたはREPEATを選びます。 - Check answer を押して確認する。
Use a WHILE loop to output 1, 2, 3, one per line.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a REPEAT loop to output 2, 4, 6, one per line (add 2 each time, stop at 6).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a WHILE loop to add up 1 to 4, then output the total (it is 10).
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。