Why is the inverted if-statement false?

1.9k Views Asked by At

Why does the if-statement count as false?

int money = -342;
int reviews 3;

if (!(money > 0 || reviews < 5 || reviews > 0)){}

Money is false,
the both reviews are true
and inverting them results with a true for the money,
and two falses for both of the reviews.
As I am using || for ´or´, one true should be enough to make the whole of-statement become true.

10

There are 10 best solutions below

0
Doberon On BEST ANSWER

Because the following:

if (!(Money > 0 || Reviews < 5 || Reviews > 0))

evaluates to

if (!(True))

thus, inverted,

if (False)
0
sellc On

The negation symbol is negating the entire statement. Thus, if any of those conditionals are true then false is returned. If all conditionals are false then true is returned.

0
MC Emperor On

The parentheses take precedence.

Take a look at the condition:

(!(money > 0 || reviews < 5 || reviews > 0))

First, the part inside the inner parenthesis are evaluated:

money > 0 || reviews < 5 || reviews > 0

Then, the order is from left to right:

  • money > 0 is false, continue to next;
  • reviews < 5 is true, so the expression in the inner parentheses is true.

The expression within the inner parentheses evaluates to true, so effectively your condition is equal to !(true) and that makes (false).

I suggest you study the basics of expression evaluation and operator precedence. Also, as pointed out by Ted Hopp, you need to rethink your logic. For example, I would rewrite expressions like !(money > 0) to money <= 0, to simplify it. In my opinion, it's best to not use the negation operator (!) in conjunction with relational operators (like < or ==), unless you absolutely have to.


Note:

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.

0
Przemysław Moskal On

if (!(Money > 0)){...} - It says Something went wrong as the Money integer is below 0. - No, it says it's below or equal to 0.

(Money > 0 || Reviews < 5 || Reviews > 0) - this is true if at least one of parts between || is true, but:

!(Money > 0 || Reviews < 5 || Reviews > 0) - this is false if at least one of parts between || is true. You get an opposite result with ! (negation).

3
ifly6 On

Why your value is false

Money is less than zero. That is false.

Reviews is less than 5. That is true.

It then short-circuits, but if you continued, Reviews is greater than 0. That is true.

Regardless, the whole statement in the brackets evaluates to true because of how an or operator works. You then invert it, so it turns into false.

A solution?

Honestly, it seems that what you want is a check that money is greater than 0, that reviews is less than 5 and that reviews is greater than 0.

This means that you should probably have something like money > 0 && reviews <= 5 && reviews >= 0. I assume that reviews here is something like a 0-5 star range. Obviously, if it were 1 to 5, that would be different.

0
daddykom On
!(Money > 0) == (Money <= 0)

!(Money > 0 || Reviews < 5 || Reviews > 0) == (Money <= 0 && Reviews >= 5 && Reviews <= 0)

It is better not to use ! before the whole statement.

0
was_777 On

Thats because it is satisfying the Reviews < 5 and Reviews > 0 to true. So the if statement in if (!(Money > 0 || Reviews < 5 || Reviews > 0)) will be false because you are using not operator.Thats the reason it prints We´re good.

0
DaHomieAdam and On

Umm. wait I've been doing this for a few years now and I'm pretty sure that the "!" also applys to the "||" thus making it an "&&" meaning it will always be false.

0
Ted Hopp On

Your if test always evaluates to false because regardless of the value of Reviews, either Reviews < 5 or Reviews > 0 will be true and you are then negating the result. (The value of Money is irrelevant, since || evaluates to true if either operand is true.) I think what you want is for these three to be true:

  1. Money must be greater than 0
  2. Reviews must be between 0 and 5 (inclusive)

This if test will do the job:

if (Money <= 0 || Reviews > 5 || Reviews < 0) {
    System.out.println("Something went wrong");
} else {
    System.out.println("We're good");
}

Alternatively, you can test for the positive conditions:

if (Money > 0 && Reviews >= 0 && Reviews <= 5) {
    System.out.println("We're good");
} else {
    System.out.println("Something went wrong");
}
0
Abhi On

Let's understand your code conditions step by step:

    int Money = -356;
    double Reviews = 4.8;

    if (!(Money > 0 || Reviews < 5 || Reviews > 0)) {
        System.out.println("Something went wrong");
    } else{
        System.out.println("We´re good");
    }

Given :

  • Money = -356
  • Reviews = 4.8

Let's break and evaluate the inner if condition first: (Money > 0 || Reviews < 5 || Reviews > 0)

 1. Money   > 0      -356 > 0           **False**  
 2. Reviews < 5       4.8 < 5           **True**
 3. Reviews > 0       4.8 > 0           **True**

Now as we can see inner conditions evaluate True but if you notice there is a (logical not) " ! " operator added outside.

(!(Money > 0 || Reviews < 5 || Reviews > 0))

Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Hence when your inner condition is True the Logical not ! makes it False.

So, System.out.println("We´re good"); Is executed.