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.
Patterns
Assignment The value of the operator can be assigned to a variable. For a weakly typed language, the data type of the selected value may determine the type of the assigned value. For a strongly typed language, both value expressions must evaluate to a type that is compatible with the target variable. The operator is similar to the way conditional expressions (if-then-else) work in functional programming languages, like Scheme, ML, Haskell, and XQuery, since if-then-else forms an expression instead of a statement in those languages. The operator allows for initializing a variable via a single statement which otherwise might require multiple statements. Use in variable assignment reduces the probability of a bug from a faulty assignment as the assigned variable is stated only once. For example, in Python:
instead of:
In a language with block scope, a variable must be declared before the if-else statement. For example:
Use of the conditional operator simplifies this:
Furthermore, since initialization is now part of the declaration, rather than a separate statement, the identifier can be a constant. For example:
Case selector The conditional operator can be used for case selectors. For example:
Variations The syntax and semantics of the operator vary by language. Major differences include whether the expressions can have side effects and whether the language provides short-circuit evaluation semantics, whereby only the selected expression is evaluated. If a language supports expressions with side effects but does not specify short-circuit evaluation, then a further distinction exists about which expression evaluates first. If no order is guaranteed, a distinction exists about whether the result is then classified as indeterminate (the value obtained from some order) or undefined (any value at all at the whim of the compiler in the face of side effects, or even a crash). If a language does not permit side-effects in expressions (common in functional languages), then the order of evaluation has no value semantics – though it may yet bear on whether an infinite recursion terminates, or have other performance implications (in a functional language with match expressions, short-circuit evaluation is inherent, and natural uses for the ternary operator arise less often, so this point is of limited concern). For these reasons, in some languages the statement form r = condition ? expr1 : expr2 can have subtly different semantics than the block conditional form if (condition) { r = expr1; } else { r = expr2; }. In almost all languages, the ternary operator is right associative, so that a == 1 ? "one" : a == 2 ? "two" : "many" evaluates intuitively as a == 1 ? "one" : (a == 2 ? "two" : "many"). This means it can be chained similarly to an if ... else if ... else if ... else chain. The main exception is PHP, in which it was left-associative (such that the same expression evaluates to (a == 1 ? "one" : a == 2) ? "two" : "many", which is rarely what the programmer expects) prior to version 8, and is non-associative thereafter. Furthermore, in all C-family languages and many others, the ternary conditional operator has low operator precedence.
Examples
Ada The 2012 edition of Ada introduced conditional expressions (using if and case), as part of an enlarged set of expressions including quantified expressions and expression functions. The Rationale for Ada 2012 states motives for Ada not having had them before, as well as motives for now adding them, such as to support "contracts" (also new).
When the value of an if_expression is itself of Boolean type, then the else part may be omitted, the value being True. Multiple conditions may chained using elsif.
ALGOL 60 ALGOL 60 introduced conditional expressions (ternary conditionals) to imperative programming languages. This conditional statement:
Can be rewritten with the conditional operator:
ALGOL 68 Both ALGOL 68's choice clauses (if and case clauses) support the following:
Single if choice clause if condition then statements [ else statements ] fi or a brief form: ( condition | statements | statements ) Chained if choice clause if condition1 then statements elif condition2 then statements [ else statements ] fi or a brief form: ( condition1 | statements |: condition2 | statements | statements ).
Bash A true ternary operator exists for arithmetic expressions:
For strings there are workarounds, like e.g.:
Where condition can be any bash command. When it exits with success, the first echo command is executed, otherwise the second one is executed.
C family The following code in C assigns result to the value of x if a > b, and otherwise to the value of y. This is the same syntax as in many related languages including C++, Java, JavaScript, and Dart.
Only the selected expression is evaluated. In this example, x and y require no evaluation, but they can be expressions with side effects. Only the side-effect for the selected expression value will occur. If x and y are of the same data type, the conditional expression generally has that type. Otherwise, the rules governing the resulting data type vary a little between languages:
… excerpt ends here. Continue reading the full article.
