java / beginner
Snippet
The Do-While Loop
The do-while loop is a post-test loop. Unlike a regular while loop, it executes the code block first and then checks the condition, ensuring the code runs at least once.
snippet.java
1
2
3
4
5
int count = 1;do {System.out.println("Count: " + count);count++;} while (count <= 3);
Breakdown
1
do {
Marks the start of the block to be executed.
2
count++;
Increments the counter variable by 1 in each iteration.
3
} while (count <= 3);
Checks the condition after execution; if true, the loop repeats.