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

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?

4 Answers
4

Leave a Comment