| Meden (39) | |||
Hi. I am just starting to learn C++ just as a hobby. I am working on a very basic program right now. I am wondering if anyone would tell me what is wrong with my while conditions. From what I have read, entering in || should make the loop run while neither gullible is 5 nor iteration is 10, but what happens is that neither condition is recognized. The loop continues forever.
| |||
|
Last edited on
|
|||
| Zhuge (2880) | |
|
|| is not the "nor" operator, it is the "or" operator. So you will continuing looping so long as either gullible is not 5 OR iteration is not 10. In other words, you will only leave the loop when both gullible is 5 and iteration is 10. Also, on line 19 I think you meant to compare gullible to 5, not assign it. Use == instead. | |
|
|
|
| Meden (39) | |
|
Thanks for that. :) For some reason my brain momentarily cannot wrap around the idea that it makes sense to use && there instead of ||, but I am sure I'll get used to it. | |
|
|
|
| Zhuge (2880) | |
"Neither X nor Y" can be written with the || operator if you like:!(X || Y)This is equivalent to writing (as you suggested) !X & !Y
| |
|
|
|
| Chervil (812) | |||||
|
It may help to look at the code from another angle: This code uses an infinite loop (one which never terminates, the only way out is via the break; statement).
If either the test at line 3 OR the test at line 6 is true, then the loop will terminate. And looking at it the converse way, both line 3 AND line 6 must be false, in order to execute the body of the code. Or to express the same code yet another way, this time using the not-equal condition,
else statement is executed and the loop terminates.
| |||||
|
|
|||||