Hey there I would like to make a
sum of the integers from 1 to 100 using the three different loop
statements: while, for, and do-while.
any help would be appreciated.
Hey there I would like to make a
sum of the integers from 1 to 100 using the three different loop
statements: while, for, and do-while.
any help would be appreciated.
Is this your homework assignment?
Sure sounds like it. Usually you would just pick one method and use it, not asking for 3 possible ways to do the same task.
$i = 0;
$sum = 0;
while($i < 101){
$sum += $i;
$i+=1;
}
You can wrap this into a function and return $sum or just echo $sum
$i = 0;
$sum = 0;
do{
$sum += $i;
$i+=1;
} while($i<101);
I agree with the above. It sounds like you’re seeking answers to your homework. Whats the point studying a IT related subject if you’re not interested? - Some of us can’t even afford the opportunity of an education in IT.
Despite that, I still find myself wanting to help.
Here are 3 examples:
//While Loop
$I = 0;
while ($I < 101)
{
$I = $I +1;
print "$I <br />";
}
//For loop
for ($I = 0; $I < 101; $I++)
{
//Do something else here - $I's incrementation is dealt with automatically
print $I .'<br />';
}
//Do-While Loop
$I = 0;
do
{
print "$I<br />";
}
while ($I < 101);
Of course a quick google for php loops would of found you examples on php.net
thanks a lot guys. Tried a few attempts before asking you people.