User:Haoran-Y/sandbox

Conditional operator is supported in many programming languages. This term usually refers to  as in C/C++/C#, Python, and JavaScript. However, in Java this term can also refer to  and.

&& and ||
In some programming languages, e.g. Java, the term “conditional operator” refer 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 that occurs 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 will always be evaluated. If expression 1 is true, expression 2 and 3 will NOT be checked. This will check expression 2 and 3, even if 1 is already true.

Short circuit operators can reduce run-time by avoid calling later functions. They can also avoid Null Exception if expression 1 check whether an object is valid.

?:
In most programming languages, ?: is called the conditional operator. It is a type of ternary operator, while ternary operator in most situation means specifically to ?:, because it is the only operator that takes three operands.

Regular Usage of ?:
is used in conditional expressions. Programmer can rewrite a if-then-else expression in a more concise way by using the conditional operator.

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

Java
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/C++
There are several rules apply to 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#
There are several rules apply to to the second and third operands in C#:


 * If x has type X and y has type Y then
 * If an implicit conversion (Implicit conversions) exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.
 * If an implicit conversion (Implicit conversions) exists from Y to X, but not from X to Y, then 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, of are implicitly convertible to that type, then that is the type of the conditional expression.
 * Otherwise, no expression type can be determined, and a compile-time error occurs.

JavaScript
Conditional operator of JavaScript is compatible with 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.

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.

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 the technique's obscurity.