One variation of the while loop is the do/while loop. This loop will run the code block once, then check to see if the condition is true before repeating the loop indefinitely.
do {
// code block to be executed
}
while (condition);
A do/while loop is used in the example below. Because the code block runs before the condition is tested, the loop will always run at least once, even if the condition is false:
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
The loop will never stop if you don’t increase the variable used in the condition!
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.