User:Ushkin N/Comparison of programming languages/Operators/Ternary operator

See ?: or ternary operator (BCPL term) for the background.

The conditional operator's most common usage is to make a terse simple conditional assignment statement. For example, if we wish to implement some C code to change a shop's normal opening hours from 9 o'clock to 12 o'clock on Sundays, we may use instead of the more verbose The two forms are nearly equivalent. Keep in mind that the ?: is an expression and if-then-else is a statement. Note that neither the true nor false portions can be omitted from the conditional operator without an error report upon parsing. This contrasts with if-then-else statements, where the else clause can be omitted.

Most of the languages emphasizing functional programming don't need such an operator as their regular conditional expression(s) is an expression in the first place e.g. the Scheme expression (if (> a b) a b) is equivalent in semantics to the C expression (a > b) ? a : b. This is also the case in many imperative languages, starting with ALGOL where it is possible to write result := if a > b then a else b, or Smalltalk (result := (a > b) ifTrue: [ a ] ifFalse: [ b ]) or Ruby (result = if a > b then a else b end, although result = a > b ? a : b works as well).

Note that some languages may evaluate 'both' the true- and false-expressions, even though only one or the other will be assigned to the variable. This means that if the true- or false-expression contain a function call, that function may be called and executed (causing any related side-effects due to the function's execution), regardless of whether or not its result will be used. Programmers should consult their programming language specifications or test the ternary operator to determine whether or not the language will evaluate both expressions in this way. If it does, and this is not the desired behaviour, then an if-then-else statement should be used.

Ada
The 2012 edition of Ada has 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).

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 else part may be omitted, the value being True. Multiple conditions may chained using elsif.

ALGOL 68
Both ALGOL 68's choice clauses (if and the case clauses) provide the coder with a choice of either the "bold" syntax or the "brief" form. if condition then statements [ else statements ] fi "brief" form: ( condition | statements | statements )
 * Single if choice clause:

if condition1 then statements elif condition2 then statements [ else statements ] fi "brief" form: ( condition1 | statements |: condition2 | statements | statements )
 * Chained if choice clause:

C
A traditional if-else construct in C, Java and JavaScript is written:

This can be rewritten as the following statement:

As in the if-else construct only one of the expressions 'x' and 'y' is evaluated. This is significant if the evaluation of 'x' or 'y' has side effects. The behaviour is undefined if an attempt is made to use the result of the conditional operator as an lvalue.

A GNU extension to C allows omitting the second operand, and using implicitly the first operand as the second also: The expression is equivalent to except that if x is an expression, it is evaluated only once. The difference is significant if evaluating the expression has side effects. This shorthand form is sometimes known as the Elvis operator in other languages.

C++
In C++ there are conditional assignment situations where use of the if-else statement is impossible, since this language explicitly distinguishes between initialization and assignment. In such case it is always possible to use a function call, but this can be cumbersome and inelegant. For example, to pass conditionally different values as an argument for a constructor of a field or a base class, it is impossible to use a plain if-else statement; in this case we can use a conditional assignment expression, or a function call. Bear in mind also that some types allow initialization, but do not allow assignment, or even that the assignment operator and the constructor do totally different things. This last is true for reference types, for example:

In this case there is no possibility of using an if-else statement in place of the ?: operator (Although we can replace the use of ?: with a function call, inside of which can be an if-else statement).

Furthermore, the conditional operator can yield an lvalue, i.e. a value to which another value can be assigned. Consider the following example:

In this example, if the boolean expression argc > 1 yields the value true in line 5, the value 1 is assigned to the variable a, otherwise, it is assigned to b.

C#
In C#, if condition is true, first expression is evaluated and becomes the result; if false, the second expression is evaluated and becomes the result. As with Java only one of two expressions is ever evaluated.

CFML
Roughly 50% of the time the  expression with return 1 (true) or 0 (false); meaning result will take the value "heads" or "tails" respectively.

Lucee, Railo, and ColdFusion 11-specific
Lucee, Railo, and ColdFusion 11 also implement the null-coalescing (or "Elvis") operator,  which will return the value of the expression if it is not-null, otherwise the specified default.

Syntax:

Example:

The function  will return   roughly 50% of the time, otherwise will not return anything. If  returns "value",   will take that value, otherwise will take the value "default".

CoffeeScript
Example of using this operator in CoffeeScript:

Returns "false value".

Common Lisp
Assignment using a conditional expression in Common Lisp:

Alternative form:

Delphi
In Delphi the  function can be used to achieve the same as. If the  library is used, the   function returns a numeric value such as an Integer, Double or Extended. If the  library is used, this function can also return a string value.

Using

Using the  library

Usage example: Unlike a true ternary operator however, both of the results are evaluated prior to performing the comparison. For example, if one of the results is a call to a function which inserts a row into a database table, that function will be called whether or not the condition to return that specific result is met.

Fortran
With the additions to the code in the 1995 release, the ternary operator was added to the Fortran compiler as the intrinsic function merge:

Haskell
The built-in if-then-else syntax is inline: the expression has type

The base library also provides the function Data.Bool.bool:

In both cases, no special treatment is needed to ensure that only the selected expression is evaluated, since Haskell is non-strict by default.

Java
In Java this expression evaluates to:

Note that Java, in a manner similar to C#, only evaluates the used expression and will not evaluate the unused expression.

JavaScript
The conditional operator in JavaScript has the same syntax and precedence structure as in the other BCPL-derived variants, where the operator returns the value of the expression, and never a reference.

Just like C# and Java, the expression will only be evaluated if, and only if, the expression is the matching one for the condition given; the other expression will not be evaluated.

Lua
Lua doesn't have a traditional conditional operator. However, the short-circuit behaviour of its "and" and "or" operators allows the emulation of this behaviour:

This will succeed unless "a" is logically false (false or nil); in this case, the expression will always result in b. This can result in some surprising behaviour if ignored.

MySQL
This syntax is not SQL standard; it is MySQL specific.

Oracle SQL
While Oracle doesn't provide an explicit conditional operator, it does have a variadic functional counterpart which operates similarly to a switch statement and can be used to emulate the conditional operator when testing for equality (more complicated logic can be encapsulated in a switch statement).

The  function is, today, deprecated in favour of Oracle's full switch statement,. This can be used in both Oracle SQL queries as well as PL/SQL blocks, whereas  can only be used in the former.

Perl
A traditional if-else construct in Perl or PHP is written: Rewritten to use the conditional operator:

Unlike C, perl allows the use of the conditional expression as an Lvalue, e.g. will assign '$result' to either '$x' or '$y' depending on the logical expression.

Perl 6
Uses double "?" symbols and double "!" instead of ":"

PHP
A simple PHP implementation is this:

Due to an unfortunate design of the language grammar, the conditional operator in PHP is left associative in contrast to other languages, thus given a value of T for arg, the PHP code in the following example would yield the value horse instead of train as one might expect:

The reason is that nesting two conditional operators produces an oversized condition with the last two options as its branches: c1 ? o1 : c2 ? o2 : o3 is really ((c1 ? o1 : c2) ? o2 : o3). This is acknowledged and will probably not change. To avoid this, nested parenthesis are needed, as in this example:

This will produce the result of train being printed to the output, analogous to a right associative conditional operator.

PHP 5.3
Since PHP 5.3 there is a short-hand of the conditional operator, sometimes referred to as the "Elvis Operator". The syntax for this short-hand is below:

Python
Though it had been delayed for several years by disagreements over syntax, an operator for a conditional expression in Python was approved as Python Enhancement Proposal 308 and was added to the 2.5 release in September 2006. Python's conditional operator differs from the common  operator in the order of its operands. The general form is:

This form invites considering  as the normal value and   as an exceptional case. One can use the syntax as a workaround for code that also needs to run under Python versions before 2.5. Note that operands are lazily evaluated, it is possible to remove the lambdas and function calls but the operands will be eagerly evaluated which isn't consistent with the conditional operator of most other languages, e.g. by indexing a tuple, or using an explicitly constructed dictionary:

A less reliable but simpler to read alternative is to abuse the  and   operators and write but this code would break if  could be a "falsy" value (,  ,  , an empty sequence or collection, …) as the expression would return   (whether it was truthy or falsy) instead of the (falsy). A possible workaround is to make  and   lists or tuples, so they are never falsy, and then grab the first element of the resulting sequence as in the following or

NB when wrapping Python's conditional construct into a utility function, the unalterably eager nature of the more intuitive language construct for side-effect functions similar results from as the correct call would be

however the python 2.5 construct is safer; calling the construct directly works more intuitively clearly the reason being that in the case of  the functions are called when sent as parameters rather than when returned from func

R
The traditional if-else construct in R (which is an implementation of S) is:

If there is only one statement in each block, braces can be omitted, like in C:

The code above can be written in the following non-standard condensed way:

There exists also the function  that allows rewriting the expression above as:

The  function is automatically vectorized. For instance:

Ruby
Example of using this operator in Ruby:

Returns "false value".

A traditional if-else construct in Ruby is written:

This could also be written as:

These can be rewritten as the following statement:

Rust
Being an expression oriented language, rust's existing  syntax can behave as the traditional   ternary operator does. Earlier versions of the language did have the  operator but it was removed due to duplication with.

Note the lack of semi-colons in the code below compared to a more declarative if...else block, and the semi-colon at the end of the assignment to y.

Scheme
Same as in Common Lisp. Every expression has a value. Thus the builtin  can be used:

Smalltalk
Every expression (message send) has a value. Thus  can be used:

TestStand
In a National Instruments TestStand expression, if condition is true, the first expression is evaluated and becomes the output of the conditional operation; if false, the second expression is evaluated and becomes the result. Only one of two expressions is ever evaluated. For example: Sets the UUTIndex local variable to 3 if TestSocket.Index is 3, otherwise it sets UUTIndex to 0.

Similar to other languages, first_expression and second_expression do not need to be autonomous expressions, allowing the operator to be used for variable assignment:

Verilog
Verilog is technically a hardware description language, not a programming language though the semantics of both are very similar. It uses the  syntax for the ternary operator.

This is equivalent to the more verbose Verilog code

Visual Basic
Visual Basic doesn't use  per se, but has a very similar implementation of this shorthand   statement. Using the first example provided in this article, it can do:

In the above example,  is a ternary function, but not a ternary operator. As a function, the values of all three portions are evaluated before the function call occurs. This imposed limitations, and in Visual Basic .Net 9.0, released with Visual Studio 2008, an actual conditional operator was introduced, using the  keyword instead of. This allows the following example code to work:

Using,   would be evaluated even if person is   (Nothing), causing an exception. With a true short-circuiting conditional operator,  is not evaluated unless person is not.

Visual Basic Version 9 has added the operator  in addition to the existing   function that existed previously. As a true operator, it does not have the side effects and potential inefficiencies of the  function.

The syntaxes of the tokens are similar:  vs. As mentioned above, the function call has significant disadvantages, because the sub-expressions must all be evaluated, according to Visual Basic's evaluation strategy for function calls and the result will always be of type variant (VB) or object (VB.NET). The operator however does not suffer from these problems as it supports conditional evaluation and determines the type of the expression based on the types of its operands.