Ternary conditional operator: Difference between revisions

Content deleted Content added
clarity
Link suggestions feature: 3 links added.
 
(3 intermediate revisions by 3 users not shown)
Line 4:
{{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 syntaxsyntaxes 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> &rarr; ''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>
Line 90:
=== ALGOL 60 ===
 
ALGOL 60 introduced [[ALGOL 60#Expressions and compound statements|conditional expressions]] (thus ternary conditionals) to [[imperative programming]] languages.
 
'''if''' <boolean expression> '''then''' <expression> '''else''' <expression>
Line 424:
</syntaxhighlight>
 
But even in this case, the if expression would return unit. You don't need to write the else branch, because the compiler will assume the [[unit type]] on else.
 
===FORTH===
Line 521:
 
===Julia===
In [[Julia (programming language)|Julia]], "Note that the spaces around {{code|?}} and {{code|:}} are mandatory: an expression like {{code|a?b:c}} is not a valid ternary expression (but a [[newline]] is acceptable after both the {{code|?}} and the {{code|:}})."<ref>{{Cite web|url=https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation-1|title=Control Flow · The Julia Language|website=docs.julialang.org|access-date=2020-03-12}}</ref>
 
===JavaScript===
Line 925:
<syntaxhighlight lang="c">
Locals.UUTIndex = ( RunState.Root.Parameters.TestSocket.Index == 3 ? 3 : 0 )
</syntaxhighlight>
 
===V (Vlang)===
[[V (programming language)|V]] uses ''if expressions'' instead of a ternary conditional operator:<ref>{{Cite web |title=V language documentation |url=https://github.com/vlang/v/blob/master/doc/docs.md#if-expressions |access-date=2025-04-01|website=github}}</ref>
<syntaxhighlight lang="c">
num := 777
var := if num % 2 == 0 { "even" } else { "odd" }
println(var)
</syntaxhighlight>
 
Line 1,072 ⟶ 1,080:
* [[Multiplexer]]
* [[Null coalescing operator]], {{code|??}} operator
* [[Safe navigation operator]], often <code>?.</code>
 
==References==