1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Ternary Operations outside of coding?

  1. Feb 21, 2013 #1
    I use the ternary operation in web development all the time, eg:
    x = y > z ? a : b

    Which reads, if x > y then a, otherwise b

    I realize that it is just a shorthand for if...else statements, but even still, are there any mathematical properties associated with it? I know inequalities have special properties - for example, dividing both sides by a negative.

    I recently picked up an elementary book on set theory and I keep thinking about this. It would be interesting to know that in certain cases, I can just skip the operation and automatically assign x to a-something
  2. jcsd
  3. Feb 22, 2013 #2


    Staff: Mentor

    There aren't any mathematical properties associated with this operator. As you note, it's just a short hand way of writing if ( ... ) then (...).

    Your explanation of your example is not quite right.

    x = y > z ? a : b

    means if y > z, then set x to a. Otherwise, set x to b.

    This is equivalent to the following C code:
    Code (Text):

    if (y > z)
       x = a;
       x = b;
    In answer to your question, no, you can't skip the comparison.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook