title: Precedence inside While loop
body: <p>I wrote a program for linked list which accepts value in ascending order. I have a while loop inside the program regarding which I have a question. I'm not entering the full code, it's just the modular function for inserting in ascending order. Here, x is taken as input from user and head node is passed into the function. Now, In while loop, if I exchange the conditions, i.e. keep "x > temp->next->data" in front and move "temp->next != NULL" behind, then, I'm getting an error if my input sequence is like (1, 2, 3, 4, 5) i.e. already in ascending order.    </p>
  
  <pre><code> list *insert(list *head, int x)
   {
      list *temp = head, *prev = temp, *temp2 = create();
      temp2-&gt;data = x;
      if(head == NULL || x &lt; head-&gt;data)
      {
          temp2-&gt;next = head;
          head = temp2;
      }
      else
      {
          while(temp-&gt;next != NULL &amp;&amp; x &gt; temp-&gt;next-&gt;data) //while(x &gt; temp-&gt;next-&gt;data &amp;&amp; temp-&gt;next != NULL)
          {
              temp = temp-&gt;next;
          }
          temp2-&gt;next = temp-&gt;next;
          temp-&gt;next = temp2;
      }
      return head;
   }
  </code></pre>
  
  <p>The way I think it is, when I enter 1, it goes to if statement(considering head is empty initially) and 1 is added to list. Then, when I enter 2 and if it checks the condition, temp->next is NULL due to only one element in list, and, NULL->data will give an error. But, it works if the conditions are according to the program above. Is it that while loop gives precedence to the conditions within itself?      </p>