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!
{{Short description|Conditional operator in computer programming}} {{Redirect|?:|use as a binary operator|Elvis operator}} {{mergefrom|conditional operator|discuss=Talk:Conditional operator#Related Article|date=February 2025}} In [[computer programming]], the '''ternary conditional operator''' is a [[ternary operator]] that is part of the syntax for basic [[conditional (programming)|conditional expressions]] in several [[programming language]]s. It is commonly referred to as the '''conditional operator''', '''conditional expression''', '''ternary if''', or '''inline if''' (abbreviated '''iif'''). An expression {{code|if a then b else c}} or {{code|a ? b : c}} evaluates to {{code|b}} if the value of {{code|a}} is true, and otherwise to {{code|c}}. One can read it aloud as "if a then b otherwise c". The form {{code|a ? b : c}} is the most common, but alternative syntaxes do exist; for example, [[Raku (programming language)|Raku]] uses the syntax {{code|a ?? b !! c}} to avoid confusion with the infix operators {{code|?}} and {{code|!}}, whereas in [[Visual Basic .NET]], it instead takes the form {{code|If(a, b, c)}}. It originally comes from [[CPL (programming language)|CPL]], in which equivalent syntax for <code>''e''<sub>1</sub> ? ''e''<sub>2</sub> : ''e''<sub>3</sub></code> was <code>''e''<sub>1</sub> → ''e''<sub>2</sub>, ''e''<sub>3</sub></code>.<ref>{{cite journal|first=Christopher|last=Strachey|author-link=Christopher Strachey|title=[[Fundamental Concepts in Programming Languages]]|journal=[[Higher-Order and Symbolic Computation]]|volume=13|pages=11β49|year=2000|doi=10.1023/A:1010000313106|s2cid=14124601}}</ref><ref>{{cite book | url = http://www.eah-jena.de/~kleine/history/languages/Richards-BCPL-ReferenceManual.pdf | title = The BCPL Reference Manual | year = 1967 | chapter = 5.5 Conditional expressions | pages = 16β17 | access-date = 2017-03-15 | archive-date = 2016-03-16 | archive-url = https://web.archive.org/web/20160316100234/http://www.eah-jena.de/~kleine/history/languages/Richards-BCPL-ReferenceManual.pdf | url-status = dead }}</ref> Although many ternary operators are possible, the conditional operator is so common, and other ternary operators so rare, that the conditional operator is commonly referred to as ''the'' ternary operator.
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)