Question

#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?

Was it helpful?

Solution

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 || (e.g., "I want to know if 'a' or 'b' or 'c' was entered").

Basically, when you would use || for a particular expression, and you want the opposite of that expression then you need to change to && (e.g., I want none of 'a', 'b' or 'c'; or to put it another way, the value cannot be 'a' and it cannot be 'b', and it cannot be 'c'"). 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 De Morgan's laws, which I would recommend you read up on so you can avoid having to rediscover each of them on your own.

OTHER TIPS

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')".

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

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!

I understand your problem well,and here is the explanation:

The do-while loop is an exit-condition loop. This means that the body of the loop is always executed first. Then, the test condition is evaluated. If the test condition is TRUE, the program executes the body of the loop again. If the test condition is FALSE, the loop terminates and program execution continues with the statement following the while.

in your code,when you press'n' or'N',your test condition will be always one true and one false, so when you use || you will satisfy the test condition (true||false=true) ,so the program will execute the body of the loop again. but when you use && ,this will gives (true && false =false),the test condition is not statisfied anymore,so the program will not execute the body of the loop again.

I hope that's helpful.....enjoy programing! Ameraradi

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top