Share on Google+Share on Google+

PHP Do While Loop

Advertisement
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

Advertisements

Posted on: March 26, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.