Ada The 2012 edition of
Ada introduced conditional expressions (using and ), 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). Pay_per_Hour := (if Day = Sunday then 12.50 else 10.00); When the value of an
if_expression is itself of Boolean type, then the part may be omitted, the value being True. Multiple conditions may chained using .
ALGOL 60 ALGOL 60 introduced
conditional expressions (ternary conditionals) to
imperative programming languages. This conditional statement: integer opening_time; if day = Sunday then opening_time := 12; else opening_time := 9; Can be rewritten with the conditional operator: integer opening_time; opening_time := if day = Sunday then 12 else 9;
ALGOL 68 Both
ALGOL 68's
choice clauses (if and case clauses) support the following: ; Single if choice clause: or a brief form: ( condition | statements | statements ) ; Chained if choice clause: or a brief form: ( condition1 | statements |: condition2 | statements | statements ).
Bash A true ternary operator exists for arithmetic expressions: ((result = condition ? value_if_true : value_if_false)) For strings there are workarounds, like e.g.: result=$(if condition ; then echo "value_if_true" ; else echo "value_if_false" ; fi) Where 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 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. result = a > b ? x : y; 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: • In C++, the usual arithmetic type conversions are performed to convert x and y to a common type. If both are pointer or reference types, or one is a pointer type and the other is a constant expression evaluating to 0, pointer or reference conversions are performed to convert them to a common type. • In C#, if one expression is implicitly convertible to the type of the other, that type is used. Otherwise, a compile-time error occurs. • In dynamically typed languages, the evaluated expression has the type of the selected expression. Furthermore, in C++, a conditional expression can be used as an
lvalue, if both x and y are lvalues, though this is rarely used in practice: (foo ? bar : baz) = frink;
Common Lisp Assignment using a conditional expression in
Common Lisp: (setq result (if (> a b) x y)) Alternative form: (if (> a b) (setq result x) (setq result y))
dBASE In
dBase, the conditional function iif(, , ) is called "Immediate IF". It uses shortcut evaluation (it only evaluates one of or ). For example, to sort a list by the street name and then (in most cases) house number, one could type to indexfile at the dBASE III command prompt, and then copy or export the table.
Fortran As part of the Fortran-90 Standard, the ternary operator was added to
Fortran as the intrinsic function : variable = merge(x,y,a>b) Note that both x and y are evaluated before the results of one or the other are returned from the function. Here, x is returned if the condition holds true and y otherwise. Fortran-2023 added conditional expressions which evaluate one or the other of the expressions based on the conditional expression: variable = ( a > b ? x : y )
Kotlin Kotlin does not include the traditional ternary operator, however, an can be used as an expression that can be assigned, achieving the same results. val max = if (a > b) a else b
Lua Lua does not have a traditional conditional operator. However, the short-circuiting behavior of its and operators allows the emulation of this behaviour. The following is equivalent to: var = cond ? a : b. var = cond and a or b This will succeed unless is logically false; in this case, the expression will always result in . This can result in some surprising behavior if ignored. There are also other variants that can be used, but they're generally more verbose: var = ( { [true] = a, [false] = b } )[not not cond] Luau, a dialect of Lua, has ternary expressions that look like if statements, but unlike them, they have no keyword, and the clause is required. One may optionally add clauses. It's designed to replace the idiom and is expected to work properly in all cases. -- in Luau var = if cond then a else b -- with elseif clause sign = if var
Object Pascal extensions Pascal was both a simplification and extension of ALGOL 60, but it removed conditional expressions.
RemObjects Oxygene added a ternary operator to Object Pascal in approximately 2011, and in 2025 Delphi followed suit. Oxygene supports
case/switch statements, essentially a repeated if, as expressions evaluating to a value as well.
Python Python has had a ternary conditional expression since release 2.5 (September 2006). Python's conditional operator differs from the C-style operator in the order of its operands. The general form is: result = x if a > b else y This form invites considering as the normal value and as an exceptional case. Unlike Python conditional statements, the elif clause is not supported, but nested conditional expressions are allowed.
Rust As an
expression-oriented programming language, all Rust conditionals are expressions, not statements, so the if
expr1 else
expr2 syntax behaves like the C-style ternary operator. Earlier versions of the language had a operator but it was removed due to duplication with . Note the lack of semi-colons in the code below compared to an imperative ... block, and the semi-colon at the end of the assignment to . let x = 5; let y = if x == 5 { 10 } else { 15 }; This could also be written as: let y = if x == 5 { 10 } else { 15 }; Curly braces are mandatory in Rust conditional expressions. You could also use a expression: let y = match x { 5 => 10, _ => 15, };
Smalltalk Every expression (message send) has a value. Thus can be used: x := 5. y := (x == 5) ifTrue:[10] ifFalse:[15].
SQL The SQL expression is a generalization of the ternary operator. Instead of one conditional and two results,
n conditionals and
n+1 results can be specified. With one conditional it is equivalent (although more verbose) to the ternary operator: SELECT (CASE WHEN a > b THEN x ELSE y END) AS CONDITIONAL_EXAMPLE FROM tab; This can be expanded to several conditionals: SELECT (CASE WHEN a > b THEN x WHEN a
Visual Basic Visual Basic provides a ternary conditional function, , as shown in the following code: Dim opening_time As Integer = IIf((day = SUNDAY), 12, 9) As a function, the values of the three arguments are evaluated before the function is called. To avoid evaluating the expression that is not selected, the keyword was added (in Visual Basic .Net 9.0) as a true ternary conditional operator. This allows the following code to avoid an exception if it were implemented with instead: Dim name As String = If(person Is Nothing, "", person.Name) ==See also==