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 operation
(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!
==Computer science== In computer science, a ternary operator is an [[operator (computer programming)|operator]] that takes three arguments (or operands).<ref name="MDM nmve"/> The arguments and result can be of different types. Many [[programming language]]s that use [[C syntax|C-like syntax]]<ref>{{cite web |last1=Hoffer |first1=Alex |title=Ternary Operator |website=Cprogramming.com |url=https://www.cprogramming.com/reference/operators/ternary-operator.html |accessdate=20 February 2017}}</ref> feature a ternary operator, <code>[[?:]]</code>, which defines a [[conditional (programming)#If expressions|conditional expression]]. In some languages, this operator is referred to as the ''conditional operator''. In [[Python (programming language)|Python]], the ternary conditional operator reads <code>x if C else y</code>. Python also supports ternary operations called [[array slicing]], e.g. <code>a[b:c]</code> return an array where the first element is <code>a[b]</code> and last element is <code>a[c-1]</code>.<ref>{{cite web |title=6. Expressions — Python 3.9.1 documentation |url=https://docs.python.org/3/reference/expressions.html |access-date=2021-01-19 |website=docs.python.org}}</ref> [[OCaml]] expressions provide ternary operations against records, arrays, and strings: <code>a.[b]<-c</code> would mean the string <code>a</code> where index <code>b</code> has value <code>c</code>.<ref>{{cite web |title=The OCaml Manual: Chapter 11 The OCaml language: (7) Expressions |website=ocaml.org |url=https://v2.ocaml.org/manual/expr.html |access-date=2023-05-03}}</ref> The [[multiply–accumulate operation]] is another ternary operator. Another example of a ternary operator is ''between'', as used in [[SQL]]. The [[Icon (programming language)|Icon programming language]] has a "to-by" ternary operator: the expression <code>1 to 10 by 2</code> generates the [[parity (mathematics)|odd]] integers from 1 through 9. In Excel formulae, the form is =if(C, x, y).
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)