Home > OS >  Why does this while loop keep looping even when its false? In C
Why does this while loop keep looping even when its false? In C

Time:01-23

This is the code in question:

int integer;
while (integer != 0 || integer != 1) {
            cout << "Choose an integer: \n0\n1\n";
            cin >> integer;
            }

When I type 1 it continues looping even though the statement is false. I have had this problem before or similar but it got fixed in a weird way that seems to not be working right now. The other code that was having problems was this one:

while(chosen != 1 || chosen != 2 || chosen != 3)
    {
      cin >> chosen;
    }

I got it fixed by doing this:

while(chosen < 1 || chosen > 3)

Does annyone know whats happening here? Ty in advance!

CodePudding user response:

let me put you out of your misery

while(chosen != 1 && chosen != 2 && chosen != 3)
    {
      cin >> chosen;
    }
  •  Tags:  
  • Related