Control Structures
Completion requirements
3. Looping (Iterative) Structures
Looping structures repeat a block of code while a condition is true or for a specified number of iterations.
2.3.1 for Loop
Executes a block of code a fixed number of times, typically with a counter.
Syntax:
for (initialization; condition; update) {
// Code to repeat
}
Example:
public class ForLoopExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
System.out.println("Count: " + i);
}
// Output:
// Count: 1
// Count: 2
// Count: 3
// Count: 4
// Count: 5
}
}
Notes:
Initialization (e.g., int i = 1) runs once before the loop.
Condition (e.g., i <= 5) is checked before each iteration.
Update (e.g., i++) runs after each iteration.