do-while LoopThe do-while loop in Java is a control structure that executes a block of code at least once, even if the condition is false.
;
// General syntax of do-while loop in Java
do {
// statements
} while (condition);
// Java program demonstrating do-while loop
class DoWhileExample {
public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
}
}
The program prints numbers from 1 to 5. The loop executes once before checking the condition, then repeats while the condition remains true.
Modify the values below to simulate how the Java code executes.
Code Logic: do { print(i); i++; } while (i <= Limit);
do-while when at least one execution is requireddo-whilewhile loop into a do-while loop