I am trying to trace a loop from "Beginning
Java 2," by Ivor Horton.
the loop goes like this:
long limit = 20;
long factorial = 1;
for (int i = 1; i <= limit; i++)
{
factorial = 1;
for (int factor = 2; factor <=i; factor++)
{
factorial *= factor;
System.out.println(i + "!" + "is" + factorial);
}
}
1) I understand the concept of what is being done, but am sketchy on some of the finer details. I'm uncertain about how exactly the inner loop functions. Even though factor <= i is initially not true, does the body of this loop still process without incrementing factor until factor <=i becomes true?
2) I'm also unsure as to the two uses of factorial. One of them is limited to the scope of the loop and is initialized to 1 each time the outer loop runs and the other is outside the loop. What effect do the different uses have?
3) And lastly, I'm having trouble conceptualizing how factorial *= factor calculates out n!.
Any clarity would be appreciated.
Regards, Michael