• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

nested loop question

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 ]
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I absolutely agree with you, Mike. You phrased it much better than I did.
 
michael bradly
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic