for loops · 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を最新バージョンに更新してください。
English
Repeating with a for loop
- A loop runs the same block many times, so you don't repeat yourself.
for (let i = 1; i <= 5; i++) { ... }has three parts: starti = 1, testi <= 5, stepi++.
日本語
for ループによる繰り返し
- ループは同じブロックを何度も実行するため、重複を避けることができます。
for (let i = 1; i <= 5; i++) { ... }には3つの部分があります:開始i = 1、テストi <= 5、ステップi++。
for (let i = 1; i <= 3; i++) {
console.log(i); // → 1, then 2, then 3
}
English
Build a running total
- Keep a variable outside the loop and add to it on every pass.
日本語
累積和を作る
- ループの外に変数を保持し、各パスごとに足し込みます。
let total = 0;
for (let i = 1; i <= 10; i++) {
total = total + i;
}
console.log(total); // → 55
Explore · 探索
Trace the running total · 合計値のトレース
A loop repeats its body. Step through the passes and watch total grow. · ループは本体を繰り返す。パスをステップバイステップで進め、total が成長する様子を見よ。
English
Watch out
i <= 5stops after 5;i < 5stops before 5. Picking the wrong one is the classic "off-by-one" bug.- Start the total at
0before the loop, not inside it.
日本語
注意
i <= 5は 5 の後に停止します;i < 5は 5 の前に停止します。間違えて選ぶと、古典的な「オフバイワン」バグになります。- 合計をループの外、
0で初期化します。
English
Common mistakes
for (let i = 0; i < n; i++)runsntimes.- Declare the counter with
let, not without.
日本語
よくあるミス
for (let i = 0; i < n; i++)はn回実行されます。- カウンターを
letで宣言します(宣言せずに使わないように)。
English
Now you try
- Use a
forloop. - Press Check answer to test it.
日本語
あなたも試してみよう
forループを使用します。- Check answer を押して確認する。
Use a for loop to log 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 log the total (it is 55). · for ループを使用して 1 から 10 を合計し、その後総計をログ出力せよ(それは 55 である)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a for loop to log the even numbers 2, 4, 6, one per line (step by 2 with i += 2). · for ループを使用して偶数 2, 4, 6 を1行ずつログ出力せよ(i += 2 で2ステップ毎に進める)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。