PHP Do While Loop Tutorial

PHP Do While Loop Tutorial

  • PHP
  • 4 mins read

In this tutorial, you will learn about do while loop in PHP.

For while and for loop, we have seen, a test condition will be evaluated and tested first, then if evaluated test condition returns true as 1, then the loop body
will be executed. Therefore, the loop body will not be executed at all if the test condition is not satisfied.

For some situation, the loop body will be executed first before the test condition is satisfied. For such a situation, the do-while loop can be used.

The general syntax of do..while loop is given below:

do
{
body of loop;
}
while(test condition);

For do..while loop, the body of the loop will be executed first. Then the test condition will be evaluated and tested, and if it returns true as 1, then the body of the loop will be executed again. After the execution of the loop body, the test condition will be evaluated and tested again. If it returns 1, then the body of the loop will be executed again.

This process will be continued until the test condition becomes false. After that, the loop will be terminated. Actually, the body of the do while loop will be executed at least once. After the termination of the loop, the control goes to the statement that appears immediately after the while statement.

Do..while loop is called as exit controlled loop. Here firstly loop body will be executed before the evaluation and testing of test condition(whether the loop will be continued or not).

Program 1 on do..while loop:

<html>
<body>
<?php

$a=0;
do
{
echo "happy new year 2019";
$a=$a+1;
}
while($a!=0);
?>
</body>
</html>

Output: happy new year 2019 will be printed for an infinte number of times.

Program 2 on do..while loop:

<html>
<body>
<?php

$a=0;
do
{
echo "happy new year 2019";
$a=$a+1;
}
while($a==0);
?>
</body>
</html>

Output: happy new year 2019 will be printed for one time.

Here firstly $a=0;will be executed. Then on reaching the do statement, the program will proceed to evaluate the body of the loop first. So echo "happy new year 2019"; will be executed. Then $a=$a+1; will be executed. After the execution, the value of $a will be 1, and then the while statement will be evaluated. Here $a==0 will be tested. But here it will not return 1(because the value of $a is 1). So the loop will be terminated, and the output will be the happy new year 2019.

Program 3 on do..while loop:

<html>
<body>
<?php

$a=1;
do
{
$b=1;
do 
{


$y=$a*$b;
echo " ".$y;
$b=$b+1;

}
while($b<=3);
echo "<br>";
$a=$a+1;
}
while($a<=3);

?>
</body>
</html>

Output:

1 2 3
2 4 6
3 6 9

Program 3 on do..while loop:

<html>
<body>
<?php

$a=1;
do
{
$b=1;
do{

if($a==$b)
{
echo "1"." ";
}
else
{
echo "0"." ";
}
$b=$b+1;

}
while($b<=3);
echo "<br>";
$a=$a+1;
}
while($a<=3);

?>
</body>
</html>

output:

1 0 0
0 1 0
0 0 1

Program 4 on do..while loop:

<html>
<body>
<?php
$fact=1;
$i=1;
$n=5;
do
{
$fact=$fact*$i;
$i=$i+1;

}
while($i<=$n);

echo "The factorial of"." ".$n." is=".$fact;
?>
</body>
</html>

Output: The factorial of 5 is=120.

Difference between for loop and do..while loop:

1. for loop is called an entry controlled loop, do..while loop is called as exit controlled loop.
2. do..while loop will be executed at least once in any condition(even loop control condition is false)but for loop will be run only if the test condition
is evaluated to true.

NB: foreach loop works only on the array. So this loop will be discussed later.