0
#include <iostream>
using namespace std; 
int main()
{
        int var1 = 5; 
        int var2 = 6;
        if ((var2 = 1) == var1)
           cout << var2;
        else 
            cout  << (var2 + 1); 
}

I want to understand how does the var2=1 assignment work. I mean what happens when the value 1 is assigned to var2 and how (and why ) can we compare an assignment operation(var2=1) with a variable. Basically I want to understand what going on behind the scenes . If anybody can help ?

  • 3
    Just like `+` returns the value of its 2 operands being added, `=` returns the new value of its LHS operand. – mediocrevegetable1 Oct 18 '21 at 06:07
  • An expression like `var2 = 1` has two properties. One is the *effect* of assigning `var2` to have the value `1`. The other is a *result* which is equal to the right hand side (i.e. `1`). So `if ((var2 = 1) == var1`) has the same net effect as `var2 = 1; if (1 == var1)` – Peter Oct 18 '21 at 06:20

0 Answers0