+ 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 } }

11th Mar 2021, 1:16 AM
wenona saranza
wenona saranza - avatar
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
12th Mar 2021, 11:42 PM
Elon
Elon - avatar
+ 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.
11th Mar 2021, 1:26 AM
Ipang
+ 1
It may be an issue with operator precedence. Try !( (a != b) || (b <= c) )
11th Mar 2021, 1:27 AM
Ben Allen (Njinx)
Ben Allen (Njinx) - avatar
+ 1
Make sure it was written correctly: System.out.println(! ( a != b || b <= c ) );
11th Mar 2021, 2:43 AM
Ipang
0
still doesnt work
11th Mar 2021, 2:15 AM
wenona saranza
wenona saranza - avatar