Page 1 of 1

while loop

Posted: Mon Dec 01, 2008 6:29 pm
by djcrisp
anyone how to use the while loop that increments through and prints every odd number between 1 and 49.

thanks in advance :-)

any help is appreciated.
my guess is:


Code: Select all

<?php
$i = 1;
while ($i <= 49)
 { 
    echo $i++;
  }
?>

Re: while loop

Posted: Mon Dec 01, 2008 6:47 pm
by Peter Anselmo
It's better practice to use a "for" loop if you know ahead of time exactly how many iterations you'll need (as is true in this case).

Code: Select all

 
for( $i=1; $i<=49; $i+=2) {
 echo $i;
}