Do while loops are another useful loop construct. Do while loops work exactly the same as normal while loops, except that the script evaluates the while expression after the loop has completed, instead of before the loop executes, as in a normal while loop.
The important difference between a do while loop and a normal while loop is that a do while loop is always executed at least once. A normal while loop may not be executed at all, depending on the expression. The following do while loop is executed one time, printing out $i to the screen:
$i = 0; do { print $i; } while ($i>0);
Whereas the following is not executed at all:
$i = 0; while($i > 0) { print $i; }
Quite frankly, I never find much use for do while loops, but you may find an odd problem that requires it.
Top |