Why ternary operation gives nullpointer while its ifelse counterpart doesn’t? [duplicate]

Here’s the relevant quote from the spec (§15.25.2): Boolean conditional expressions are standalone expressions (§15.2). The type of a boolean conditional expression is determined as follows: If the second and third operands are both of type Boolean, the conditional expression has type Boolean. Otherwise, the conditional expression has type boolean. Therefore, the overall expression’s type … Read more

“Wrong” return type when using if vs. ternary opertator in Java

Basically it’s following the rules of section 15.25 of the JLS, specifically: Otherwise, if the second and third operands have types that are convertible (§5.1.8) to numeric types, then there are several cases: […] Otherwise, binary numeric promotion (§5.6.2) is applied to the operand types, and the type of the conditional expression is the promoted … Read more

Precedence: Logical or vs. Ternary operator

Yes, the || operator has higher precedence than the conditional ?: operator. This means that it is executed first. From the page you link: Operator precedence determines the order in which operators are evaluated. Operators with higher precedence are evaluated first. Let’s have a look at all the operations here: step = step || (start … Read more

Why can’t I use a “break” statement inside a ternary conditional statement in C++?

The ternary conditional operator is an operator that combines multiple expressions into a larger expression. break is a statement and not an expression, so it can’t be used inside a ternary conditional expression. You could, though, rewrite your code like this: while (current->left != nullptr) current = current->left; Hope this helps!