Control Structures
Completion requirements
4. do-while Loop
Similar to while, but guarantees at least one execution by checking the condition after the loop body.
Syntax:
do {
// Code
} while (condition);
Example:
public class DoWhileLoopExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Count: " + i);
i++;
} while (i <= 5);
// Output:
// Count: 1
// Count: 2
// Count: 3
// Count: 4
// Count: 5
}
}
Notes:
Useful when the loop body must execute at least once.
Condition is evaluated after the loop body.