Login | Register | FAQ
Anonymous

Understanding Loop "Process"

+ Post a reply

4 posts Page 1 of 1


Understanding Loop "Process"

by Fletch » Tue Jan 17, 2012 12:19 am

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
for($num=1; $num <= 10; $num++)
{
echo "Number: $num<br />";

}
?>

…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 random number up to 10) which ran into, for example:


<?php

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

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

?>

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

$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";

?>


I’m stumped trying to understand the 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.

I've italicized my comments and questions to easier distinguish from code. Hope it helps.

Thank you in advance.
Fletch
User avatar

Fletch

  • Posts: 2
  • Joined: Tue Jan 17, 2012 12:15 am

Re: Understanding Loop "Process"

by Fletch » Tue Jan 17, 2012 12:22 am

Additional question: When does the RANDOM NUMBER come in when the code processes? Is it the first factor is it determined later?

Fletch
User avatar

Fletch

  • Posts: 2
  • Joined: Tue Jan 17, 2012 12:15 am

Re: Understanding Loop "Process"

by atik » Tue Jan 17, 2012 11:02 am

Hi Fletch,
From your last question, when random number comes in the code process. When you use the assingment operation as
Code: Select all
$number = rand(1,10);
, then you call a funcion named rand() and it's return a value between 1 to 10 and assins this value to the $number variable.
Then the first for loop, just look at this code. i am showing for $number = 5;
Code: Select all
$factorial = $factorial * $counter;

Then for iterations the values are as follows
$factorial = 1*1;
$factorial = 1*2;
$factorial = 2*3;
$factorial = 6*4;
$factorial = 24*5;

So the last value of the variable $factorial is 120.
And for the second for loop as you write reverse this method as
$factorial = 1*5;
$factorial = 5*4;
....

So you thought that every time in the multiplication the value of the $factorial is 1 which is not the case, as i show here it is increasing with the iteration.
Hope this will help you to understand the case here.
User avatar

atik

  • Posts: 471
  • Joined: Mon Oct 17, 2011 4:55 pm

Re: Understanding Loop "Process"

by akashtablet » Sat Jan 28, 2012 1:07 pm

The question you are asking can also be solved by recursion. In recursion you just have to repeatedly call the same function until a specific condition is reached.
User avatar

akashtablet

  • Posts: 50
  • Joined: Fri Jan 27, 2012 8:24 pm
  • Location: India
--
The computing and fun with click for Micromax fun book


+ Post a reply

Page 1 of 1