-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?

erip
  • 16,374
  • 11
  • 66
  • 121
DachuanZhao
  • 1,181
  • 3
  • 15
  • 34
  • 5
    It's equal to zero because you need to [read a book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). The problem is assignment versus comparison for equality. – Some programmer dude Oct 20 '16 at 15:57
  • 1
    To be fair this is a novice mistake. We all made such mistakes while we were learning... – vz0 Oct 20 '16 at 16:02
  • 1
    @Someprogrammerdude There is no need to be rude on this site. Would you consider redacting the first sentence? – DBPriGuy Oct 20 '16 at 16:03
  • @Someprogrammerdude Yes,I konw.But My question is that why does "cout<<(i=0);" output "0"? – DachuanZhao Oct 20 '16 at 16:19
  • @yellowantphil I miss a ";" at the sixth line.It is my fault.But My question is that why does "cout<<(i=0);" output "0"? – DachuanZhao Oct 20 '16 at 16:24
  • Because `i=0` is ASSIGNING, not comparing. And the value of the assignment expression is the value being assigned. – Fred Larson Oct 20 '16 at 16:25
  • @yellowantphil I have read six chapters of a C++ book named "C++ primer".But I doesn't find the answer. – DachuanZhao Oct 20 '16 at 16:27
  • @FredLarson Your answer is fantastic!Thanks a lot! – DachuanZhao Oct 20 '16 at 16:30
  • @yellowantphil.I think "if(i=0)" is equivalent to “if(0)”.So "{i = 1;}" is not executed by the program.What is your point of view? – DachuanZhao Oct 20 '16 at 16:37
  • 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 Oct 20 '16 at 17:08
  • @lisyarus My specific issue with his comment is that phrase "you need to read a book" is often considered derogatory, at least in American English. – DBPriGuy Oct 20 '16 at 17:50

3 Answers3

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.

DBPriGuy
  • 294
  • 1
  • 13
  • Yes,I konw.But why does "cout<<(i=0);" put out"0"? – DachuanZhao Oct 20 '16 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 Oct 20 '16 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.

M. S.
  • 109
  • 7
-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.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131