post-incremented when?

This is a discussion on post-incremented when? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> using namespace std; int main() { unsigned long factorial = 1, number = 0; cout <<" Enter ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    28

    post-decremented when?

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    
     unsigned long factorial = 1,
                   number = 0;
    
      cout <<" Enter number " ; // entered 12 as number
      cin >> number;
    
      for ( int counter = number; counter >= 1; counter -- )     // post increment counter
          factorial *= counter;    // value of counter is 12 * 12 ?
    
          cout << " " << factorial;   // answer
    
    
    
          return 0;
    }

    Hi i was wondering in the for structure where i have counter--.
    And i have then counter * counter ,
    does it multiply 12 * 12 ? or 12 * 11 and when does it get post decremented?

    Thanks alot.
    Last edited by blackgold>>; 04-04-2004 at 06:06 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Add some cout lines and see for yourself

    Code:
    for ( int counter = number; counter >= 1; counter -- )
    {
      cout << "counter : " << counter endl 
           << "factorial before: " << factorial <<endl;
      factorial *= counter;
      cout << "factorial after: " << factorial <<endl;
    }
    Also, this: -- is decrement. This: ++ is increment.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Hi hammer thanks alot i wrote wrong word.
    I will try what you said

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    28
    Ok here is output.

    Enter number 12
    1st 12
    2nd 12
    3rd 12
    1st 11
    2nd 132
    3rd 11
    1st 10
    2nd 1320
    3rd 10
    1st 9
    2nd 11880
    3rd 9
    1st 8
    2nd 95040
    3rd 8
    1st 7
    2nd 665280
    3rd 7
    1st 6
    2nd 3991680
    3rd 6
    1st 5
    2nd 19958400
    3rd 5
    1st 4
    2nd 79833600
    3rd 4
    1st 3
    2nd 239500800
    3rd 3
    1st 2
    2nd 479001600
    3rd 2
    1st 1
    2nd 479001600
    3rd 1

    Program exited normally.
    (gdb)

    Looks good .

    Thanks alot for idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help (Trying to post message)
    By mrkm in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-06-2003, 05:05 PM
  2. Post your Best Text Adventure
    By Joe100 in forum Game Programming
    Replies: 3
    Last Post: 08-15-2003, 06:47 PM
  3. Auto POST
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-07-2003, 11:42 AM
  4. post update
    By iain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-12-2001, 11:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21