increment/decrement operators

This is a discussion on increment/decrement operators within the C++ Programming forums, part of the General Programming Boards category; Ok, So I got into math functions in C++ and it showed this which is totally confusing me. I'm just ...

  1. #1
    ___
    Join Date
    Jun 2003
    Posts
    806

    increment/decrement operators

    Ok, So I got into math functions in C++ and it showed this which is totally confusing me. I'm just not comprehending what it does.


    Code:
    ++count;         
    
    //OR
    
    total = 6 + count--;
    I'm just not getting what that is meaning. Any help?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  2. #2
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Code:
    ++count;
    Increases count by 1.
    Code:
    count--;
    Decreases count by 1.

  3. #3
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    There are post and pre incremenet/decrement operators....
    which increment or decrement the variables before or after their usage.....

    Here is an example:

    Code:
    #include <stdio.h>
    
    int main()
    {
     int i=1;
    
     printf("i=%d\n",i);         /*i=1*/
     i++;
     printf("i=%d\n",i);         /*i=2*/
     printf("i=%d\n",i++);       /*i=2*/
     printf("i=%d\n",i);         /*i=3*/
     printf("i=%d\n",++i);       /*i=4*/
    
     return 0;
    }

  4. #4
    ___
    Join Date
    Jun 2003
    Posts
    806
    I'm still not understanding and CodingMaster I'm using C++ not C.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    It really doesn't get much simpler.

    int c = 5;

    sets "c" to 5.

    c++

    makes "c" equal 6.

    c--

    makes "c" equal 5 again.

    Here's codingmasters code in C++...

    Code:
    // include stream header files here...
    
    int main()
    {
     int i=1;
    
     cout << i;         /*i=1*/
     i++;
     cout << i;         /*i=2*/
     cout << i++;       /*i=2*/
     cout << i;         /*i=3*/
     cout << ++i;       /*i=4*/
    
     return 0;
    }

  6. #6
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
     int i=1;
    
     cout << i;         /*i=1*/
     i++;
     cout << i;         /*i=2*/
     cout << i++;       /*i=2*/
     cout << i;         /*i=3*/
     cout << ++i;       /*i=4*/
    
     return 0;
    }
    :P

    To expound on Elixa's message... if the ++ is before the variable, it is incremented by one BEFORE it is used. If it is after, it gets incremented AFTER it is used. Same with --.

  7. #7
    ___
    Join Date
    Jun 2003
    Posts
    806
    ok so 2 negatives minuses it by 1. opposite with positive. and its before or after depending on position.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  8. #8
    Registered User Azmeos's Avatar
    Join Date
    Jun 2003
    Posts
    65
    mhmm.

  9. #9
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    heh, _Elixia_ did the work for me.....

    int x=5; // set x the value of x to 5

    u use ++ for incrementation and -- for decrementation

    ++ is equal to +1
    -- is equl to -1

    so, u could write instead of ++ or -- for post incrementation / decrementation
    x-=1; // equal to x=x-1
    x+=1;// equal to x=x+1

    when u use pre incrementation / decrementation.....

    the compiler increments / decrements the variable first and then output its......
    e.g.:

    x--; // is equal to --x;

    but.... have a look at this code:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    int x=5;
    cout << x++ << endl; // cout: the value of x is 5 and after it increments it.....
    cout << x << endl; // cout: the value of x is 6
    cout << ++x <<endl; // cout: !!!now!!! we use pre incrementation: cout: the value of x is 7
    return 0;
    }
    Decremenation works like incrementation.....

    Dec: -- (=-1)
    Inc: ++ (=+1)
    Last edited by codingmaster; 07-10-2003 at 03:31 PM.

  10. #10
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Don't forget the difference between postfix and prefix:

    Code:
    int x=1;
    int c=++x; //c will be 2 since it increments and then returns the value
    x=1;
    int y=x++; //y will be 1 since it returns the value then increments
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  11. #11
    ___
    Join Date
    Jun 2003
    Posts
    806
    Ok cool thanks that helped me a bunch.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. How Does Increment Operators Work...??
    By ajayd in forum C Programming
    Replies: 37
    Last Post: 12-31-2008, 09:01 AM
  3. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  4. operators???
    By arjunajay in forum C++ Programming
    Replies: 11
    Last Post: 06-25-2005, 04:37 AM
  5. Operators
    By George in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2003, 06:35 PM

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