HOME

TheInfoList



OR:

The conditional operator is supported in many
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s. This term usually refers to ?: as in C, C++, C#, and JavaScript. However, in Java, this term can also refer to && and , , .


&& and , ,

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.


Difference from bitwise operator

& and , are
bitwise operators In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operati ...
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 (expression1 , , expression2 , , expression3) If expression 1 is true, expressions 2 and 3 are NOT checked. if (expression1 , expression2 , expression3) 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.


Usage in Java

class ConditionalDemo1


"?:"

In most programming languages, ?: is called the conditional operator. It is a type of
ternary operator In mathematics, a ternary operation is an ''n''- ary operation with ''n'' = 3. A ternary operation on a set ''A'' takes any given three elements of ''A'' and combines them to form a single element of ''A''. In computer science, a ternary operator ...
. However, ternary operator in most situations refers specifically to ?: because it is the only operator that takes three operands.


Regular usage of "?:"

?: is used in conditional expressions. Programmers can rewrite an if-then-else expression in a more concise way by using the conditional operator.


Syntax

condition ? expression 1 : expression 2 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."


Association property

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).


Examples by languages


= Java

= class ConditionalDemo2 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).


= C++

= #include int main() There are several rules that apply to the second and third operands in C++: * If both operands are of the same type, the result is of that type * 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 * 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 * If both operands are of reference types, reference conversions are performed to convert them to a common type * If both operands are of type void, the common type is type void * If both operands are of the same user-defined type, the common type is that type.


= C#

= // condition ? first_expression : second_expression; static double sinc(double x) There are several rules that apply to the second and third operands x and y in C#: * If x has type X and y has type Y: * If an implicit conversion exists from X to Y but not from Y to X, Y is the type of the conditional expression. * If an implicit conversion exists from Y to X but not from X to Y, X is the type of the conditional expression. * Otherwise, no expression type can be determined, and a compile-time error occurs. * 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. * Otherwise, no expression type can be determined, and a compile-time error occurs.


= JavaScript

= var age = 26; var beverage = (age >= 21) ? "Beer" : "Juice"; console.log(beverage); // "Beer" The conditional operator of JavaScript is compatible with the following browsers: Chrome,
Edge Edge or EDGE may refer to: Technology Computing * Edge computing, a network load-balancing system * Edge device, an entry point to a computer network * Adobe Edge, a graphical development application * Microsoft Edge, a web browser developed by ...
,
Firefox Mozilla Firefox, or simply Firefox, is a free and open-source web browser developed by the Mozilla Foundation and its subsidiary, the Mozilla Corporation. It uses the Gecko rendering engine to display web pages, which implements current and ...
(1),
Internet Explorer Internet Explorer (formerly Microsoft Internet Explorer and Windows Internet Explorer, commonly abbreviated IE or MSIE) is a series of graphical user interface, graphical web browsers developed by Microsoft which was used in the Microsoft Wind ...
,
Opera Opera is a form of theatre in which music is a fundamental component and dramatic roles are taken by singers. Such a "work" (the literal translation of the Italian word "opera") is typically a collaboration between a composer and a librett ...
,
Safari A safari (; ) is an overland journey to observe wild animals, especially in eastern or southern Africa. The so-called "Big Five" game animals of Africa – lion, leopard, rhinoceros, elephant, and Cape buffalo – particularly form an importa ...
, Android webview, Chrome for Android, Edge Mobile,
Firefox for Android Firefox for Android is a web browser developed by Mozilla for Android smartphones and tablet computers. As with its desktop version, it uses the Gecko layout engine, and supports features such as synchronization with Firefox Sync, blocking web t ...
(4), Opera for Android, Safari on IOS,
Samsung Internet Samsung Internet Browser (or simply Samsung Internet or ''S Browser'') is a mobile web browser for smartphones and tablets developed by Samsung Electronics. It is based on the open-source Chromium project. It comes pre-installed on Samsung Galax ...
,
Node.js Node.js is an open-source server environment. Node.js is cross-platform and runs on Windows, Linux, Unix, and macOS. Node.js is a back-end JavaScript runtime environment. Node.js runs on the V8 JavaScript Engine and executes JavaScript code ou ...
.


Special usage in conditional chain

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.


Examples by languages


= JavaScript

= function example(…) // Equivalent to: function example(…)


= C/C++

= const double a = expression1 ? a1 : expression2 ? a2 : expression3 ? a3 : /*otherwise*/ a4; // Equivalent to: double a; if (expression1) a = a1; else if (expression2) a = a2; else if (expression3) a = a3; else /*otherwise*/ a = a4;


Special usage in assignment expression

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.


C/C++

((foo) ? bar : baz) = frink; //equivalent to: if (foo) bar = frink; else baz = frink;


See also

* ?:, a conditional operator in computer programming *
Ternary operation In mathematics, a ternary operation is an ''n''- ary operation with ''n'' = 3. A ternary operation on a set ''A'' takes any given three elements of ''A'' and combines them to form a single element of ''A''. In computer science, a ternary operator ...
*
Bitwise operators In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral (considered as a bit string) at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operati ...
* short circuit boolean operators *
Operator (programming) In computer programming, operators are constructs defined within programming languages which behave generally like functions, but which differ syntactically or semantically. Common simple examples include arithmetic (e.g. addition with ), c ...


References

{{reflist Computer programming Operators (programming) Articles with example Java code