aspose file tools*
The moose likes Beginning Java and the fly likes nested loop question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "nested loop question" Watch "nested loop question" New topic
Author

nested loop question

michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
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
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12


1) 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?

Almost. The inner loop processes until the comparison becomes true. Then factor is incremented and the inner loop reinitializes and runs again until the comparison becomes true. This continues until the comparison in the outer loop 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?

The reference to the factorial variable outside the loop is never used. I've moved the declaration of that variable to inside the loop where it is used.

3) And lastly, I'm having trouble conceptualizing how factorial *= factor calculates out n!.

If you run the code above, you will see how it works. If you still have questions after you've seen the output, please rephrase your question.
[ February 10, 2002: Message edited by: Marilyn deQueiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

note on #2: They are actually the same variable, not two different ones. It is simply declared outside of either loop, but re-initialized inside the innermost loop. So it breaks the 'least privelege' quasi-rule of variable declaration.

When you guys talk about "...one of them...", it makes me think you believe there are two instances of the variable, and that scope hiding is going on.

#3: if you attempt to calculate 6! in your head, what are you most likely to do? Would you try to calculate what 1*2*3*4*5*6 equals? (and an interesting philosophical discussion can spring from this line of thinking)... or, do you figure it out as a series of 'partial answers'

ie:
what is 1*2?
multiply that answer by 3
multiply that answer by 4
etc...

This is what that loop is doing.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
I absolutely agree with you, Mike. You phrased it much better than I did.
michael bradly
Ranch Hand

Joined: Oct 06, 2000
Posts: 112
Thank you both for the insight. I finally understand what the code is doing.
If you attempt to calculate 6! in your head, what are you most likely to do? Would you try to calculate what 1*2*3*4*5*6 equals?

When I do 6! in my head, I actually go 6*5*4*3*2*1 and I think that is what caught me up. I couldn't figure out how the loop would do that repeatedly with a higher number each time. I can see that the loop isn't doing what I am thinking, so maybe I should start thinking more like the loop. Again, thanks for the help.
Regards, Michael
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

OR...

As an exercise for yourself, rewrite that loop to calculate the factorial the way *you* do it. It would be excellent practice with the for loop.

And as a clarification.. i meant 1*2*3*4*5*6 to imply that you did the *entire* calculation as a single operation, and not implying any order. In fact, order would be meaningless if you could indeed calculate the 'entire' answer in an instance of thought. Which is that philosophical discussion I alluded to.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: nested loop question