Loops: while, for, and accumulation · ループ:while、for、および累加
Loops
- A loop repeats a block of code many times.
- Loops let you count, add up numbers, and do work without copying lines.
- In this lesson you learn the
whileloop and theforloop.
ループ
- ループはコードブロックを複数回繰り返します。
- ループを使用すれば、コピーすることなくカウントや合計計算、作業を行うことができます。
- このレッスンでは
whileループとforループを学びます。
The while loop
- A
whileloop runs its block again and again while a condition istrue. - You need three parts: start a counter, test it, and change it inside the loop.
- If you never change the counter, the loop runs forever. Be careful.
while ループ
whileループは条件がtrueである間、ブロックを繰り返し実行します。- 3つの部分が必要です:初期化 counter、テスト counter、ループ内で 更新 counter です。
- Counter を更新しない場合、ループは永遠に続きます。ご注意ください。
public class Main {
public static void main(String[] args) {
int i = 1; // start
while (i <= 3) { // test
System.out.println("Line " + i);
i = i + 1; // change
}
}
}
The for loop
- A
forloop puts the start, test, and change on one line. - The shape is
for (start; test; change) { ... }. - It does the same job as the
whileabove, but it is shorter and clearer.
for ループ
forループでは、開始、テスト、変更 が1行にまとめられます。- その形式は
for (start; test; change) { ... }です。 - 上記の
whileと同じ役割を果たしますが、より短く明瞭です。
public class Main {
public static void main(String[] args) {
for (int i = 1; i <= 3; i++) {
System.out.println("Line " + i);
}
}
}
Accumulation
- Accumulation means building up an answer step by step inside a loop.
- Use a variable that starts at
0and grows:sum = sum + i;. i++is a short way to writei = i + 1.sum += iis short forsum = sum + i.
蓄積
- 蓄積とは、ループ内で段階的に答えを積み上げていくことです。
0から始まって増加する変数を使用します:sum = sum + i;。i++はi = i + 1を短縮した書き方です。sum += iはsum = sum + iの省略形です。
public class Main {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 5; i++) {
sum += i; // add each number
}
System.out.println(sum); // 15
}
}
Counting with a condition
- You can count how many times something is true.
- Start a counter at
0, and add1only when the test passes. - Here we count even numbers from 1 to 10.
条件付きでのカウント
- 何回条件が真になったかをカウントできます。
- Counter を
0で始め、テストが通過したときにのみ1を加算します。 - ここでは1から10までの偶数をカウントしています。
public class Main {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) { // % gives the remainder
count++;
}
}
System.out.println(count); // 5
}
}
Off-by-one care
- The most common loop bug is doing one too many or one too few steps.
i <= nincludesn.i < nstops beforen.- To loop from 1 to
n(and includen), usefor (int i = 1; i <= n; i++).
オフ・バイ・ワンへの注意
- 最も一般的なループのバグは、ステップが1つ多すぎるまたは1つ少なすぎることです。
i <= nはnを含みます。i < nはnより前に停止します。- 1から
nまでループしてnを含む場合、for (int i = 1; i <= n; i++)を使用します。
public class Main {
public static void main(String[] args) {
int n = 4;
// i goes 1, 2, 3, 4 — four times, because of <=
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}
}
Common mistakes
for (int i = 0; i < n; i++)runsntimes;<=runs one extra.- Do not put a
;right afterfor(...).
よくあるミス
for (int i = 0; i < n; i++)はn回実行され、<=は1回多く実行されます。;の直後にfor(...)を置かないでください。
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
あなたも試してみよう
- 各タスクはクラス骨組みを事前入力しています。main 内にコードを書くか、表示されたメソッドを完成させてください。
- 実行 を押してコンパイル・実行し、その後 回答を確認 を押してください。
- コードはサーバー上でコンパイル・実行されるため、初回実行でも高速です。
Loops and accumulation · ループと累加
A loop repeats; an accumulator builds up across the passes. · ループは繰り返され、**累算変数(accumulator)**は反復ごとに値を積み上げる。
Use a for loop to print the numbers 1 to 5, each on its own line. · forループを使って、各数字を別々の行に印刷し、1から5までの数字を表示する。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete sumTo(int n) so it returns the sum 1 + 2 + ... + n. For example sumTo(5) is 15. If n is 0 or less, return 0. · sumTo(int n)を完成させ、合計1 + 2 + ... + nを返す。例:sumTo(5)は15である。nが0以下の場合、0を返す。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。
Complete countMultiples(int n, int k) so it returns how many numbers from 1 to n divide evenly by k (use % == 0). For example countMultiples(10, 3) is 3 (that is 3, 6, 9). · countMultiples(int n, int k)を完成させ、1からnまでのうち、kで割り切れる数がいくつあるかを返す(% == 0を使用)。例:countMultiples(10, 3)は3(つまり3, 6, 9)である。
Click Run to see the output here. · 実行ボタンをクリックして出力を確認してください。