Loops: while, for, and accumulation
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 work
- A loop runs the same block of code many times, so you don't copy it by hand.
- C has three loops:
while,for, anddo-while. They all repeat while a condition is true. - The condition is checked each time around. When it becomes false, the loop stops.
日本語
作業の繰り返し
- ループは同じコードブロックを何度も実行するため、手動でコピーする必要がありません。
- C には3つのループがあります:
while、for、do-while。条件がtrueの間すべて繰り返されます。 - 各反復時に条件が確認され、falseになるとループは停止します。
English
while loops
while (test) { ... }checks the test first, then runs the block if it is true.- Something inside the block must eventually make the test false, or the loop never ends.
- Use
whilewhen you don't know in advance how many times you will repeat.
日本語
while ループ
while (test) { ... }はまずテストを確認し、trueであればブロックを実行します。- ブロック内の何かが最終的にテストをfalseにしなければ、ループは終了しません。
- 反復回数が事前不明な場合に
whileを使用します。
English
for loops
- A
forloop puts three parts in one line:for (start; test; step). for (int i = 0; i < 5; i++)means: startiat0, keep going whilei < 5, add1each time.- Use
forwhen you are counting a known number of times.
日本語
for ループ
forループは3つの部分を1行にまとめます:for (start; test; step)。for (int i = 0; i < 5; i++)は以下を意味します:iを0で始め、i < 5の間継続し、毎回1を加算します。- 既知の回数カウント时使用
forを使用します。
#include <stdio.h>
int main(void) {
int total = 0;
for (int i = 1; i <= 4; i++) {
total = total + i; // accumulate: 1, then 3, then 6, then 10
}
printf("%d\n", total); // 10
return 0;
}
English
Accumulation and counting
- An accumulator is a variable that builds up a result across the loop, like
totalabove. - A counter adds
1each time something happens, to count matches. break;stops the loop early;continue;skips to the next round.
日本語
蓄積とカウント
- 蓄積変数はループを通じて結果を積み上げる変数で、上記の
totalのようなものです。 - カウンタは事象が起きるたびに
1を加算し、一致数をカウントします。 break;はループを早期に停止させ、continue;は次の反復へスキップします。
English
Common mistakes
- Do not put a
;right afterfor(...)— it makes an empty loop. - Change the condition so the loop can end.
日本語
よくあるミス
;をfor(...)の直後に置かないでください。空のループになってしまいます。- ループが終了できるよう条件を変更してください。
English
Now you try
- Set up your accumulator or counter before the loop, update it inside, and use it after.
- For the function tasks, do not write a
main— the checker provides one.
日本語
あなたも試してみよう
- 蓄積変数やカウンタはループ前に設定し、中で更新し、後で使用します。
- 関数タスクについては、
mainを書かないでください;チェックツールが用意します。
Explore · 探索
Loops and accumulation
A loop repeats; a running total builds up each pass.
In main, use a for loop to print the numbers 1 to 5, each on its own line.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete int sum_to(int n) so it returns 1 + 2 + ... + n. Return 0 when n is 0 or negative. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete int count_multiples(int n, int k) so it returns how many numbers from 1 to n are divisible by k. Use % inside a loop. Do not write a main.
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。