# OR or AND

• 06-12-2002
vasanth
OR or AND
Well which of the followinf is correct for the below statement and why

i want to make sure that the user does not enter the key a b c and d. In case he enters these he should get an error messgae

if(input!='a' && input!='b' && input!='c' && input!='d')
cour<<"Error";

OR

if(input!='a' || input!='b' || input!='c' || input!='d')
cour<<"Error";

well logicaly the furst one seems to be correct saying that if the user enters A or B or C or D give the error message.. But this statement does not work.. The second statement works...

I know it is got to do with the OR gate and AND gates.. But please clarify
• 06-12-2002
Xmevs
In the first you say that if the user does not press a,b,c or d, he will get an error.
Use the second one.
• 06-12-2002
Monster
I say: use none... both are not correct.

In the first you say if the use does not press a, b, c or d he or she will het an error.
In the second you say always show an error.

Third statement: if user enter a OR b OR c OR d then show error
Code:

```if(input=='a' || input=='b' || input=='c' || input=='d')   cout<<"Error" << endl;```
• 06-12-2002
d00b
Monster is correct...... just read it to yourself and remember

&& - AND
|| - OR

So the code monster put up reads, "If input is equal to a or b or c or d, output error"
• 06-12-2002
Magos
Re: OR or AND
Quote:

if(input!='a' || input!='b' || input!='c' || input!='d')
cour<<"Error";

This will always be true. Using ||, the whole expression will be true if at least one of the parts are true.

Assume Input has the value 'a'.
Then Input != 'b' will be true, since a != b
Assume that Input has any value except 'a'.
Then Input != 'a' will be true according to the assumption above.

Conclusion: The expression will always be true, since one of Input != 'b' and Input != 'a' will always be true.

(I'm learning Logics in school, this was a nice excercise :))
• 06-12-2002
Shiro
>i want to make sure that the user does not enter the key a b c
>and d. In case he enters these he should get an error messgae

In logic it often helps to describe a problem in a different way. You want an error message appear when the user has entered a or b or c or d. In other words: if the user enters a or b or c or d, then an error message must be printed.
• 06-12-2002
Zewu
Even I consider this as easy!

The only thing about these logic operators which I can consider as a problem is where to place the !, whenever you want to invert an expression.
• 06-12-2002
Shiro
If the expression is

a && b

the the inversion is

!(a && B)

but using the rules of logic, it is the same as

!a || !b

So you can place the inverter wherever you want, but always take good care of the used logic operators. If you're not used to the laws of logic, a thruth table is always very useful.