From Seo Wiki - Search Engine Optimization and Programming Languages

Jump to: navigation, search


In computer programming, ?: is a ternary operator that is part of the syntax for a basic conditional expression in several programming languages. It is commonly referred to as the conditional operator.

It originally comes from BCPL, whose equivalent syntax for e1 ? e2 : e3 was e1 -> e2, e3[1]. Languages derived from BCPL tend to feature this operator.


Conditional assignment

?: is used as follows:

condition ? value if true : value if false 

The condition is evaluated true or false as a Boolean expression. On the basis of the evaluation of the Boolean condition, the entire expression returns value if true if condition is true, but value if false otherwise. Usually the two sub-expressions value if true and value if false must have the same type, which determines the type of the whole expression. The importance of this type-checking lies in the operator's most common use—in conditional assignment statements. In this usage it appears as an expression on the right side of an assignment statement, as follows:

variable = condition ? value if true : value if false

The ?: operator is similar to the way conditional expressions (if-then-else constructs) work in functional programming languages, like Scheme, ML, and Haskell, since if-then-else forms an expression instead of a statement in those languages.


This ternary 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 opening hours to 12 o'clock in weekends, and 9 o'clock on weekdays, we may use

int opening_time = (day == WEEKEND) ? 12 : 9;

instead of the more verbose

int opening_time;
if (day == WEEKEND)
    opening_time = 12;
    opening_time = 9;

The two forms are nearly equivalent. Keep in mind that the ?: is an expression and if-then-else is a statement. Note that neither value if true nor value if false expressions can be omitted from the ternary operator without an error report upon parsing. This contrasts with if..else statements, where the else clause can be omitted.

C Variants

A GNU extension to C allows the second operand to be omitted, and the first operand is implicitly used as the second as well:

a = x ? : y;

The expression is equivalent to

a = x ? x : y;

except that if x is an expression, it is evaluated only once. The difference is significant if evaluating the expression has side effects.

C# provides similar functionality with its coalescing operator

a= x ?? y;

(Unlike the above usage of "x ?: y", ?? will only test if x is non-null, as opposed to non-false.)


In C++ there are conditional assignment situations where use of the if-else statement is not possible, 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, if you want to pass conditionally different values as an argument for a constructor of a field or a base class, it is not possible to use a plain if-else statement; in this case we can use a conditional assignment expression, or a function call. Mind also that some types allow initialization, but do not allow assignment, or even the assignment operator does totally different things than the constructor. The latter is true for reference types, for example:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char** argv)
    string name;
    ofstream fout;
    if (argc > 1 && argv[1])
        name = argv[1];
        fout.open(name.c_str(), ios::out | ios::app);
    ostream& sout = name.empty() ? cout : fout;

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

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

  1. #include <iostream>
  2. int main () {
  3.    int a=0, b=0;
  5.    const bool cond = ...;
  6.    (cond ? a : b) = 1;
  7.    std::cout << "a=" << a << ','
  8.              << "b=" << b << '\n';
  9. }

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


The Python programming language uses a different syntax for this operator:

value_when_true if condition else value_when_false

This feature is not available for Python versions prior to 2.5, however. The Python programming FAQ mentions several possible workarounds for these versions, with varying degrees of simplicity versus semantic exactness (for example, regarding the short-circuit evaluation property of ?:). The most complete alternative, that has all the same properties as ?: and still works in Python versions prior to 2.5, would be:

(condition and [value_when_true] or [value_when_false])[0]

Note that value_when_true and value_when_false must be put in lists, because the expression would otherwise return value_when_false if value_when_true evaluated to False. It should be noted that this construct uses more memory and executes more slowly than simple if/else blocks or a native ternary operator. The linked FAQ that mentions it describes the structure as "ugly," and specifically recommends using other techniques.

Visual Basic .NET

Although it doesn't use ?: per se, Microsoft Visual Basic .NET has a very similar implementation of this shorthand if...else statement. Using the first example provided in this article, you can do:

Dim opening_time As Integer = IIf((day = WEEKEND), 12, 9)
'general syntax is IIf(condition, value_if_true, value_if_false)

In the above example, IIf is a function, and not an actual 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 ternary operator was introduced, using the If keyword instead of IIf. This allows the following example code to work:

Dim name As String = If(person Is Nothing, "", person.Name)

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


$arg = "T";
$vehicle = ( ( $arg == 'B' ) ? 'bus' : 
             ( $arg == 'A' ) ? 'airplane' : 
	     ( $arg == 'T' ) ? 'train' : 
	     ( $arg == 'C' ) ? 'car' : 
	     ( $arg == 'H' ) ? 'horse' : 
             'feet' );
echo $vehicle;

Due to an unfortunate error in the language grammar, the implementation of ?: in PHP uses the incorrect associativity when compared to other languages, and given a value of T for arg, the PHP equivalent of the above example would yield the value horse instead of train as one would expect. To avoid this, nested parenthesis are needed, as in this example:

$arg = "T";
$vehicle = ($arg == 'B') ? 'bus' :
          (($arg == 'A') ? 'airplane' :
          (($arg == 'T') ? 'train' :
          (($arg == 'C') ? 'car' :
          (($arg == 'H') ? 'horse' :
echo $vehicle;

This will produce the correct result of train being printed to the screen.

CFML (Railo only)

arg = "T";
vehicle = ( ( arg == 'B' ) ? 'bus' : 
            ( arg == 'A' ) ? 'airplane' : 
            ( arg == 'T' ) ? 'train' : 
            ( arg == 'C' ) ? 'car' : 
            ( arg == 'H' ) ? 'horse' : 
                             'feet' );

Result type

Clearly the type of the result of the ?: operator must be in some sense the type unification of the types of its second and third operands. In C this is accomplished for numeric types by arithmetic promotion; since C does not have a type hierarchy for pointer types, pointer operands may only be used if they are of the same type (ignoring type qualifiers) or one is void or NULL. It is undefined behaviour to mix pointer and integral or incompatible pointer types; thus

number = spell_out_numbers ? "forty-two" : 42;

will result in a compile-time error in most compilers.

?: in style guidelines

Some corporate programming guidelines list the use of the conditional operator as bad practice because it can harm readability and long-term maintainability.[citation needed] Conditional operators are widely used and can be useful in certain circumstances to avoid the use of an if statement, either because the extra verbiage would be too lengthy or because the syntactic context does not permit a statement. For example:

#define MAX(a, b) (((a)>(b)) ? (a) : (b))


 for (i = 0; i < MAX_PATTERNS; i++)
    c_patterns[i].ShowWindow(m_data.fOn[i] ? SW_SHOW : SW_HIDE);

(The latter example uses the Microsoft Foundation Classes Framework for Win32.)

When properly formatted, the conditional operator can be used to write simple and coherent case selectors. For example:

vehicle = arg == 'B' ? bus :
          arg == 'A' ? airplane :
          arg == 'T' ? train :
          arg == 'C' ? car :
          arg == 'H' ? horse :

Appropriate use of the conditional operator in a variable assignment context reduces the probability of a bug from a faulty assignment as the assigned variable is stated just once as opposed to multiple times.

See also



ja:条件演算子 pl:Operator warunkowy ru:Тернарная условная операция zh:条件运算符

Personal tools

Served in 0.829 secs.