The magic of WHILE Loops
The WHILE loop is probably the most popular of the loop slaves because he is very intuitive to our needs as a programmer or web developer. Our WHILE loop keeps going while our statement which we give it to evaluate remains true. Very simple and efficient!
<?php
$factorial = 1;
$factorialThis = 5;
while ($factorialThis > 0)
{
$factorial = factorial * factorialThis;
$factorialThis--;
}
?>
The code above is jibberish unless you know what a factorial is. I’ll take the assumption that if you are reading this you are at least moderately intelligent and thus will be able to figure out what it is by means of a simple example rather than a long explanation that should be read before going to bed if you wish to quickly fall asleep.
Five factorial, written commonly as 5! is equal to 5×4x3×2x1 = 120.
In the above example we compute the value of five factorial very quickly within our WHILE loop! The best part is, our code is very flexible thanks to the WHILE loop. Sure, we could have calculated the same thing using integer variables, but with the above code we can simply change the value of $factorialThis to compute the factorial of any number.
Really, this is magical and as a programmer, you must learn the spells if you hope to be a powerful wizard! Don’t tell people though that you are both a programmer and a powerfull wizard, as if you weren’t cool enough already.
May 29th, 2007 at 7:03 am
[...] WHILE Loops [...]