Like Tree1Likes
  • 1 Post By iMalc

Ternary Operator to choose which operator to use

This is a discussion on Ternary Operator to choose which operator to use within the C Programming forums, part of the General Programming Boards category; Is there a way to use the ternary operator to choose between two operators? Example: Code: BOOL greaterThan = true; ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    1

    Ternary Operator to choose which operator to use

    Is there a way to use the ternary operator to choose between two operators?

    Example:

    Code:
    BOOL greaterThan = true;
    
    int x = 5;
    int y = 10;
    
    if(x (greaterThan? > : <) y)
    {
        do something if greaterThan is true, and x > y, or if greaterThan is false, and x < y
    }

  2. #2
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    You literally spelled out how to do this:

    Code:
    if ((greaterThan && x > y) || (!greaterThan && x < y))

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In the expression you don't actually need the greaterThan as factor, unless it relates to some part of your program you aren't showing...

    The simplest solution is like this...
    Code:
    if (x > y)
      { DoThisThing(); }  // x is greater than y
    else if (y > x)
      { DoThatThing(); }  // y is greater than x
    else
      { DoTheOtherThing(); }  // y equals x
    Terniary solutions or those with a single if()-eles statement will let the case of x = y fall right through.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    The expresions used between the ? and the :, or after the :, must be able to be evaluated on their own.
    What you had doesn't work for the same reason that
    Code:
    if ( < )
    doesn't work.

    This would have worked:
    Code:
    if (greaterThan ? x > y : x < y)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    Quote Originally Posted by iMalc View Post
    This would have worked:
    Code:
    if (greaterThan ? x > y : x < y)
    Judging by what he wrote in plain text I don't think that's actually what he wanted to do... and if it is he'll definitely have to work on putting his ideas into words, because that's fundamental to programming.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    Quote Originally Posted by dennis.cpp View Post
    Judging by what he wrote in plain text I don't think that's actually what he wanted to do... and if it is he'll definitely have to work on putting his ideas into words, because that's fundamental to programming.
    I'm perplexed as to why you think that.

    His statement "do something if greaterThan is true, and x > y, or if greaterThan is false, and x < y" is perfectly understandable, and the expressions you and I wrote are exactly equivalent.
    Generate a truth table if you don't believe me. Either your boolean expression rearranging isn't quite second nature yet as it is for me after all these years, or you must misunderstand how the ternary operator works.

    Combine the code he actually wrote and the description, and it is 100% clear that he was after exactly what I posted.
    Last edited by iMalc; 06-27-2011 at 12:20 AM.
    dennis.cpp likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    language hopper dennis.cpp's Avatar
    Join Date
    Jan 2011
    Location
    Herborn, Hesse, Germany
    Posts
    197
    @iMalc - You're perfectly right, my fault. Sorry.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    Not possible to do exacly what OP wanted, but how about
    x < y ^ greaterThan
    ... which flips the logic outcome depending on 'greaterThan'.
    There is still the issue of how cases <=, >=, ==, != are handled exactly. But this was not specified to begin with.
    Last edited by nonoob; 06-27-2011 at 01:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bug in ternary operator ?:
    By wiglaf in forum C Programming
    Replies: 14
    Last Post: 03-31-2011, 10:26 PM
  2. Ternary Operator and Saturation Arithmetic
    By enkwiringmindz in forum C Programming
    Replies: 9
    Last Post: 04-17-2010, 04:53 PM
  3. Is the PHP ternary operator broken?
    By mike_g in forum Tech Board
    Replies: 13
    Last Post: 07-18-2009, 05:04 PM
  4. Ternary operator expression
    By rits in forum C Programming
    Replies: 9
    Last Post: 03-13-2009, 08:02 AM
  5. ternary operator
    By abhijith gopal in forum C Programming
    Replies: 37
    Last Post: 07-10-2006, 11:58 AM

Tags for this Thread


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