Ternary operator expression

This is a discussion on Ternary operator expression within the C Programming forums, part of the General Programming Boards category; Hi, How this expression will be evaluated??? help me out Code: ((a>b)?((a>c)?a:c):((b>c)?b:c))...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Cool Ternary operator expression

    Hi,

    How this expression will be evaluated??? help me out

    Code:
    ((a>b)?((a>c)?a:c):((b>c)?b:c))

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,730
    Quote Originally Posted by rits
    How this expression will be evaluated?
    Quickly.

    If you want to know what the expression does, analyse it yourself following what you know of the ternary operator.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    the ?: operator is directly related to if..else statements. convert one to the other and you know how it works.
    Code:
    // ((a>b)?((a>c)?a:c):((b>c)?b:c))
    if (a > b)
    {
        if (a > c)
            a;
        else
            c;
    }
    else
    {
        if (b > c)
            b;
        else
            c;
    }

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Thanks Meldreth very clear explanation...

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's worth noting that the ternary operator evaluates to a value, unlike if-else statements; so, for example, you can do something like this . . .
    Code:
    sign = (number < 0 ? -1 : 1);
    Then sign is assigned the value -1 if number is negative, and +1 otherwise.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Red face

    According to my limited thinking thats also if-else. if number is less than 0 sign is -1 otherwise 1

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rits View Post
    According to my limited thinking thats also if-else. if number is less than 0 sign is -1 otherwise 1
    Yes, what dwks was trying to say is that the use of the ternary operator is slightly different from an if (although it has the same effect).
    You can not do:
    Code:
    x = if(a < b) a; else b;
    That will not compile.
    You could do:
    Code:
    if (a < b) x = a; else x = b;
    or you can use the ternary form:
    Code:
    x = (a < b)?a:b;
    The latter comes in very handy if you have a long expression:
    If-statements:
    Code:
    if (a < b)
         x = 3.6 * y + 9.6 *b;
    else
        x = 3.6 * y + 9.6 * a;
    would be a bit easier on the hands typing and the eyes reading it like this:
    Code:
       x = 3.6 *y + 9.6 * (a < b)?b:a;
    Note that there are situations when this is RIGHT, and other times when it is wrong. It may be better to have a temporary variable:
    Code:
    double bigger = a;
    if (a < b)
         bigger = b;
    x = 3.6 * y + 9.6 * bigger;

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    The conditional evaluation is a compact way of expressing an if-else.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    As I tried to say, the conditional operator is pretty much the same thing as an if-else statement. However, the ternary operator evaluates to a value, whereas an if-else construct does not. Whether you use this value is up to you.

    You can always convert a ternary operator into an if-else, but it's not quite as simple as Meldreth has made out if the value of the expression is used. That's all I was trying to say, and matsp has elaborated nicely where I was too lazy to.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Hey buddies thanks to all of you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. branch vs ternary... cast
    By CodeMonkey in forum C++ Programming
    Replies: 15
    Last Post: 01-19-2009, 12:16 PM
  2. ternary operation with compound instruction
    By byfreak in forum C Programming
    Replies: 6
    Last Post: 07-01-2008, 03:45 AM
  3. Ternary nesting -- bad idea?
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 04-14-2005, 03:38 AM
  4. ternary operator(?), vectors, and recursive function
    By kes103 in forum C++ Programming
    Replies: 2
    Last Post: 02-02-2003, 02:19 PM
  5. ternary operation
    By lambs4 in forum C Programming
    Replies: 10
    Last Post: 12-30-2002, 07:29 AM

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