while loops · while ループ
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 repeats
while (test) { ... }runs again and again, as long as the test staystrue.- It checks the test before each pass — so the body might run zero times.
日本語
while は繰り返す
while (test) { ... }はテストがtrueの間繰り返し実行されます。- 各パスの前にテストを確認するため、本体が0回実行されることもあります。
let n = 1;
while (n <= 3) {
console.log(n); // → 1, then 2, then 3
n++;
}
English
Make progress every pass
- Something inside must move the test toward
false. - If nothing changes, the loop never stops — an infinite loop (the Stop button saves you).
日本語
各パスで進捗を出す
- 内部にある何かがテストを
falseに近づける必要があります。 - 何も変化なければループは止まらず、無限ループになります(Stopボタンで救われます)。
let n = 1;
while (n < 50) {
n = n * 2; // 2, 4, 8, 16, 32, 64 — then stop
}
console.log(n); // → 64
Explore · 探索
Each pass doubles n · 各反復でnが倍になる
while re-checks its test each pass. n must change, or it runs forever. · while は各パスでテストを再確認する。n が変化しない限り、無限に実行され続ける。
English
Common mistakes
- Change the condition inside the loop, or it runs forever.
- A
whilechecks its condition first, so it may run zero times.
日本語
よくあるミス
- ループ内で条件を変更不然、永遠に実行されます。
whileはまず条件をチェックするため、0回実行される可能性があります。
English
Now you try
- Use a
whileloop. - Press Check answer to test it.
日本語
あなたも試してみよう
whileループを使用します。- Check answer を押して確認する。
let n = 3. Use a while loop to count down: log 3, 2, 1, one per line. · let n = 3 。while ループを使用して カウントダウン :3 , 2 , 1 を1行ずつログ出力せよ。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
let n = 1. While n is 16 or less, log n then double it. Output should be 1, 2, 4, 8, 16. · let n = 1。nが16以下である間、nをログ出力してからは倍にする。出力は1, 2, 4, 8, 16となるべきである。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Use a while loop to add up 1 to 4, then log the total (it is 10). · while ループを使用して 1 から 4 を合計し、その後総計をログ出力せよ(それは 10 である)。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。