The conditional operator is ? :
. They are not two operators. Since it is ternary, it has two parts to separate, as you well observed, the condition, the value for true, and the value for false.
It's also called the ternary operator , but I do not like the term. If one day has another ternary, it causes confusion. And this name does not say what it does. It's bad terminology.
In a way it is a substitute for if
, at least when you just want to get a value according to the decision. Obviously it can not execute commands, it can only execute expressions. And if it gets too complicated, though it still works, it's unreadable. Especially if you have multiple conditions nested.
So use only when fits a simple expression and will not nest. It is common to use parentheses in the condition even when it is not necessary. Other times parentheses are used throughout the operator expression:
resultado = ((valor < 10) ? 50 : 0);
Of course in this case it is exaggeration, it is simple enough not to cause confusion. But if this expression were part of another expression, it would be more confusing without parentheses. And obviously in some cases it becomes mandatory to achieve what you want without having problems precedence .
I prefer this in simple cases, but you have to do it:
if (valor < 10)
resultado = 50;
else
resultado = 0;
See more in another answer . Language is different but works the same. There is this too .