multiple conditions in a while loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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));

29th Jan 2020, 3:22 AM
Hector Garcia
Hector Garcia - avatar
2 Answers
+ 3
So I figured out that I had to do this instead. while ((guess != 5) && (a != 10)) 🤦‍♂️
29th Jan 2020, 4:06 AM
Hector Garcia
Hector Garcia - avatar
+ 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
29th Jan 2020, 3:56 AM
HNNX 🐿
HNNX 🐿 - avatar