You have to divide that line into parts:
- Part 1:
!a|b&&(c||b)&&c
- Part 2:
(?a?1:2):3
Analysis of Part 1:
First the operators with precedence are executed, which in this case is the negation! The expression can be written like this:
(!a) | b && (c||b) && c
Now, you have to directly evaluate the expression. For this, it is convenient to replace the Boolean values with the variables, so you have the following:
(!false) | false && (true || false) && true
We solve the elements in parentheses first, and you get:
true | false && true && true
We execute the operators one by one. The result of true | false
is false
:
true && true && true
When only operators &&
remain, the result of this expression will be true
.
Part 2 Analysis:
Once we have the result of part 1, it is easier to evaluate part 2. We replace the variables by their values:
true ? (false ? 1 : 2) : 3
The ternary operator ?
is like a if
, so the code could be understood like this:
if (true) {
if (false) {
return 1;
} else {
return 2;
}
} else {
return 3;
}
By following the execution of this code, you can see that the result is 2.