Loops process

Hello,

Total Newbie here. Currently on LOOPS.

I get how “While” and “For” (which is a compact version of While, correct?) execute a block of code for specified number of times, or while a specified condition is true.

I understand this:

[php]<?php
for($num=1; $num <= 10; $num++)
{
echo “Number: $num
”;

}
?>[/php]

…which repeats the count from ONE to TEN

My questions are more for understanding “the sequence by which loops process code” (I hope I’m using the right terminology).

Take these codes (for getting factorials of a random number up to 10) which I ran into, for example:

[php]<?php

$number = rand(1,10);
$factorial = 1;

for ($counter=1;$counter <= $number; $counter++)
{
$factorial = $factorial * $counter;
}
echo “The factorial of $number is $factorial”;

?>[/php]

I’d like to know how the code works—step by step. Perhaps an actual numeric assignment might clear it up for my confused mind. E.G.
$factorial = $factorial (1 based on the condition?) * $counter (starts at 1?);
So it repeats?—1 x 1
Then 1 ($factorial) x 2 ($counter +1)?
Then……???
How does the formula for factorials (e.g. FIVE: 5x4x3x2x1=120) work here?


Same Question with this (which seems to be the reverse):

[php]<?php

$number = rand(1,10);
$counter=$number;
$factorial = 1;

for ($counter=$number;$counter >0;$counter–)
{
$factorial = $factorial * $counter;
//1 ($factorial) x random number ($counter) say, 5
–Then 1 x 5 minus 1=4
–Then 1 x 4?
//
}
echo “The factorial of $number is $factorial”;

?>[/php]

I’m stumped trying to understand how the code processes. Maybe I’m completely off the mark. I’d truly appreciate some enlightenment.

Pardon the long questions. I just want to learn this completely before moving forward.

Thank you in advance.
Chris

Hi Chris, even I just picked up PHP and am an absolute beginner, however I did some procedural C++ programming (I’m and electrical and electronics engineer). So I may be able to help you out here.

[code]

<?php $number = rand(1,10); $factorial = 1; for ($counter=1;$counter <= $number; $counter++){ $factorial = $factorial * $counter; } echo "The factorial of $number is $factorial"; ?>[/code]

$number = rand(1,10) generates a random number from 1 to 10 and assignes it to the variable $number, this is the random value whose factorial we wish to calculate. The variable $factorial is assigned the value 1.

Now the for loop comes into play. This is what the condition in the for loop does:
(1) $counter = 1: It first sets variable $counter to 1.
(2) $counter <= number: It then says, until the variable $counter reaches the value of the variable $number keep repeating this loop.
(3) $counter++: This just increases the counter by 1 evertime the loop is executed.

Code inside the loop:
First suppose the rand(1,10) function generates the number 4. Now lets see how the factorial will be calculated:

Loop 1:
Remember! $factorial has initially been set to 1, at the first execution of the loop counter is also just 1. These values are multiplied and we get the value 1.

Loop 2:
$factorial after the first execution of the loop is still one. Now $counter however has increased in value by 1 because of the incrementer in the for conditional statement. So -
$factorial = $factorial * $counter (which is now 2).
The value of factorial will now be 2.

Loop 3:
Value of $factorial is now 2. When the loop is executed for the third time the value of $counter increases to 3. So now -
$factorial = $factorial (2) * $counter (3)
$factoral now is 6.

Loop 4:
$counter increases to 4 (it is now = to the value the rand(1,10) function generated so this is the last time this loop will be executed. The value of $factorial has now increased to 6. So -
$factorial = $factorial (6) * $counter (4)
$factorial now is 24!

This is the factorial of 4 since: 432*1 = 24.

After this we exit the loop and simply echo the value of $factorial & voila. Hope that helped.

Tremendous! I thought an engineer might be the one to explain this clearly and thoroughly.

I’ve never done any programming. I didn’t know that the new factorial product would also carry over to the next loop, thinking that it would always be the assigned value of 1. And that only the counter would incrementally increase in each iteration.

Now I know how the program works.

Thanks, Xanathos! Much appreciated.

Chris

Anytime! I’m just glad I was able to help. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service