Search code examples
c++ternary-operator

"Do nothing" in the else-part of the ternary operator?


What's the standard line to add to the ternary operator in order to do nothing if the condition is not met?

Example:

int a = 0;
a > 10 ? a = 5 : /*do nothing*/;

Using a seems to do the trick, but I am wondering if there is a more generally accepted way.


Solution

  • That will do it:

    a = a > 10 ? 5 : a;
    

    or simply:

    if (a > 10) a = 5;