&& vs. || Operator
Consider the following example where we want to exit the loop when the value of x is either 1 or 2.
In the example above, both x == 1 and x == 2 would have to be true so that the negation of each would be false in order to make the || operation return false (thus exiting the loop). The statement above is always true because x can't be both 1 and 2 at the same time. If the intention of the program is only to loop when x is neither 1 nor 2, we need to use the && instead of || operator. The correct code is shown below.
|
Loops while do...while for Control Structure Loop Design Methodology Common Loop Errors |
||
Fook Yuen Sin - Team 1 |