I have an if statement that looks as follows:
int count=0;
string Check;
if ((count==4 && Check!="-s")||(count==4 && Check!="-S"))
If count equals 4 and Check equals "-s" or "-S" it still enters this if statement because of the count == 4. It totally seems to ignore the second part. Is there something I'm doing wrong?
It's always going to be the case that either Check!="-s" or Check!="-S". Hence, your if statement is equivalent to if (count==4).
Well, if Check is "-S", then it will not even check the second pair of conditions, because you check with ||. The same holds true for the opposite case. If one is false, the other is true. Replace that with a &&.
int count = 4;
string Check = "-S";
if( (count == 4 && // count is 4, alright.
Check != "-s") || // Check is "-S", alright I'm done thanks to || (OR)
(count == 4 &&
Check != "-S") )
{
// ...
}
int count = 4;
string Check = "-s";
if( (count == 4 && // count is 4, alright.
Check != "-s") || // Check is "-S", time to check the other condition pair...
(count == 4 && // count is 4, alright.
Check != "-S") ) // Check is "-s", which is different from "-S", perfect.
{
// ...
}
Now the corrected version:
int count = 4;
string Check = "-S";
if( (count == 4 && // count is 4, alright.
Check != "-s") && // Check is "-S", different from "-s", now on to the other condition!
(count == 4 && // count is 4, alright.
Check != "-S") ) // Check is "-S"... oh dang! No executed code for you.
{
// ...
}
If count == 4 and Check == "-s", then the expression to the right of the || is true. If count == 4 and Check == "-S", then the expression to the left of the || is true. So you have true or true which is true. Thus, your if-block is executed.
The right statement is:
if(count==4 && (Check != "-s" || Check!="-S"))
The statement that you wrote is true if you have count = 4 and Check = "-S" because then the first part of the OR is true.
Might be more clear to use:
if (count==4 && Check!="-s" && Check!="-S")
You should use !strcmp(Check, "-s") and !strcmp(Check, "-S") instead of !=.
If you use == you compare the pointers and that is no what you want. The pointers will always be different thus your second argument will always be true.
You want to enter the if body if and only if Check is != from either -s or -S and count is = 4 right?
if ( (Check!="-s" && Check!="-S") && count==4 )
should work.
or
if ( Check.tolower() !="-s" && count==4 )
should work.
(Do not remember the name of the function to lowercase a string, you have got to look it up)
Hope this help.
Related
Similar question to:
How is the comma operator being used here?
BOOL bShowLoadingIcon = FALSE;
if (sCurrentLevelId_5C3030 == 0 || sCurrentLevelId_5C3030 == 16 || (bShowLoadingIcon = TRUE, sCurrentLevelId_5C3030 == -1))
{
bShowLoadingIcon = FALSE;
}
In the above code sample what values/range of sCurrentLevelId_5C3030 would cause bShowLoadingIcon to be set to TRUE. Is it possible it would be set to TRUE and also become true (overall if expression) thus also then being set back to FALSE?
I have no idea what (bShowLoadingIcon = TRUE, sCurrentLevelId_5C3030 == -1) is actually doing.
C++ only evaluates a boolean OR if it needs to. So if sCurrentLevelId_5C30303 is 0 or 16, the last statement never gets evaluated.
If (bShowLoadingIcon = TRUE, sCurrentLevelId_5C3030 == -1) does get evaluated, it first sets bShowLoadingIcon to TRUE and then evaluates to the result of sCurrentLevelId_5C3030 == -1. If that's true, then bShowLoadingIcon will just get set back to FALSE.
So in summary, bShowLoadingIcon is set to FALSE. Then if sCurrentLevelId_5C3030 is neither 0 nor 16, then bShowLoadingIcon is set to TRUE, only to be set back to false if sCurrentLevelId_5C3030 is -1.
So, in even more summary, bShowLoadingIcon is set to TRUE if sCurrentLevelId_5C3030 is neither 0 nor 16 and stays that way so long as sCurrentLevelId_5C303030 is not -1.
It's equivalent to:
BOOL bShowLoadingIcon = (
(sCurrentLevelId_5C3030 != 0) &&
(sCurrentLevelId_5C3030 != 16) &&
(sCurrentLevelId_5C3030 != -1)) ? TRUE : FALSE:
Or, if you prefer:
BOOL bShowLoadingIcon = (
(sCurrentLevelId_5C3030 == 0) ||
(sCurrentLevelId_5C3030 == 16) ||
(sCurrentLevelId_5C3030 == -1)) ? FALSE : TRUE;
In C++, the comma operator (statementX, statementY) first executes statementX, and then statementY. The expression holds the value of the second statement.
In your code, bShowLoadingIcon is assigned the value TRUE, and then the value that C++ checks for in the if statement is whether sCurrentLevelId_5C3030 == -1.
I have the following problem
in my app i have severeal if-statements
if ( (number >= 1 && number <= 18) && !strcmp("half1-18", _myBetCh) ) {
}
Now I realized that I have to split this condition because I need a boolean variable after one condition
bool success = false,
if(!strcmp("half1-18", _myBetCh) {
success = true;
if (number >= 1 && number <= 18) {
}
}
Is there a workaround to this? Is it possible, for instance, to make an assignment withing the if-statement?
It's possible, like this:
if ((success = !strcmp("half1-18", _myBatCh)) && number > 1 && number < 18)
but I personally think assignments in conditions are messy and hard to read, and prefer this variation:
bool success = strcmp("half1-18", _myBetCh) == 0;
if (success && number >= 1 && number <= 18) {
// ...
}
Well, there is:
if ( !strcmp("half1-18", _myBatCh) && (success = true, number > 1 && number < 18) )
or, obviating the need for the success = false earlier
if ( (success = !strcmp("half1-18", _myBatCh)) && number > 1 && number < 18 )
Your way is easier to read though, so I would consider sticking with what you've got.
I am attempting to write a loop that will repeat until the user enters one of the correct choices (either 1 or 0). For some reason when I have the loop written as below it creates an infinite loop.
I am intending for the loop to only execute while control is not 0 OR not 1, but for some reason it will always execute and becomes an infinite loop.
cout<<"Please enter 1 for another customer or 0 to quit : ";
cin>>control;
while ((control != 0 )|| (control != 1))
{
cout<<"Invalid Entry! Please enter a 1 to enter another customer or 0 to quit: ";
cin>>control;
}
I changed it to be while control less than 0 OR greater than 1, which works but I am still confused as to why the other loop is not working.
You have to use && operator.
while ((control != 0 ) && (control != 1))
(control != 0) || (control != 1)
is equivalent to,
!(control == 0 && control == 1)
but,
(control == 0 && control == 1)
is always false (there is no such number).
Therefore, the whole expression will always get true value.
The only way to break out
while ((control != 0 )|| (control != 1))
is
!(control != 0) && !(control != 1)
which is equivalent to
control == 0 && control == 1
which is impossible for all integers.
I'm increasing a pointer and decreasing a counter in a while loop. I want to exit the loop when the counter reaches zero or the pointer is equal to a max point (IF present). That if part is giving me the problem.
Consider the following:
char * pIt = utf8str;
char ** pEnd = 0;
size_t nVal = 10;
while ( 0 < nVal && (pEnd && pIt < *pEnd || true) )
{
--nVal;
++pIt;
}
If pEnd is null, I want it to be excluded from the comparison so pIt will have no upper bound. Obviously the above code doesn't work since when pIt >= pEnd the 'or' comes into play and will always be true.
How would one do this?
Ok let's deconstruct your condition a bit. So at the top level we have two parts the counter and the iterator and both conditions must be met to continue so we have:
A && B
Now A, is the simplest and thats: nVal > 0 (it's more natural at least for me to have what I'm comparing to on the right side).
Now be is a little bit trickier. Either pEnd is null and pIt has no upper bound, or pEnd exists. Which means B == C || D. So let's sub.
A && (C || D)
Now, if pEnd is null we want it to be true right? null is false, so we want true when false so what do we do ? !pEnd. That gives us C.
Finally we have D. Which is the case where pIt has an upper bound. pIt < *pEnd.
Let's put this all together and we have:
nVal > 0 && (!pEnd || (pIt < *pEnd))
Hope this helps!
How about:
while ( 0 < nVal && (!pEnd || pIt < *pEnd) )
If you are used to reading the ternary operator, this is pretty clear:
while (0 < nVal && (pEnd ? pIt < *pEnd : true))
Just:
((pEnd == NULL) || (pIt < *pEnd))
This is not intended as an answer but as a comment with code. Shakes fist at SOs lousy comment-code options
When you run into a brain-bending logic issue and sleeping on it is not an option, deconstruct with a for(;;) loop.
for (;;) {
if (nVal <= 0)
break;
if (pEnd == nullptr) {
// logic test you want here
}
--nVal;
++pIt;
}
#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?
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.
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'))