- 最終更新
- 2026/08/17
3秒でわかる
先に本体を実行してから継続条件を判定するループ構文。入力の受け付けなど、最低 1 回は必ず動かしたい繰り返しを素直に書けます。
30秒図解
もう少し詳しく
どういうものか
do-while は、
Java、C、C++、JavaScript でwhile True と break で
なぜ必要か
「まず 1 回やってみて、
具体例
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number;
do {
System.out.print("1 から 10 の数を入力 ");
number = sc.nextInt();
} while (number < 1 || number > 10);
System.out.println("受け付けました " + number);
}
}入力を
つまずきやすいところ
条件をnumber = sc.nextInt(); を
もう 1 つは while (...) の
似た用語との違い
| 構文 | 判定の位置 | 最低実行回数 |
|---|---|---|
| while | 先頭 | 0 回 |
| do-while | 末尾 | 1 回 |
| for | 先頭 | 0 回 |
回数が
覚え方
「やってから


