Equals vs. Assignment Operator
Consider the following example where we are comparing x to 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.
|
Loops while do...while for Control Structure Loop Design Methodology Common Loop Errors |
||
Fook Yuen Sin - Team 1 |