while(false){
System.out.println("While ran");
}
do{
System.out.rpintln("Do while ran");
} while(false);
The output of the code above is "Do while ran" only. This is a classic example of the difference between do/while and while. Both will only run if the condition is true. However do/while will run the code block and then test the condition. A while loop will test the condition and then run the code block.
Do/while is great when you always want to run some code AT LEAST once. Other than that stick to a while loop.
Note - In psuedocode there is something known as a repeat/until. In Java this is the do/while loop but in other languages it is called repeat/until. They do work slightly differently in the fact that repeat/until will only loop if the condition evaluates to false. Ignore the rest of this page if you are only interested in Java.
The above code (written in psudeocode) will loop forever!
There are two major differences from repeat/until and while / for -
Converting this into a REPEAT/UNTIL -
The condition appears at the bottom to signify that the loop will always run once. Also notice that the condition has changed from A<=10 to A>10 . This is because the loop will only run if the condition is false (so we effectively inverse the condition).