Do…while loop
Do..while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do..while loop is guaranteed to run (the truth expression is only checked at the end of the iteration), whereas it’s may not necessarily run with a regular while loop (the truth expression is checked at the beginning of each iteration, if it evaluates to FALSE right from the beginning, the loop execution would end immediately).
The general syntax for a do..while loop is as follows:
do { statement; } while (expr);
Example
i = 1; sum=0; do { sum=sum+i; } while (i<0);
The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to FALSE (i is not smaller than 0) and the loop execution ends.
Advanced C users may be familiar with an additional statement available in loop structures to allow stopping execution in the middle of code blocks, by using the break statement. The following code fragment demonstrates this:
do { if (i < 5) { // i is not big enough; break; // will go straight forward to the point immediately following the loop } i = i * factor; if (i < $minimum_limit) { break; // will go straight forward to the point immediately following the loop } // i is ok, process i ...process i... } while(true); // here's the point immediately following the loop... return(i);