Translate

Monday, May 5, 2014

9.Control Structure (Loop)

Loop Structure
For loop
Syntax:
for (init; condition; increment)
{
   statements;
}
for (init; condition; increment):
  statements;
endfor;
Example:
<?php
$s=0;
echo "sum = ";
  for($i=1;$i<=5;$i++)
  {
    $s =$s + $i;
    $op=$i>4?"= " : "+ ";
    echo $i ." ". $op;
  }
​​  echo  $s;
?>

While loop

Syntax:
<?php
while (condition)
{
 statements;
 increment;
}
?>
example
<?php
$s=0; $i = 1;
echo "sum = ";
while($i<=5)
 $s =$s + $i;
 $op=$i>4?"= " : "+ ";
 echo $i ." ". $op;
 $i++;
}
echo $s;
?>

do...while Statement

Syntax:
<?php
do{
 statements;
}
?>
example
<?php
$i=5;
do
 {
   echo "Hello World !";
   $i++;
 }while($i<=7);
?>
Understanding Break,Continue and Exit

Break

Break used to leave the Loop.

Continue

Continue for further processing Loop nothing under it does not work.

Exit

Exit For End Process Code.

0 comments:

Post a Comment