views:

431

answers:

6
#include<iostream>
using namespace std;

int main()
{

char again;

do
{
cout<<"you are in the while loop";
cout<<"do you want to continue looping?";
cin>>again;
}while(again!='n' || again!='N');

system("pause");
return 0;

}

i know something is wrong with the test condition in the 'while'. But i can't figure it out.

when the input of the user is neither 'n' nor 'N', the loop should keep on printing the code "you are in the while loop". Once 'n' or 'N' is pressed, the program will be terminated.

However for my code, program will keep on looping the code regardless what character i enter. But when i change the '||' to '&&', the program can ran as desired. Anyone can tell me what is going on?

+9  A: 

Yes: || is "or" and && is "and".

Every character is either "not 'n'" or "not 'N'" because it can't possibly be both 'n' and 'N' simultaneously.

Another (probably simpler to read) way of writing the condition would be:

!(again == 'n' || again == 'N')

which means "the opposite of (again is either 'n' or 'N')".

Jon Skeet
A: 

&& is a logical AND. || is a logical OR.

(also, & is a bitwise AND, and | is a bitwise OR.)

Jim H.
A: 

you might want to try while(!(again == 'n' || again == 'N'))

Pete
+8  A: 

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.

Max Lybbert
thanks. useful information
+7  A: 

It is the boolean algebra called "De Morgan's laws".

Not (P And Q) = (Not P) Or (Not Q)
Not (P Or Q) = (Not P) And (Not Q)

In your case, you want users not to enter 'n' or 'N', it can be translate in to this logic.

!(again == 'n' || again == 'N')

When applying De Morgan's laws, it will be

(again != 'n' && again != 'N')

For more info, you might want to read Propositional logic

chaowman
thank you very much.
+4  A: 

Although the original poster is happy now, I didn't see this in the other answers:

(true  && true ) == true
(true  && false) == false
(false && true ) == false
(false && false) == false

(true  || true ) == true
(true  || false) == true
(false || true ) == true
(false || false) == false

!true  == false
!false == true

That's everything!

Daniel Earwicker
Daniel LeCheminant
Okay, okay, I added parentheses! I didn't mean these to appear in programs as expressions. :)
Daniel Earwicker
Daniel LeCheminant
To understand recursion, first you must understand recursion.
Daniel Earwicker