Don't understand why this do/while code works


#1


Do/While loops


I can't seem to understand why the "while" part of the loop is executing. The $loopCond variable has been set to false and the while condition should only run if $loopCond is true, but both echo statements still print out even though $loopCond has not changed. What am I not getting here"..Thanks..


    $loopCond = false;
    do {
    echo "<p>The loop ran even though the loop condition is false.</p>";
    } while ($loopCond);
      echo "<p>Now the loop is done running.</p>";


#2

Because it is not part of the do..while, but an independent statement. The loop statement ends with,

while ($loopCond);

There are two types of while. One that only executes when it satisfies the condition, and one that executes at least once before testing the condition.

while (condition) {}    // condition must yield true for the loop to run.

do {} while (condition);  // will execute at least once before checking condition.

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.