do-while loop

do-while loop:
A slight variation to the while loop is the do….while loop,used when you want a block of code to be executed at last once and made conditional only after that .

Do-While in C Programming
Do-While in C Programming
do
{
-
statement
-
}while(condition);

⦁	IN this case the operation is slightly different i.e first the statements are executed and then the condition is checked.if the condition is true the statement are executed again .if the condition is false,the statement are not executed again.
Example for Do-while Loop:
<?php
         $i = 0;
         $num = 0;
         
         do {
            $i++;
         }
         
         while( $i < 10 );
         echo ("Loop stopped at i = $i" );
      ?>

OUTPUT:
Loop stopped at i = 10
Example for do-while loop:


<?php  
$n=1;  
do{  
echo "$n<br/>";  
$n++;  
}while($n<=10);  
?>  

Output:

1
2
3
4
5
6
7
8
9
10
Translate »