• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using Postfix notation......

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Folks, the listing below is a small program that calculates the factorial for a given range of numbers.
public class TestFactorial
{
public static void main (String[] args)
{
for (int i = 0; i < 9; i++)
System.out.println(" f(" + i + ") = " + f(i));
}
static long f (int n)
{
long f = 1;
while (n > 1)
f *= n--;
return f;
}
}

Now, the thing is, I'm having a little problem getting my head around the line
<B> f *= n--; </B>
This is an example of a postfix operation, right?
So, f is multiplied by the value of n BEFORE its value is decreased by 1.
Going by this, and using the value of n= 3 - how does it work?
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's equivalent to the following:
Don't write code like f *=n--; !
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another way using a for loop
[ March 25, 2003: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And of course the well know recursive factoral solution:

Expanding on that to cover all the bases:


Don't write code like f *=n--; !


Passionate about not writing confusing code are we? I absolutely agree, make it clear.
Michael Morris
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that n-- is not postfix notation (aka Polish notation). It is an example of using -- as a postfix operator.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wassisname said:

Passionate about not writing confusing code are we?


Must be this SCJP certi going to me 'ead.
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Here's another way using a for loop


OK, looking at this code.....
Taking the number 3 as an example
What happens??
OK...
The integer 3 enters the method as m.
And then within the for loop below
for ( int n = m ; n > 1 ; n-- )
We FIRSTLY decrease the value of 3 by 1, i.e., so we get 2. And then we multily this by f, i.e., the value 1. - 1 X 2 = 2
Then do the same thing again with the previously decremented value of 2, using the for loop, so we get 1.
And then we multiply this by f, i.e., the value 1. - 1 X 1 = 1
But, 2 x 1 = 2
How do we get 6???
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


for ( int n = m ; n > 1 ; n-- )
We FIRSTLY decrease the value of 3 by 1,


Um, .... no. That isn't how a 'for' loop works. The "n--" doesn't hit until after the first iteration of the loop.
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers!
Got it now!
Forgot to see that the postfix expression was relevant to the for loop.
Cheers folks!
 
William Barnes
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know if postfix has anything to do with it. These loops will behave the same:

and
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Steve Jensen:
Cheers!
Got it now!
Forgot to see that the postfix expression was relevant to the for loop.
Cheers folks!


In fact, the postfix operator has NOTHING to do with it. This is all about how a for loop works. When the loop starts, the first part is executed only (commonly int i = 0 . The last section (commonly i++) isn't executed until the end if the loop. And finally the comparison, or middle part, (commonly i < SOME_FINAL_VALUE), is executed after the increment.
HTH
Layne
reply
    Bookmark Topic Watch Topic
  • New Topic