GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing en.wikipedia.org — Ternary conditional operator - Wikipedia "?:" redirects here. For use as a binary operator, see Elvis operator.
"Conditional operator" redirects here. For the
|| and && operators, sometimes called conditional operators in Java and C#, see Short-circuit evaluation.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 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, The construct first appeared in CPL in 1963, as PatternsAssignmentThe 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: x: str = 'foo' if b else 'bar' instead of: x: str if b: x = 'foo' else: x = 'bar' In a language with block scope, a variable must be declared before the if-else statement. For example: std::string s; if (b) { s = "foo"; } else { s = "bar"; } Use of the conditional operator simplifies this: std::string s = b ? "foo" : "bar"; Furthermore, since initialization is now part of the declaration, rather than a separate statement, the identifier can be a constant. For example: const std::string s = b ? "foo" : "bar"; Case selectorThe conditional operator can be used for case selectors. For example: vehicle = arg == 'B' ? bus : arg == 'A' ? airplane : arg == 'T' ? train : arg == 'C' ? car : arg == 'H' ? horse : feet; VariationsThe 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 In almost all languages, the ternary operator is right associative, so that Furthermore, in all C-family languages and many others, the ternary conditional operator has low operator precedence. ExamplesAdaThe 2012 edition of Ada introduced conditional expressions (using 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 ALGOL 60ALGOL 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 68Both ALGOL 68's choice clauses (if and case clauses) support the following:
BashA 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 C familyThe following code in C assigns result = a > b ? x : y; Only the selected expression is evaluated. In this example, If
Furthermore, in C++, a conditional expression can be used as an lvalue, if both (foo ? bar : baz) = frink; Common LispAssignment 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)) dBASEIn dBase, the conditional function For example, to sort a list by the street name and then (in most cases) house number, one could type
at the dBASE III command prompt, and then copy or export the table. FortranAs 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 ) KotlinKotlin does not include the traditional val max = if (a > b) a else b LuaLua does not have a traditional conditional operator. However, the short-circuiting behavior of its var = cond and a or b This will succeed unless 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 -- in Luau var = if cond then a else b -- with elseif clause sign = if var < 0 then -1 elseif var == 0 then 0 else 1 Object Pascal extensionsPascal 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,[13] and in 2025 Delphi followed suit.[14] Oxygene supports case/switch statements, essentially a repeated if, as expressions evaluating to a value as well.[15] PythonPython has had a ternary conditional expression since release 2.5 (September 2006).[16] Python's conditional operator differs from the C-style result = x if a > b else y This form invites considering RubyRuby has supported the status = age >= 18 ? "adult" : "minor" Ruby's greeting = logged_in? ? "Welcome back!" : "Please sign in." The community style guide recommends using spaces around both RustAs an expression-oriented programming language, all Rust conditionals are expressions, not statements, so the Note the lack of semi-colons in the code below compared to an imperative 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 let y = match x { 5 => 10, _ => 15, }; SmalltalkEvery expression (message send) has a value. Thus |x y| x := 5. y := (x == 5) ifTrue:[10] ifFalse:[15]. SQLThe SQL 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 < b THEN y ELSE z END) AS CONDITIONAL_EXAMPLE FROM tab; Visual BasicVisual Basic provides a ternary conditional function, 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 Dim name As String = If(person Is Nothing, "", person.Name) See also
References
External links |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |