? as a arithmatic operator

This is a discussion on ? as a arithmatic operator within the C Programming forums, part of the General Programming Boards category; In the line temp = temp- (temp >150 ? 100 : 20 ); what does the '?' stand for? I ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Unhappy ? as a arithmatic operator

    In the line
    temp = temp- (temp >150 ? 100 : 20 );
    what does the '?' stand for? I know it's got to be doing something there. I just don't know what.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Its part of the ternary operator. Its goes something like this:

    (testcondition) ? ThisHappensIfTrue : ThisHappensIfFalse ;

    So in your example:

    >>temp = temp- (temp >150 ? 100 : 20 );

    if temp is greater than 150, the calculation is
    >temp - 100
    else its
    >temp - 20
    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
    Jun 2002
    Posts
    25
    >>Its part of the tenary operator<<

    otherwise known as the conditional operator.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 07:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM

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