Home Tutorial Php Phpbasics Tutorial PHP Do While Loop

 
 

PHP Do While Loop
Posted on: March 26, 2010 at 12:00 AM
In this tutorial we will study about do-while loop, by default do-while loop iterates at least once irrespective of condition. Examples in this tutorial will make it more clear.

Do-While Control Structure:

Do-While loop is another type of loop which runs at least once, since this loop checks the condition after executing the code within. We generally use Do-While loop  when the iteration must run at least once despite of the validity of the variable. In Do-While loop we need to initialize the variable first the coding within the loop executes and at last the this loop checks the condition. We should change the value of the variable within the loop.

Format of do-while loop is as follows:

do{

statement;

}while(condition);

Example:

<?php

$i=34;

do{

echo "Value of \$i is $i <br/>";

$i++;

}while($i<11);

?>

Output:

Value of $i is 34

Example:

<?php

$a=2;

$b=1;

do

{

echo "$a*$b=".$a*$b++."<br/>";

}while($b<=10);

?>

Output:

2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20

Related Tags for PHP Do While Loop:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.