<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Conditional_operator</id>
	<title>Conditional operator - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Conditional_operator"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Conditional_operator&amp;action=history"/>
	<updated>2026-05-05T16:43:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Conditional_operator&amp;diff=3028603&amp;oldid=prev</id>
		<title>imported&gt;RastaKins: /* &quot;?:&quot; */ ?: does not appear in most programming languages</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Conditional_operator&amp;diff=3028603&amp;oldid=prev"/>
		<updated>2025-06-19T14:17:38Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;&amp;quot;?:&amp;quot;: &lt;/span&gt; ?: does not appear in most programming languages&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Wiktionary|conditional operator}}&lt;br /&gt;
&lt;br /&gt;
{{mergeto|ternary conditional operator|discuss=Talk:Conditional operator#Related Article|date=February 2025}}&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;conditional operator&amp;#039;&amp;#039;&amp;#039; is supported in many [[programming language]]s. This term usually refers to &amp;lt;code&amp;gt;?:&amp;lt;/code&amp;gt; as in [[C (programming language)|C]], [[C++]], [[C sharp (programming language)|C#]], [[JavaScript]] and [[PHP]]. However, in Java, this term can also refer to &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;||&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== &amp;amp;&amp;amp; and || ==&lt;br /&gt;
In some programming languages, e.g. Java, the term &amp;#039;&amp;#039;conditional operator&amp;#039;&amp;#039; refers to [[Short-circuit evaluation|short circuit boolean operators]]  &amp;lt;code&amp;gt;&amp;amp;&amp;amp;&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;||&amp;lt;/code&amp;gt;. The second expression is evaluated only when the first expression is not sufficient to determine the value of the whole expression.&amp;lt;ref&amp;gt;{{Cite web|url=https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html|title=Equality, Relational, and Conditional Operators (The Java™ Tutorials &amp;gt; Learning the Java Language &amp;gt; Language Basics)|website=docs.oracle.com|access-date=2019-04-29}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Difference from bitwise operator ===&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;amp;&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;|&amp;lt;/code&amp;gt; are [[Bitwise operation|bitwise operators]] that occur in many programming languages. The major difference is that bitwise operations operate on the individual bits of a binary numeral, whereas conditional operators operate on logical operations. Additionally, expressions before and after a bitwise operator are always evaluated.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
if (expression1 || expression2 || expression3)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;If expression 1 is true, expressions 2 and 3 are NOT checked.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
if (expression1 | expression2 | expression3)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;This checks expressions 2 and 3, even if expression 1 is true.&lt;br /&gt;
&lt;br /&gt;
Short circuit operators can reduce run times by avoiding unnecessary calculations. They can also avoid Null Exceptions when expression 1 checks whether an object is valid.&lt;br /&gt;
&lt;br /&gt;
=== Usage in Java ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class ConditionalDemo1 {&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        int value1 = 1;&lt;br /&gt;
        int value2 = 2;&lt;br /&gt;
        if ((value1 == 1) &amp;amp;&amp;amp; (value2 == 2))&lt;br /&gt;
            System.out.println(&amp;quot;value1 is 1 AND value2 is 2&amp;quot;);&lt;br /&gt;
        if ((value1 == 1) || (value2 == 1))&lt;br /&gt;
            System.out.println(&amp;quot;value1 is 1 OR value2 is 1&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;?:&amp;quot; ==&lt;br /&gt;
In some programming languages, [[?:]] is called the conditional operator. It is a type of [[ternary operator]]. However, ternary operator in most situations refers specifically to [[?:]]  because it is the only operator that takes three operands.&amp;lt;ref name=&amp;quot;:0&amp;quot;&amp;gt;{{Cite web|url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator|title=?: Operator - C# Reference|last=BillWagner|website=docs.microsoft.com|language=en-us|access-date=2019-04-29}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Regular usage of &amp;quot;?:&amp;quot; ===&lt;br /&gt;
&amp;lt;code&amp;gt;?:&amp;lt;/code&amp;gt; is used in conditional expressions. Programmers can rewrite an if-then-else expression in a more concise way by using the conditional operator.&amp;lt;ref&amp;gt;{{Cite web|url=http://www.cafeaulait.org/course/week2/43.html|title=The ? : operator in Java|website=www.cafeaulait.org|access-date=2019-04-29}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Syntax ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
condition ? expression 1 : expression 2&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;condition: An expression which is evaluated as a [[Boolean algebra|boolean]] value.&lt;br /&gt;
&lt;br /&gt;
expression 1, expression 2: Expressions with values of any type.&lt;br /&gt;
&lt;br /&gt;
If the condition is evaluated to true, the expression 1 will be evaluated. If the condition is evaluated to false, the expression 2 will be evaluated.&lt;br /&gt;
&lt;br /&gt;
It should be read as: &amp;quot;If condition is true, assign the value of expression 1 to result. Otherwise, assign the value of expression 2 to result.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
==== Association property ====&lt;br /&gt;
The conditional operator is right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a ? b : c ? d : e is evaluated as a ? b : (c ? d : e).&amp;lt;ref name=&amp;quot;:0&amp;quot; /&amp;gt;  The exception is [[PHP]], in which it was left-associative prior to version 8, and is non-associative thereafter.&amp;lt;ref&amp;gt;https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Examples by languages ====&lt;br /&gt;
&lt;br /&gt;
===== Java =====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
class ConditionalDemo2 {&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        int value1 = 1;&lt;br /&gt;
        int value2 = 2;&lt;br /&gt;
        int result;&lt;br /&gt;
        boolean someCondition = true;&lt;br /&gt;
        result = someCondition ? value1 : value2;&lt;br /&gt;
&lt;br /&gt;
        System.out.println(result);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;In this example, because &amp;#039;&amp;#039;someCondition&amp;#039;&amp;#039; is true, this program prints &amp;quot;1&amp;quot; to the screen. Use the ?: operator instead of an if-then-else statement if it makes your code more readable; for example, when the expressions are compact and without side-effects (such as assignments).&lt;br /&gt;
&lt;br /&gt;
===== C++ =====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    int x = 1;&lt;br /&gt;
    int y = 2;&lt;br /&gt;
    std::cout &amp;lt;&amp;lt; ( x &amp;gt; y ? x : y ) &amp;lt;&amp;lt; &amp;quot; is the greater of the two.&amp;quot; &amp;lt;&amp;lt; std::endl;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;There are several rules that apply to the second and third operands in C++:&lt;br /&gt;
&lt;br /&gt;
* If both operands are of the same type, the result is of that type&lt;br /&gt;
* If both operands are of arithmetic or enumeration types, the usual arithmetic conversions (covered in Standard Conversions) are performed to convert them to a common type&lt;br /&gt;
* If both operands are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type&lt;br /&gt;
* If both operands are of reference types, reference conversions are performed to convert them to a common type&lt;br /&gt;
* If both operands are of type void, the common type is type void&lt;br /&gt;
* If both operands are of the same user-defined type, the common type is that type.&amp;lt;ref name=&amp;quot;:1&amp;quot;&amp;gt;{{Cite web|url=https://docs.microsoft.com/en-us/cpp/cpp/conditional-operator-q|title=Conditional Operator: ?|last=mikeblome|website=docs.microsoft.com|language=en-us|access-date=2019-04-29}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== C# =====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
// condition ? first_expression : second_expression;&lt;br /&gt;
&lt;br /&gt;
static double sinc(double x) &lt;br /&gt;
{&lt;br /&gt;
    return x != 0.0 ? Math.Sin(x) / x : 1.0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
There are several rules that apply to the second and third operands &amp;#039;&amp;#039;x&amp;#039;&amp;#039; and &amp;#039;&amp;#039;y&amp;#039;&amp;#039; in C#:&lt;br /&gt;
&lt;br /&gt;
* If x has type X and y has type Y:&lt;br /&gt;
* If an implicit conversion exists from X to Y but not from Y to X, Y is the type of the conditional expression.&lt;br /&gt;
* If an implicit conversion exists from Y to X but not from X to Y, X is the type of the conditional expression.&lt;br /&gt;
* Otherwise, no expression type can be determined, and a compile-time error occurs.&lt;br /&gt;
* If only one of x and y has a type, and both x and y are implicitly convertible to that type, that type is the type of the conditional expression.&lt;br /&gt;
* Otherwise, no expression type can be determined, and a compile-time error occurs.&amp;lt;ref name=&amp;quot;:0&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== JavaScript =====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
var age = 26;&lt;br /&gt;
var beverage = (age &amp;gt;= 21) ? &amp;quot;Beer&amp;quot; : &amp;quot;Juice&amp;quot;;&lt;br /&gt;
console.log(beverage); // &amp;quot;Beer&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The conditional operator of JavaScript is compatible with the following browsers:&lt;br /&gt;
&lt;br /&gt;
[[Google Chrome|Chrome]], [[Microsoft Edge|Edge]], [[Firefox]] (1), [[Internet Explorer]], [[Opera (web browser)|Opera]], [[Safari (web browser)|Safari]], Android webview, [[Google Chrome for Android|Chrome for Android]], [[Microsoft Edge|Edge Mobile]], [[Firefox for Android]] (4), Opera for Android, Safari on IOS, [[Samsung Internet]], [[Node.js]].&amp;lt;ref&amp;gt;{{Cite web |title=Conditional (ternary) operator - JavaScript |url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator |access-date=2019-04-29 |website=developer.mozilla.org |language=en-US}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Special usage in conditional chain ===&lt;br /&gt;
The ternary operator is right-associative, which means it can be &amp;quot;chained&amp;quot; in the following way, similar to an if ... else if ... else if ... else chain.&amp;lt;ref name=&amp;quot;:1&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Examples by languages ====&lt;br /&gt;
&lt;br /&gt;
===== JavaScript =====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
function example(…) {&lt;br /&gt;
    return condition1 ? value1&lt;br /&gt;
        : condition2 ? value2&lt;br /&gt;
        : condition3 ? value3&lt;br /&gt;
        : value4;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Equivalent to:&lt;br /&gt;
&lt;br /&gt;
function example(…) {&lt;br /&gt;
    if (condition1) { return value1; }&lt;br /&gt;
    else if (condition2) { return value2; }&lt;br /&gt;
    else if (condition3) { return value3; }&lt;br /&gt;
    else { return value4; }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===== C/C++ =====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
const double a =&lt;br /&gt;
	expression1	? a1&lt;br /&gt;
	: expression2	? a2&lt;br /&gt;
	: expression3	? a3&lt;br /&gt;
	: /*otherwise*/	a4;&lt;br /&gt;
&lt;br /&gt;
// Equivalent to:&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
  if (expression1)&lt;br /&gt;
	a = a1;&lt;br /&gt;
  else if (expression2)&lt;br /&gt;
	a = a2;&lt;br /&gt;
  else if (expression3)&lt;br /&gt;
	a = a3;&lt;br /&gt;
  else /*otherwise*/&lt;br /&gt;
	a = a4;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Special usage in assignment expression ===&lt;br /&gt;
the conditional operator can yield a [[Value (computer science)#lrvalue|L-value]] in C/C++ which can be assigned another value, but the vast majority of programmers consider this extremely poor style, if only because of the technique&amp;#039;s obscurity.&amp;lt;ref&amp;gt;{{Cite web|url=http://wiki.c2.com/?ConditionalOperator|title=Conditional Operator|website=wiki.c2.com|access-date=2019-04-29}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== C/C++ ====&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
((foo) ? bar : baz) = frink;&lt;br /&gt;
&lt;br /&gt;
// equivalent to:&lt;br /&gt;
if (foo)&lt;br /&gt;
    bar = frink;&lt;br /&gt;
else&lt;br /&gt;
    baz = frink;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
{{Portal|Computer programming}}&lt;br /&gt;
* {{Mono|[[?:]]}}, a conditional operator in computer programming&lt;br /&gt;
* [[Ternary operation]]&lt;br /&gt;
* [[Bitwise operation|Bitwise operators]]&lt;br /&gt;
* [[Short-circuit evaluation|Short-circuit boolean operators]]&lt;br /&gt;
* [[Operator (programming)]]&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{reflist}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Computer programming]]&lt;br /&gt;
[[Category:Operators (programming)]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;/div&gt;</summary>
		<author><name>imported&gt;RastaKins</name></author>
	</entry>
</feed>