Beginner here: How to use the 'or' operator

Thread Starter

sailmike

Joined Nov 11, 2013
147
I teaching myself how to program in C++ from the book "Jumping into C++". I'm stuck on one of the lessons. I have to have a user select from a list and to keep reprinting the list until the user selects something from the list. I am to use a 'for', 'while', or 'do-while' loop. I picked a 'while' loop as follows:
while( input != 1 || input != 2 || input !=3 )

But this doesn't work. The loop never exits.

Thanks,
Mike
Learning C++ OR.jpg
 

djsfantasi

Joined Apr 11, 2010
9,156
Your input would have to be equal to 1,2&3 at the same time, for your expression to be false. This isn't going to happen.
When Or'ing, all terms in the expression must be false for the result of the Or to be false.
For that to happen in the first term (input!=1), input must be equal to 1. For it to happen, input must equal 2 in the second term. Similarly for the third term. But...! Input cannot both be equal to 1 and 2 (same holds for 3). Hence, there is no way your expression will ever be false!
 

Thread Starter

sailmike

Joined Nov 11, 2013
147
You just described an AND operator. I'm an electrical engineer and one of the things we learn are logic circuits. In a 3 input OR gate, any one of the inputs has to be one for the output to be one. In a 3 input AND gate, all of the inputs have to be one for the output to be one. So, I'm not seeing how all inputs have to be 1, 2, and 3 at the same time. I thought that line read, loop as long as the input doesn't equal 1, 2, OR 3.

I hope this doesn't sound hostile.
 
Last edited:

djsfantasi

Joined Apr 11, 2010
9,156
Sounds like an AND operator, because your tests are for not equal rather than equal. Negated inputs are a requirement when making an OR gate from NAND gates. Just a coincidence. I am describing an OR gate. Check out DeMorgans Law.

Don't confuse the logical values of 1 and 0, with your inputs which appear to be the integers 1,2&3.

The line reads "loop as long as the input doesn't equal 1 OR the input doesn't equal 2 OR the input doesn't equal 3"

So you want to exit the loop if, for example, input equals 2? Let's walk through the logic. (T=True; F=False)
Input not equal 1? T
Input not equal 2? F
Input not equal 3? T

T OR F OR T = T

So execution remains in the loop.
Now look at this expression:
!(input==1 || input=2 || input==3)

Walk through this as before. Input is equal to 2.
Input equal 1? F
Input equal 2? T
Input equal 3? F

F OR T OR F = T
Not T = F

Execution exits the loop. To be sure operation is what you expect, let's walk through with input equal to 4.

Input equal 1? F
Input equal 2? F
Input equal 3? F

F OR F OR F = F
Not F = T

Execution remains within the loop.
 

vpoko

Joined Jan 5, 2012
267
Sailmike, just follow the loop in your head. Let's say they select 1. The loop has three conditions, if any one of them is true, it repeats. So in this case, it evaluates is input != 1: This is false (since they selected 1), so far so good. Next it evaluates input != 2: this is true, since they selected 1. So the loop continues. You only want the loop to continue if they didn't select 1 AND they didn't select 2, AND they didn't select 3, so change || to &&.
 
Top