• An Exit Control Loop checks the condition for exit and if given condition for exit evaluate to true, control will exit from the loop body else control will enter again into the loop.
  • Such type of loop controls exit of the loop that’s why it is called exit control loop.

Exit Control loop

Do…While loop:

  • Do …while loop is an exit control loop.
  • It is a variation of while loop.
  • When we want to execute or perform some task at least for once, we can use do…while loop structure.
  • In a while loop if the expression becomes false at very first try then loop will never be executed.
  • Since, do…while is of type exit control loop it checks the condition at last so, first time the loop will be execute unconditionally.
  • If the condition for the exit becomes true then loop will be terminate otherwise it will be executed for the next time.
  • It has the following form:

Syntax:

do
{
    //loop body
} while (expression);
  • The main difference between while loop and do… while is as follow.
While loop Do…while loop
It is an entry control loop. It is an exit control loop.
In while loop expression/condition is checked at the time of entry. In do… while loop condition is checked at the time of exit.
It does not have semicolon (;) at the end of the expression. In do while loop semicolon (;) at the end of the expression is compulsory.
Syntax:
while(condition)
{
// loop body
}
do
{
// loop body
} while(condition);
Example:
while($i<=5)
{
echo “Value of i is $i <br/>” ;
$i++ ;
}
do
{
echo “Value of i is $i <br/> ” ;
$i++ ;
} while($i<=5) ;

The following program demonstrates use of do…while loop to to print value of i.

<?php
      $i = 0 ;
      do
      {
          echo 'Value of i is ' .  $i . '<br/>' ;
             
      } while($i > 0 ) ;
?>
   
  • The above loop will run only once. The first time it will run unconditionally and second time the loop will be terminated as the given expression evaluates to false.