This is a common boolean logic question. ||
means "or," which means "as long as one side of this is true, then the expression is true. So when you pass an uppercase 'N'
to c != 'n' || c != 'N'
the program says "well, 'N'
is not equal to 'n'
, therefore one side of the expression is true, therefore the whole expression is true and there is no need to check the rest of the expression." Even when you press lowercase 'n'
, the program says "well, 'n'
is equal to 'n'
, but it's not equal to 'N'
, therefore one side of the expression is true, therefore the whole expression is true. This is what is happening in your while loop.
On the other hand, &&
means "and" which means "both sides of the expression must be true when you pass an uppercase 'N'
to c != 'n' && c != 'N'
the program thinks "'N'
is not equal to 'n'
, but it is equal to 'N'
, therefore only one side of the expression is true, therefore the expression is false."
This gets confusing because if you were testing to see if the characters entered were equal to particular values you would use ||
.
Basically, when you would use ||
for a particular expression, and you want the opposite of that expression then you need to change to &&
. Likewise, if you would use &&
for a particular expression, and you want the opposite of that expression then you need to use ||
. This is one of DeMorgan's laws, which I would recommend you read up on so you can avoid having to rediscover each of them on your own.