+ 4
i need help 😅
public class Main { public static void main(String[] args) { int a = 100; int b = 25; int c = 5; System.out.println(! (a! = b | | b <= c)); // returns false because ! (not) is used to reverse the result } }
5 Antworten
+ 3
There is not an error !
Because the logical operator "or" return the first true evaluated in a logical expression. So (a!=b ||b<=c) is evaluated to true because a!=b is true.
Next the not operator bring to false the result.
Ipang perfectly answered to your question
+ 2
Yes, that's right. Additionally, logical OR operator only evaluated `a != b` in this case, since that evaluates to true, the right hand side operand `b <= c` is abandoned.
Logical OR operator only needs one true value, and it was found in `a != b` hence `b <= c` evaluation was unnecessary.
You may lookup on 'short circuit evaluation' online for more on that 👍
(Edit)
Careful when writing operator, I don't know whether it was because of copy/paste problem or what; but look at how `!=` and `||` was written in the snippet.
+ 1
It may be an issue with operator precedence. Try !( (a != b) || (b <= c) )
+ 1
Make sure it was written correctly:
System.out.println(! ( a != b || b <= c ) );
0
still doesnt work