A while loop
continues repeating as long as certain conditions are true. The loop works as
follows:
1. You set up a condition.
2. The condition is
tested at the top of each loop.
3. If the condition
is true, the loop repeats. If the condition is not true, the loop stops.
The following is the
general format of a while loop:
while
( condition )
{
block
of statements
}
The following
statements set up a while loop that looks through an array for an apple:
$fruit
= array ( “orange”, “apple”, “grape” );
$testvar
= “no”;
$k =
0;
while
( $testvar != “yes” )
{
if
($fruit[$k] == “apple” )
{
$testvar
= “yes”;
echo
“apple\n”;
}
else
{
echo
“$fruit[$k] is not an apple\n”;
}
$k++;
}
These statements
generate the following output:
orange
is not an apple
apple
Written by “Shojib”.
No comments:
Post a Comment