Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ do while condition is not work as expected

Tags:

c++

I wonder why while (selection != 'q' && selection != 'Q') works but while (selection != 'q' || selection != 'Q') does not work. It will never terminate the loop. When I use else if (selection == 'q' || selection == 'Q' ) (with ||) it's working fine. Can someone help?

#include <iostream>

using namespace std;

int main()
{
    char selection{};
    do{
        cout << "\n--------------------------"<< endl;
        cout << "1.Do this" << endl;
        cout << "2.Do that" << endl;
        cout << "3.Do something else" << endl;
        cout << "4.Quit" << endl;
        cout << "\nEnter your selection" << endl;
        cin >> selection;

        if (selection == '1')
            cout << "You chose 1 - doing this" << endl;
        else if (selection == '2')
            cout << "You chose 2 - doing that" << endl;
        else if (selection == '3')
            cout << "You chose 3 - doing  something else" << endl;
        else if (selection == 'q' || selection == 'Q' )
            cout << "Goodbye" << endl;
        else
            cout << "Unknown option -- try again" << endl;
    }
    while (selection != 'q' && selection != 'Q');
}
like image 821
logan_92 Avatar asked Apr 12 '26 08:04

logan_92


2 Answers

Let's look at your proposed condition, which you are wondering why it doesn't work:

while (selection != 'q' || selection != 'Q')

In order for this while loop to terminate the loop, the above expression must evaluate to false. That's how a while loop works. In other words:

!(selection != 'q' || selection != 'Q')

must be true. Basic rules of boolean logic show that this expression is logically equivalent to

selection == 'q' && selection == 'Q'

which, obviously can never happen. The selection value can never be q and Q at the same time. Only Schrödinger's cat can do that.

like image 55
Sam Varshavchik Avatar answered Apr 14 '26 20:04

Sam Varshavchik


To add onto the answer. you could also use !(selection == 'q' || selection == 'Q') where in this case, when q, !(true||false)=false and when Q, !(false||true)=false and everything else, !(false||false)=true.

#include <iostream>

using namespace std;

int main()
{
    char selection{};
    do{
        cout << "\n--------------------------"<< endl;
        cout << "1.Do this" << endl;
        cout << "2.Do that" << endl;
        cout << "3.Do something else" << endl;
        cout << "4.Quit" << endl;
        cout << "\nEnter your selection" << endl;
        cin >> selection;

        if(selection == '1')
            cout << "You chose 1 - doing this" << endl;
        else if(selection == '2')
            cout << "You chose 2 - doing that" << endl;
        else if (selection == '3')
            cout << "You chose 3 - doing  something else" << endl;
        else if (selection == 'q' || selection == 'Q' ){
            cout << "Goodbye" << endl;
            break; //break in case of 'Q'||'q'
        }
        else
            cout << "Unknown option -- try again" << endl;
    }while(true);

Just to add on to the explanation already answered. This works as well.

like image 36
djacob Avatar answered Apr 14 '26 22:04

djacob