Why does this if-statement combining assignment and an equality check return true?

C++If Statement

C++ Problem Overview


I've been thinking of some beginner mistakes and I ended up with the one on the if statement. I expanded a bit the code to this:

int i = 0;
if (i = 1 && i == 0) {
    std::cout << i;
}

I have seen that the if statement returns true, and it cout's i as 1. If i is assigned 1 in the if statement, why did i == 0 return true?

C++ Solutions


Solution 1 - C++

This has to do with operator precedence.

if (i = 1 && i == 0)

is not

if ((i = 1) && (i == 0))

because both && and == have a higher precedence than =. What it really works out to is

if (i = (1 && (i == 0)))

which assigns the result of 1 && (i == 0) to i. So, if i starts at 0 then i == 0 is true, so 1 && true is true (or 1), and then i gets set to 1. Then since 1 is true, you enter the if block and print the value you assigned to i.

Solution 2 - C++

Assuming your code actually looks like this:

#include <iostream>
using namespace std;

int main()  {
    int i = 0;
    if (i = 1 && i == 0) {
        cout << i;
    }
}

Then this:

if (i = 1 && i == 0) {
    

evaluates as

 if (i = (1 && i == 0)) {

and so i is set to 1.

Solution 3 - C++

The actual answer is:

  1. The compiler gives precedence to "i == 0", which evaluates to true.
  2. Then it will evaluate i=1 as TRUE or FALSE, and since compiled assignment operators never fail (otherwise they wouldn't compile), it also evaluates to true.
  3. Since both statements evaluate as true, and TRUE && TRUE evaluates to TRUE, the if statement will evaluate to TRUE.

As proof, just look at the asm output of your compiler for the code you entered (all comments are my own):

mov     dword ptr [rbp - 8], 0    ; i = 0;
cmp     dword ptr [rbp - 8], 0    ; i == 0?
sete    al                        ; TRUE (=1)
mov     cl, al
and     cl, 1                     ; = operator always TRUE
movzx   edx, cl
mov     dword ptr [rbp - 8], edx  ; set i=TRUE;
test    al, 1                     ; al never changed,
                                  ; so final ans is TRUE

The asm output above was from CLANG, but all other compilers I looked at gave similar output. This is true for all the compilers on that site, whether they are pure C or C++ compilers, all without any pragmas to change the mode of the compiler (which by default is C++ for the C++ compilers)

Note that your compiler did not actually set i=1, but i=TRUE (which means any 32-bit not zero integer value). That's because the && operator only evaluates whether a statement is TRUE or FALSE, and then sets the results according to that result. As proof, try changing i=1 to i=2 and you can observe for yourself that nothing will change. See for yourself using any online compiler at Compiler Explorer

Solution 4 - C++

It has to do with parsing an the right to left rules. Eg y = x+5.
All sub-expressions are weighted in importance. Two expressions of equal importance are evaluated right to left, . The && expression side is done first, followed by the LHS.

Makes sense to me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTehMattGRView Question on Stackoverflow
Solution 1 - C++NathanOliverView Answer on Stackoverflow
Solution 2 - C++user2100815View Answer on Stackoverflow
Solution 3 - C++ar18View Answer on Stackoverflow
Solution 4 - C++Leslie SatensteinView Answer on Stackoverflow