In some programming languages, e.g. Java, the term conditional operator refers to short circuit boolean operators && and ||. The second expression is evaluated only when the first expression is not sufficient to determine the value of the whole expression.1
& and | are 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.
If expression 1 is true, expressions 2 and 3 are NOT checked.
This checks expressions 2 and 3, even if expression 1 is true.
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.
In most 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.2
?: is used in conditional expressions. Programmers can rewrite an if-then-else expression in a more concise way by using the conditional operator.3
condition: An expression which is evaluated as a boolean value.
expression 1, expression 2: Expressions with values of any type.
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.
It should be read as: "If condition is true, assign the value of expression 1 to result. Otherwise, assign the value of expression 2 to result."
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).4 The exception is PHP, in which it was left-associative prior to version 8, and is non-associative thereafter.5
In this example, because someCondition is true, this program prints "1" 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).
There are several rules that apply to the second and third operands in C++:
There are several rules that apply to the second and third operands x and y in C#:
The conditional operator of JavaScript is compatible with the following browsers:
Chrome, Edge, Firefox (1), Internet Explorer, Opera, Safari, Android webview, Chrome for Android, Edge Mobile, Firefox for Android (4), Opera for Android, Safari on IOS, Samsung Internet, Node.js.8
The ternary operator is right-associative, which means it can be "chained" in the following way, similar to an if ... else if ... else if ... else chain.9
the conditional operator can yield a 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's obscurity.10
"Equality, Relational, and Conditional Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)". docs.oracle.com. Retrieved 2019-04-29. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html ↩
BillWagner. "?: Operator - C# Reference". docs.microsoft.com. Retrieved 2019-04-29. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator ↩
"The ? : operator in Java". www.cafeaulait.org. Retrieved 2019-04-29. http://www.cafeaulait.org/course/week2/43.html ↩
https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary ↩
mikeblome. "Conditional Operator: ?". docs.microsoft.com. Retrieved 2019-04-29. https://docs.microsoft.com/en-us/cpp/cpp/conditional-operator-q ↩
"Conditional (ternary) operator - JavaScript". developer.mozilla.org. Retrieved 2019-04-29. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator ↩
"Conditional Operator". wiki.c2.com. Retrieved 2019-04-29. http://wiki.c2.com/?ConditionalOperator ↩