In the below given code, why the || logical doesn't work, instead the loop terminates specifically when && is used ?
int main() {
char select {};
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while (select != 'q' && select != 'Q'); // <--- why || (or) doesn't work here ??
return 0;
}
This loop will go on while select is not q and it's not Q:
while (select != 'q' && select != 'Q');
This loop will go on while select is not q or it's not Q.
while (select != 'q' || select != 'Q');
Since one of them must be true, it'll go on forever.
Examples:
The user inputs q
select != 'q' evaluates to false
select != 'Q' evaluates to true
false || true evaluates to true
The user inputs Q
select != 'q' evaluates to true
select != 'Q' evaluates to false
true || false evaluates to true
You want to terminate the loop when select is equal either to 'q' or 'Q'.
The opposite condition can be written like
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( not ( select == 'q' || select == 'Q' ) );
If to open the parentheses then you will get
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( not( select == 'q' ) && not ( select == 'Q' ) );
that in turn is equivalent to
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while ( select != 'q' && select != 'Q' );
Consider the following diagrams:
The full ellipse are all characters. The white dots is q and Q respectively. The black filled area depicts characters that will make the expression true. First line is select != 'q' && select != 'Q', second line is select != 'q' || select != 'Q'.
&& means both conditions must be true. The resulting black area is the overlap of the two areas on the left.
|| means either of the conditions must be true. The resulting black area is the sum of the two areas on the left.
Related
I have a loop in my code which looks like:
string var = ""
while (var != "q" || var != "Q")
{
...
cin >> var;
}
It does not work, my loop becomes unstoppable. But if I change my code to this:
while (var != "q")
or this:
while (var == "q" || var == "Q")
It`s gonna be working. What am I doing wrong?
while (var != "q" || var != "Q")
Written in plain English, this says: While var is not equal to q or var is not equal to Q run this loop.
This will always return true because var will always not be either q or Q (it can't simultaneously be both).
Your other conditions work because they're checking the right thing.
while (var != "q")
While var is not equal to q run this loop.
while (var == "q" || var == "Q")
While var is equal to q or var is equal to Q run this loop.
The condition you're looking for is to use the and operator.
while(var != 'q' && var != 'Q')
While var is not equal to q and var is not equal to Q run this loop. If var goes to q or Q, the loop will exit.
Since a variable can only have one value, it is always "not 'q' or not 'Q'".
This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 6 years ago.
Hello I am doing a very simple while loop in C++ and I can not figure out why I am stuck in it even when the proper input is give.
string itemType = "";
while(!(itemType == "b") || !(itemType == "m") || !(itemType == "d") || !(itemType == "t") || !(itemType == "c")){
cout<<"Enter the item type-b,m,d,t,c:"<<endl;
cin>>itemType;
cout<<itemType<<endl;
}
cout<<itemType;
if someone can point out what I am over looking I'd very much appreciate it. It is suppossed to exit when b,m,d,t or c is entered.
Your problem is in your logic. If you look at your conditions for your while loop, the loop will repeat if the item type is not "b" or not "m" or not "d" etc. That means if your item type is "b", it is obviously not "m", so it will repeat. You want to use && instead of ||.
As other answers and comments wrote correctly your logic is wrong. Using find() would simplify your task:
std::string validCharacters( "bmdtc" );
while ( std::string::npos == validCharacters.find( itemType ) )
{
...
}
This solution is more general and easier to read. See also documentation of std::string::find
The boolean expression to exit the loop is flawed. The way it is, in order to exit the loop the itemType would have to be all those letters at the same time. Try to instead || the letters first, and then negate it:
while(!(itemType == "b" || itemType == "m" || itemType == "d" || itemType == "t" || itemType == "c")
try this
string itemType = "";
while(!(itemType == "b" || itemType == "m" || itemType == "d" || itemType == "t" || itemType == "c")){
cout<<"Enter the item type-b,m,d,t,c:"<<endl;
cin>>itemType;
cout<<itemType<<endl;
}
cout<<itemType;
you condition is always true
#include <iostream>
using namespace std;
int main()
{
string player1, player2;
char choice1, choice2;
cout<<"Enter the name of player number one"<<endl;
cin>> player1;
cout<<"Enter the name of player number two"<<endl;
cin>>player2;
//choose choice
while((choice1 !='X')|| ( choice1 !='O' ) )
{
cout<<"Play number one choose X or O"<<endl;
cin>>choice1;
}
while loop never ends even when I enter X or O. I want it to end when either value is entered
That expression can literally never be false. I think you meant
while((choice1 !='X') && ( choice1 !='O' ))
The while statement you have will never be false. Change the || to &&:
while((choice1 !='X') && ( choice1 !='O' ) )
Think about it. Imagine choice1='O':
while((choice1 !='X')|| ( choice1 !='O' ) )
^true ^false == true
Now choice='X':
while((choice1 !='X')|| ( choice1 !='O' ) )
^ false || ^true == true
For more info on this check out De Morgan's Laws
it is because of the OR (||) condition. If you enter 'X', it evaluates false on the first expression but evaluates true on the second expression, so while loop will never be false. Use "&&" instead.
The following while loop has two conditions
cin >> user;
while (( user != 'X') || ( user != 'O'))
{
cout << "Please enter either X or O " << endl;
cin >> user;
}
After I enter either X or O, it keeps asking for a new input. I don't understand why? But if I remove on of the conditions it works properly.
Think about the logic of "this thing is not X, or this thing is not Y" — barring overlaps between X and Y, such a condition is always true, even in English!
You've been misled by the colloquial and subtly different "this thing is neither X nor Y", but your code is not "neither X nor Y", but "not X or not Y".
What you meant was "not X and not Y".
while (( user != 'X') && ( user != 'O'))
Use && (and) instead of || (or).
Your condition is always true...
while (( user != 'X') || ( user != 'O'))
If user is 'X' then user is not 'O' thus, even if the first part of the condition is not satisfied, the second part of the condition is satisfied. So the whole condition is true.
Same thing if useris 'O'.
Try this with a "logical and" (a.k.a. &&) instead of a "logical or".
It looks like you want logical AND (&&) instead of logical OR (||).
If you want to understand what happens more intuitively, reverse the logic of your expression:
!= becomes ==, and || (or) becomes && (and).
So what you wrote is:
Quit the loop if user equals 'X' AND user equals 'O'.
As you can see it's impossible for user to have both values at the same time.
What you want is:
while (( user != 'X') && ( user != 'O'))
{
cout << "Please enter either X or O " << endl;
cin >> user;
}
Well, || means or. If you read the condition aloud to yourself, it would sound like "While user is not X or user is not O. If you think about it, it will always be true - when it is X, it is also not O, and when it is O, it is not X. What you probably need is &&:
while (( user != 'X') && ( user != 'O'))
That way, the loop will stop when user is neither X nor O.
Thats because you're using the OR operator.
You're asking if user is not X or user is not O keep asking for input.
change it to && operator
I have a string vector of user-input data containing strings. Now I need to make sure program won't execute if strings are different than specified few. Vector contains 4 fields and every has different condition:
vector[0] can only be "1" or "0"
vector[1] can only be "red" or "green
vector[2] can only be "1", "2" or "3"
vector[3] can only be "1" or "0"
I tried writing if for every condition:
if(tokens[0]!="1" || tokens[0]!="0"){
decy = "error";
}
else if(tokens[1]!="red" || tokens[1]!="green"){
decy = "error";
}
else if(tokens[2]!="1" || tokens[2]!="2" || tokens[2]!="3"){
decy = "error";
}
else if(tokens[3]!="1" || tokens[3]!="0"){
decy = "error";
}
else{
switch(){} //working code
}
return decy;
It always enters first if and returns error. I tried with if instead of else if but it doesn't work either. I checked vector[i] contents and it returns correct strings. No " " at the end of it etc. Removing else and releasing switch just makes program check first condition and ignore rest of it.
I'm probably doing something terribly wrong, but I can't find an answer on internet so I decided to ask here.
This line:
if(tokens[0]!="1" || tokens[0]!="0")
should be:
if(tokens[0]!="1" && tokens[0]!="0")
^^
The same goes for the rest of the if statements as well.
The conditions are invalid.
Any distinct value can satisfy your conditions.
You should use && instead of ||.
For example:
if (tokens[0] != "1" || tokens[0] != "0") {
Consider this line. If tokens[0] is "1", which is valid input, it will not satisfy the first condition, but it will satisfy the second. You only want to throw an error when the value is neither of the valid possible inputs.
This means that your condition should be:
if (tokens[0] != "1" && tokens[0] != "0") {
Same goes for all the others.
You should turn those || into &&. If the input can only be X or Y, this means that it is illegal when it is not X and not Y:
if (tokens[0] != "1" && tokens [0] !="0")
// ^^
The first if:
if(tokens[0]!="1" || tokens[0]!="0")
ALWAYS evaluates to true.