Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Ternary conditional operator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Variations== The detailed semantics of "the" ternary operator as well as its syntax differs significantly from language to language. A top level distinction from one language to another is whether the expressions permit [[side effect (computer science)|side effects]] (as in most procedural languages) and whether the language provides [[short-circuit evaluation]] semantics, whereby only the selected expression is evaluated (most standard operators in most languages evaluate all arguments). If the language supports expressions with side effects but does not specify short-circuit evaluation, then a further distinction exists about which expression evaluates first—if the language guarantees any specific order (bear in mind that the conditional also counts as an expression). Furthermore, 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 behavior|undefined]] (any value at all at the whim of the compiler in the face of side effects, or even a crash). If the 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 {{code|1=variable = condition ? expr1 : expr2;}} can have subtly different semantics than the block conditional form {{code|1=if (condition) { variable = expr1; } else { variable = expr2; } }} (in the C language—the syntax of the example given—these are in fact equivalent). The associativity of nested ternary operators can also differ from language to language. In almost all languages, the ternary operator is [[right associative]] so that {{code|1=a == 1 ? "one" : a == 2 ? "two" : "many"}} evaluates intuitively as {{code|1=a == 1 ? "one" : (a == 2 ? "two" : "many")}}, but [[PHP]] in particular is notoriously left-associative,<ref>{{cite web |url=http://phpsadness.com/sad/30|title=Ternary operator associativity|last1=Wastl |first1=Eric |website=phpsadness.com |publisher=PHP Sadness |access-date=20 September 2017}}</ref> and evaluates as follows: {{code|1=(a == 1 ? "one" : a == 2) ? "two" : "many"}}, which is rarely what any programmer expects. (The given examples assume that the ternary operator has low [[operator precedence]], which is true in all C-family languages, and many others.)
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)