Control Structure : while & do while loop compared

  • while loop

    • The condition is checked in the beginning of the loop and finds Friday to be true and exits the loop.

  • do_while

    • The condition is verified at the end of the loop, so function enters the loop finds Friday, goes back to the loop again till it cycles to Friday

 

while : loop watch before then enter

<?php
//get the current date in number of seconds
$currentDate = time();

//print some text explaining the output
print("Days left before Friday:\n");
print("<ol>\n");
while(date("l", $currentDate) != "Friday")
{
//print day name
print("<li>" . date("l", $currentDate) . "</li>\n");

//add 24 hours to currentDate
$currentDate += (60 * 60 * 24);
}
print("</ol>\n");
?>

do while loop : jump first then decide, exit at-last

 

<?php
/*
** get the current date in number of seconds
*/
$currentDate = time();
//print some text explaining the output
print("Days left before next Friday:\n");
print("<ol>\n");
do
{
/*
** print day name
*/
print("<li>" . date("l", $currentDate) . "</li>\n");
/*
** add 24 hours to currentDate
*/
$currentDate += (60 * 60 * 24);
}
while(date("l", $currentDate) != "Friday");
print("</ol>\n");
?>