2
if (1 == 1 == 1 == 1 == 1)
    std::cout << "right";

The code above shows 'right'.

if (-1 == -1)
    std::cout << "right";

The code above also shows 'right'.

if (-1 == -1 == -1)
    std::cout << "right";

The code above shows nothing. (It's because the if statement isn't TRUE I guess?)

I would like to know why this weird thing happens.

Because -1 is equal to -1 and this statement is always TRUE no matter how many times I repeat (as far as I know).

asmmo
  • 6,922
  • 1
  • 11
  • 25
GamerCoder
  • 139
  • 6
  • 1
    think about what `true == negative-one` will evaluate to – M.M Mar 15 '20 at 11:37
  • 7
    @M.M, same problem, but you just don't start with -1 instead. Anyway, `-1 == -1` is `true`. `true` is `1`. `1 == -1` is `false` – ChrisMM Mar 15 '20 at 11:37
  • 3
    Keep in mind that, in C++, you don't write mathematical equations, but a series of statements that are evaluated one by one. – Lukas-T Mar 15 '20 at 11:44
  • @M.M aha, now I understand. thanks! – GamerCoder Mar 15 '20 at 11:44
  • i just upvoted all the comments! thanks to everyone! – GamerCoder Mar 15 '20 at 11:46
  • You need to also accept an answer! – CinchBlue Mar 15 '20 at 11:47
  • I would have been shocked if the question had been asked by @M.M . – Soner from The Ottoman Empire Mar 15 '20 at 11:53
  • duplicates: [Why comparing three variables together with == evaluates to false?](https://stackoverflow.com/q/58271178/995714), [Two '==' equality operators in same 'if' condition are not working](https://stackoverflow.com/q/2155280/995714), [Chaining Bool values](https://stackoverflow.com/q/5939077/995714), [Check to see if all variable are equal to the same value](https://stackoverflow.com/q/15208831/995714), [Why do most mainstream languages not support “x < y < z” syntax for 3-way Boolean comparisons?](https://softwareengineering.stackexchange.com/q/316969/98103) – phuclv Mar 15 '20 at 11:58

5 Answers5

9

The conditions are evaluated from left to right, hence the following conditional statement

if (-1 == -1 == -1)
    std::cout << "right";

is equivalent to

if (true == -1)//since -1 === -1
    std::cout << "right";

equivalent to

if (1 == -1) // true is casted to 1
    std::cout << "right";

equivalent to

if (false)
    std::cout << "right";

So it's normal the statement std::cout << "right"; doesn't execute and you get nothing.

asmmo
  • 6,922
  • 1
  • 11
  • 25
2

The conditions are done in their order so:

-1 == -1 == -1 == -1 == -1

becomes

true == -1 == -1 == -1 (because the first -1==-1 gives true)

becomes

false == -1 == -1 (because true==-1 gives false)

becomes

false ==-1

becomes

false

The same thing happen with 1==1==1==1==1 because 1==true (but not 1===true).

Entretoize
  • 2,124
  • 3
  • 23
  • 44
1

Let's look at your three examples.

1 == 1 == 1 == 1 == 1 would be equivalent to true == 1 == 1 == 1, since true == 1 is true (since true gets treated as a 1), you'd then get true == 1 == 1 which is then true == 1 and finally just true.

In your second example, -1 == -1, this is simply just true, since obviously -1 equals itself.

Now, for your last example, you have -1 == -1 == -1, which becomes true == -1. true is not equal to -1 since 1 is not equal to -1, so this is therefore false. As a result, the "right" does not get printed.

ChrisMM
  • 8,448
  • 13
  • 29
  • 48
1

In C++ the evaluation order of the equality operator == is from left to right:

(-1 == -1) == -1 

is evaluated to

1 == -1 

which returns false

flaskoln
  • 87
  • 9
0

Your code hits on the rules for casting between int and bool.

  1. Comparisons between int objects return a bool.
  2. Comparisons between an bool and an int upcasts the bool to an int.
    • false cast to int is 0
    • true cast to int is usually 1

Your code does (1) for the first comparison and (2) for all comparisons after.

So, if we write your condition and evaluate it step by step:

(1 == 1)   // == ... == 1
(true)     // == ... == 1
int(true)  // == ... == 1
1          // == ... == 1
true

versus if you compare -1:

(-1 == -1)   // == ... == -1
(true)       // == ... == -1
int(true)    // == ... == -1
1            // == ... == -1
false
CinchBlue
  • 6,046
  • 1
  • 27
  • 58