0
multiple conditions in a while loop
I am having trouble breaking the loop using the second condition. The loop only breaks when the first condition is satisfied. Does anyone have any suggestions? do { cout << "\nEnter a number other than 5: "; cin >> guess; ++a; } while ((guess != 5) || (a == 10));
2 Antworten
+ 3
So I figured out that I had to do this instead.
while ((guess != 5) && (a != 10))
🤦♂️
+ 2
There's two ORs available (at least in java)
| = OR
|| = short-circuit OR
To understand why, consider the following. In an AND operation, if the first evaluation is false, the outcome will always be false no matter what the second evaluation is.
Compiler will save time and won't check second condition as soon as it encounters something like this:
"false &&" will always result in false
try using OR ( single line | ) instead of short circuit OR and it should evaluate both conditions.
Source:
Java - A beginners guide book