The do-while loop in Java is a control structure that is used to execute a block of code repeatedly as long as the specified condition is true. Unlike the while loop, the do-while loop always executes the body of the loop at least once before checking the condition.
int i = 1; do { System.out.println("The value of i is: " + i); i++; } while (i <= 10);
In this example, the loop will print the value of i
ten times, starting from 1 and ending at 10. The value of i
is incremented by 1 in each iteration of the loop. The condition i <= 10
is checked after each iteration, and the loop will terminate when the condition is false.
It's important to note that the do-while loop is a post-testing loop, meaning that it checks the condition after executing the loop body. This is in contrast to the pre-testing loop, such as the while loop, which checks the condition before executing the loop body.