for, while, and do-while Loops
Overview
Java has three loop constructs:
| Loop | Best for |
|---|---|
for |
Known number of iterations |
while |
Unknown iterations, test before body |
do-while |
Unknown iterations, body executes at least once |
Basic for Loop
javafor (initialization; condition; update) {
// body
}javafor (int i = 0; i < 5; i++) {
System.out.println(i); // 0, 1, 2, 3, 4
}- Initialization runs once before the loop starts.
- Condition is checked before each iteration; if
false, the loop ends. - Update runs after each iteration.
All three parts are optional:
javafor (;;) {
// infinite loop
}Multiple variables and updates separated by commas:
javafor (int i = 0, j = 10; i < j; i++, j--) {
System.out.println(i + " " + j);
}Scope: Variables declared in the initialization clause exist only within the loop.
Enhanced for Loop (for-each)
Iterates over arrays and Iterable collections without an index:
javaint[] numbers = {1, 2, 3, 4, 5};
for (int n : numbers) {
System.out.println(n);
}javaList<String> names = List.of("Alice", "Bob", "Carol");
for (String name : names) {
System.out.println(name);
}The loop variable is a copy — modifying it does not change the original array element (for primitives).
while Loop
javawhile (condition) {
// body
}javaint count = 0;
while (count < 5) {
System.out.println(count);
count++;
}The body may never execute if the condition is false from the start.
do-while Loop
javado {
// body
} while (condition);javaint count = 0;
do {
System.out.println(count);
count++;
} while (count < 5);The body always executes at least once, then the condition is checked.
java// Typical use: input validation
Scanner sc = new Scanner(System.in);
int value;
do {
System.out.print("Enter a positive number: ");
value = sc.nextInt();
} while (value <= 0);Comparing the Three Loops
java// All three print 0..4
// for
for (int i = 0; i < 5; i++) System.out.println(i);
// while
int i = 0;
while (i < 5) { System.out.println(i); i++; }
// do-while
int j = 0;
do { System.out.println(j); j++; } while (j < 5);Infinite Loops
javawhile (true) { /* ... */ }
for (;;) { /* ... */ }
do { /* ... */ } while (true);Use break to exit, or return/throw from the enclosing method.
Key Rules
| Rule | Detail |
|---|---|
| Condition type | Must be boolean |
for scope |
Init variable scoped to the loop |
do-while semicolon |
Required after the closing ) |
Enhanced for |
Works on arrays and Iterable — not Iterator directly |
| Modifying collection | Throws ConcurrentModificationException in enhanced for |