Topic summary

Ternary conditional operator

In computer programming, the ternary conditional operator is a conditional expression with three parts: the Boolean condition, the then-expression, and the else-expression. If the condition is true, the then-expression is evaluated; otherwise, the else-expression is evaluated; and the value is returned. Thus it is a non-strict operator, like other conditional expressions. It is also called a conditional operator, ternary if, immediate if, or inline if (iif).

Although a ternary operator in general is any operator with three arguments, the three-argument conditional operator is the only common one in programming, so it is loosely called the ternary operator.

C introduced the syntax a ? b : c, read as "if a then b otherwise c", which is widely used by other languages with C-like syntax. Some languages use functional syntax: in Visual Basic, it is written If(a, b, c).

Many languages support conditional expressions with two, three, or more clauses. Languages following ALGOL-like syntax often support them with the same syntax as conditional statements, if a then b else c.

The construct first appeared in CPL in 1963, as a → b, c.