While used to repeat a command to a certain amount. Having a final value true or false and the loop will continue as long as the condition is true.
Syntax
:
while(condition)
{
statement;
}
Example :
1. Displaying item 1 to 20
$x=1;
while($x<=20)
{
echo $x++;
}
Do ... While almost the same, but while looping commands will be done first and will stop when it reaches a certain stop condition.
Syntax
:
do{
Statement;
}
while(condition);
Example :
1. Displaying item 1 to 20
$x=1;
do{
$x++;
Echo Sx;
}
while ($x<=20);
The difference between While and Do .. while is if the first check while new conditions while executing statemenr Do .. While statement will execute first and check the condition so it is definitely a statement to be executed at least once.
No comments:
Post a Comment