| ||
Do While Loop
A "do while" loop is a slightly modified version of the while loop. In "while-loops" the conditional statement is checked and if true then the code within the while loop is executed. On the other hand, a "do-while" loop always executes its block of code at least once. This is because the conditional statement is not checked until after the contained code has been executed. PHP -
While Loop and Do While Loop Contrast The examples below shows the difference between these two loop types. while-loop: $book = 0; while($book > 1){ echo "buy a book"; } Display:
This while loop's conditional statement failed (0 is not greater than 1), therefore the code within the while loop was not executed. do-while: $book = 0; do { echo "buy a book"; } while ($cookies > 1); Display:
buy a book The code segment "buy a book" was executed first. A do-while loop first do it and and then it checks the while condition! You might not need to use a do while loop often but it is good to know it's there! |