The logic of logical operators?

I am having trouble understanding the logic of a couple of logical operators.

I will show you an example in a context below that will (hopefully) illustrate my confusion.

Take this snippet of code for example:

1
2
3
4
5
6
7
8
cout << "Choose either a or b";
cin  >> choice;

while (choice != 'a' && choice != 'b')
{
  cout << "Wrong choice, try again ";
  cin  >> choice;
}


As you can see, what I am (successfully) doing here is prompting the user for input for as long as he/she does NOT enter a or b. Once a or b is entered, the program continues as it should.

Now, while the previous example works (I found that out the hard way via trial and error and a lot of swearing), the example below does not:

1
2
3
4
5
while (choice != 'a' || choice != 'b') // The difference is the || operator
{
  cout << "Wrong choice, try again ";
  cin  >> choice;
}


I guess I could just be pleased with the fact that the first example works, and the latter doesn't. But I am not. I want to understand why. Because; to me it seems like the latter example makes much more sense – logically, than the first. I want to use the || operator, because supposedly it equals ”or”. In pseudo code (to me) the last example looks like:

While user does not enter 'a' OR 'b';
do the loop.

This seems logical to me, but it doesn't work. The first example in pseudo code looks to me like:

While user does not enter 'a' AND 'b';
do the loop.

This does not make sense to me, it seems illogical, but it works!

I hope you understand my ramblings. Like I said, this is not ”a problem” as such. I have found the right way to do it – now what I really want to know is why it works!!!

Thanks for reading this,

HMW
While user does not enter 'a' AND 'b';


Not quite. It's

While (user does not enter 'a' AND user does not enter 'b')

The outcome of an AND is TRUE only if both sides of the AND are TRUE.

TRUE AND TRUE -> TRUE
TRUE AND FALSE -> FALSE
FALSE AND TRUE -> FALSE
FALSE AND FALSE - > FALSE

while (choice != 'a' || choice != 'b')


This one will evaluate to true whenever the choice isn't 'a' OR whenever the choice isn't 'b'

So, if the choice is 'a', then the choice isn't 'b', so the right hand side is TRUE
If the choice is 'b', then the choice isn't 'a', so the left hand side is TRUE.

There is no choice of letter where at least one of the sides won't come out to TRUE.
Last edited on
Hello again Moschops!

This gives me a headache (and I have read Wittgenstein and all!). So, the reason:

1
2
3
4
5
6
7
8
cout << "Choose either a or b";
cin  >> choice;

while (choice != 'a' && choice != 'b')
{
  cout << "Wrong choice, try again ";
  cin  >> choice;
}


works, is that for as long as the user enters 'q' for instance, both my arguments are ”true” (as in ”q does not equal neither a or b”)?

Thanks for answering!

HMW

Edit: I just saw your edit. I think I got it thanks. Will mark as solved. Again, thanks for replying, I appreciate it.
Last edited on
Topic archived. No new replies allowed.