Iteration: FOR · 反復:FOR
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を最新バージョンに更新してください。
FOR repeats a fixed number of times
- A
FORloop counts from a start value to an end value. NEXTmarks the end of the loop body.
FOR I ← 1 TO 5
OUTPUT I // 1, 2, 3, 4, 5
NEXT I
A running total
- Start a total at 0, then add to it inside the loop.
Total ← 0
FOR I ← 1 TO 10
Total ← Total + I
NEXT I
OUTPUT Total // → 55
Explore · 探索
Trace the running total · 合計値のトレース
A FOR loop repeats a set number of times. Step through and watch Total grow. · FOR ループは指定された回数だけ繰り返されます。ステップバイステップで実行し、Total が増加する様子を確認してください。
STEP changes the count
STEPsets how much the counter changes each pass.STEP -1counts down.
FOR I ← 5 TO 1 STEP -1
OUTPUT I // 5, 4, 3, 2, 1
NEXT I
Watch out
- Use
FORwhen you know how many times to repeat; useWHILEwhen you do not. - The counter (
I) andNEXT Imust use the same name.
Common mistakes
FOR I ← 1 TO N ... NEXT Irunsntimes, including both ends.- The counter changes automatically each turn.
Now you try
- Use
FOR ... NEXT. Type<-for←. - Press Check answer to test it.
Use a FOR loop to output the numbers 1 to 5, one per line. · FORループを使用して、数字1から5を1行ずつ出力します。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a FOR loop to add up 1 to 10, then output the total (it is 55). · FOR ループを使用して、1 から 10 までの和を計算し、合計を出力します(合計は 55 です)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Read an INTEGER N, then output N × 1, N × 2, N × 3 (one per line). For input 4, output 4, 8, 12. · INTEGER N を読み取り、次に N × 1、N × 2、N × 3 を各行に一つずつ出力します。入力 4 に対しては、出力 4、8、12 となります。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。