Thread: Conditional Operator

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Conditional Operator

    Is it possible to use the Ternary operator without the colon( : ).
    i.e. with only one operand.
    Code:
    (condition)?
    instead of
    (condition)? true_stmt : false_stmt ;
    I've heard that its very low precedence causes it to return (condition) rather than true_stmt or false_stmt when used with any operator other than assignment operators.
    Last edited by arjunajay; 07-09-2008 at 08:20 PM. Reason: :)

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    And that makes sense how?

  3. #3
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    My edit was to make sense.
    If we use any operator other than assignments or comma, ternary op being of very low precedence would not work as expected but instead return the value of the test expression. I think it has happened to me some times when i used it with
    Code:
     cout<<(expression)? sddfd : dfgdfgdfg;
    wait ...
    Let me check before i say something foolish...
    Last edited by arjunajay; 07-09-2008 at 08:28 PM. Reason: said something senseless...

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by arjunajay View Post
    Is it possible to use the Ternary operator without the colon( : ).
    i.e. with only one operand.
    Code:
    (condition)?
    instead of
    (condition)? true_stmt : false_stmt ;
    I've heard that its very low precedence causes it to return (condition) rather than true_stmt or false_stmt when used with any operator other than assignment operators.
    To answer your question: no there is no way to use ternary operator without the false part of it. For one thing it would change it from a ternary operator to a binary operator

    I think the problem you are describing is just those people not understanding precedence.

  5. #5
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    Let me see if if i got it right.
    So basically you need three parts for the operator to work.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ternary means 3 so yeah

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Some gcc extensions allow variations on the theme, but it's such a low value feature that the standard usage just seems fine to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by arjunajay View Post
    Is it possible to use the Ternary operator without the colon( : ).
    i.e. with only one operand.
    Code:
    (condition)?
    instead of
    (condition)? true_stmt : false_stmt ;
    I've heard that its very low precedence causes it to return (condition) rather than true_stmt or false_stmt when used with any operator other than assignment operators.
    I have no idea what you're trying to do?
    The conditional operator is basically a condensed if/else statement. So instead of writing:
    Code:
    if ( condition )
    {
       return true_stmt;
    }
    else
    {
       return false_stmt;
    }
    You want it to do this?
    Code:
    if ( condition );
    That's just a useless conditional check...

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think what arjunajay wants is
    Code:
    (condition)?expr;
    You can achieve this with:
    Code:
    (condition && expr)
    assuming we don't want to assign the result.

    If you want to assign something, then you can do:
    Code:
    x = (condition)?x+1:x;
    All of these are of course not particularly good solutions - in general, you should make code obvious, and none of the above does that [although the last one is sort of acceptable].

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 12:24 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. Conditional operator ? :)
    By ER in forum C++ Programming
    Replies: 4
    Last Post: 11-29-2001, 03:34 AM