Saturday, October 6, 2012

Repetition Using FOR

FOR is used to repeat the order with a known number of repetitions.


Syntax :
for(initialization;condition;modifier)
{
statement
}

Example :

1. Displaying the numbers 1 to 20

            for($x=1;$x<=20;$x++)
{
echo $x;
}

2. Displaying even numbers from 2 to 20

            for($x=2;$x<=20;$x+=2)
{
echo $x;
}

The difference between the first example of the second instance only in the initialization and modifier, which makes x modifier increased 2 each time iteration.



No comments:

Post a Comment