-1

My C++ code is as follows:

#include <iostream>
using namespace std;

int main() {

    int i = 0;

    cout << (i=0) << endl;

    if(i=0) {
        i=1;
    }

    cout << i;

    return 0;
}

Why is (i=0) equal to 0?

11
  • 5
    It's equal to zero because you need to read a book. The problem is assignment versus comparison for equality. Commented Oct 20, 2016 at 15:57
  • 1
    To be fair this is a novice mistake. We all made such mistakes while we were learning...
    – vz0
    Commented Oct 20, 2016 at 16:02
  • 1
    @Someprogrammerdude There is no need to be rude on this site. Would you consider redacting the first sentence?
    – DBPriGuy
    Commented Oct 20, 2016 at 16:03
  • @Someprogrammerdude Yes,I konw.But My question is that why does "cout<<(i=0);" output "0"? Commented Oct 20, 2016 at 16:19
  • 2
    @DBPriGuy I don't find Someprogrammerdude's comment to be rude, - he even provided a link to a list of good books. The mistake is obviously a novice one and most books cover this issue.
    – lisyarus
    Commented Oct 20, 2016 at 17:08

3 Answers 3

8

You have confused the assignment operator '=' with the equality operator '=='.

Thus, your if statement if(i=0) isn't checking whether i is equal to 0, it is assigning 0 to i. And since that assignment succeeded, the if succeeded and so i was subsequently assigned to the value in the statement.

EDIT:

As per request: Why does "cout<<(i=0);" put out"0"?

cout<<(i=0)<<endl; prints zero because once again, i=0 is an assignment statement, not a comparison statement. It is not comparing i to 0 then printing the result, it is assigning i to 0 and then printing i. Since i is 0, the character '0' gets printed.

2
  • Yes,I konw.But why does "cout<<(i=0);" put out"0"? Commented Oct 20, 2016 at 16:09
  • It is not clear that that was your question, but I will add an addendum. You may want to edit your question to add clarity to what specifically you are looking for.
    – DBPriGuy
    Commented Oct 20, 2016 at 16:15
0

If you put you condition to if(condition) statements:

If value returned by condition is != 0, then statements are executed

If value returned by condition is == 0 then statements are not executed.

Assignment operator return the reference to value

So: If you put assignment operator to if, then the results of the assignment will be checked and i=0 return 0 so the statements won't be executed.

-2

If you put your code on more lines and use a debugger you can answer your own question.

Should be obvious that the body of if(0) never runs.

2
  • Yes,I konw.But why does "cout<<(i=0);" output "0"? Commented Oct 20, 2016 at 16:18
  • This is not an answer. Even if it were, it's fairly condescending. Not sure why people do this. -1
    – erip
    Commented Oct 20, 2016 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.