title: C++ comparison with an assignment expression be the right hand side expression
body: <p>I am currently working with a problem about data structure using C++.
  I am going to check whether the nodes in a singly linked list are sorted in ascending order or not. Here comes some details of my code</p>
  
  <p>Node.cpp</p>
  
  <pre><code>class Node
  {
     public:
       double data;       
       Node* next;    
  };
  </code></pre>
  
  <p>For the part that problem occurs,</p>
  
  <pre><code>double preValue = sLL.head-&gt;data;       
  Node *curNode = sLL.head-&gt;next;     
  
      do
      {
          if (preValue &gt; (preValue = curNode-&gt;data))  // Problem occur in this line   
              return false;
      }while (curNode = curNode-&gt;next);
  </code></pre>
  
  <p>As the order of evaluation of '>' operator is evaluating the left hand side expression and then the right hand side expression </p>
  
  <p>AND</p>
  
  <p>the assignment operator would return a reference of the lvalue.</p>
  
  <p>Therefore, <code>preValue &gt; (preValue = curNode-&gt;data)</code> should compares the last node and the current node and assign the data of the next node after the comparison is done. Therefore, I think my implementation should be valid.</p>
  
  <p>However, the result of <code>if(preValue &gt; (preValue = curNode-&gt;data))</code> is out of my expected, when comparing a <code>preValue</code> which is bigger than the new <code>preValue</code>, it keeps returning false.</p>
  
  <p>I have tried to print out the return value of the if statement, it always return 0 whenever the left expression if greater or smaller than the right expression.
  I cannot understand why this happen. Could anyone tell me what mistake did I made?</p>
  
  <p>p.s. the program work fine with the following implementation</p>
  
  <pre><code>double preValue = list.head-&gt;data;      
  Node *curNode = list.head-&gt;next;            
  
  do
  {
      if (preValue &gt; curNode-&gt;data)       // Check ascending order
          return false;
  
      preValue = curNode-&gt;data;
  
  }while (curNode = curNode-&gt;next);       
  </code></pre>