Equals vs. Assignment Operator

Consider the following example where we are comparing x to y.

int x, y;

do {
   ...
} while (x = y);

In the example above, we are using the assignment operator "=" when we should have used the equality operator "==". As a result, the value of y will be assigned to x when it's checking the expression through each iteration of the loop. The assignment of variables of the same data type will always result in a boolean value of true. Therefore, we will never exit the loop. The correct code is shown below.

int x, y;

do {
   ...
} while (x == y);

Fook Yuen Sin - Team 1