I have an enum with 3 values:
enum InputState { Pressed, Released, Held };
And I'm using it in this code:
//GetState returns an InputState
if(myInput.GetState(keyCode) == InputState::Pressed)
{
//This means "keyCode" has the state "Pressed"
}
Why doesn't this work?
if(myInput.GetState(keyCode) == (InputState::Pressed || InputState::Held))
{
//This is always false
}
if((myInput.GetState(keyCode) == InputState::Pressed) || (myInput.GetState(keyCode) == InputState::Held))
{
//This works as intended, triggers when "keyCode" is either Pressed OR Held
}
As a test, I did:
//Using the same values from the enum, but as int now
if(1 == (1 || 2))
{
//This works as intended
}
Am I missing something?
|| is a binary operation that expects two boolean values. In your example, the boolean values are the result of testing for equality with ==.
To see why your simplified example works, let's evaluate the expression
1 == (1 || 2)
We must start inside the parentheses first, so we are going to first evaluate (1 || 2). In C++, any non-zero value is equivalent to true when it is used in a boolean expression, so (1 || 2) is equivalent to (true || true) which evaluates to true. So now our expression is
1 == true
Again, 1 is equivalent to true in this context, so this comparison is the same as true == true, which of course evaluates to true.
Yes, you are missing something. This worked by total accident:
(1 == (1 || 2))
It is NOT set comparison. It simply calculated (1 || 2) as true, then converted true to its integral value (1).
The same thing would have happened with
(1 == (1 || 4))
(1 == (0 || 1))
(1 == (0 || 4))
and they all are true.
But
(2 == (1 || 2))
is false.
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.
This question already has answers here:
using Boolean to represent integers
(4 answers)
Closed 7 years ago.
int x = 5;
bool t = ((x % 3) && (x % 4));
when I try this code in visual studio, I get the value true for variable t. How this code is working?
First of all, you have the && operator, which compares two values and returns a boolean, so t will b a boolean. Secondly, in C++, every value other than 0 will evaluate to true, so x % 3, which evaluates to 2 will return true, then x % 4, which evaluates to 1 will also return true.
So in the end you have
bool t = (x % 3) && (x % 4);
which equals
bool t = 2 && 1;
which equals
bool t = true && true;
which equals
bool t = true;
All nonzero values are considered as true for logical operations like AND(&&) and OR(||)
x%3 -s a module operation returned value 2,
x%4 returns 1
2 && 1 is equal to true && true , which is equal to true.
All non zero value assignment to a bool typed variable is considered as true or 1 and all zero value assignment considered as false or 0.
Operator % returns the remainder after division (modulo) and true is any value that is not 0.
5 % 3 == 2
5 % 4 == 1
2 && 1 == true && true == true
you can just do:
bool t=(x% 3!=0) && (x% 4!=0);
Confused about if condition, how does it executes following statements.
if(1 && (1 || 0) != 0) or if(1 || (1 && 0) != 0)
In above if statement what is the sequence of executing/validating the statements.
(left to right or right to left) if left to right, then if first argument/expression is true does it evaluates 2nd expression/argument? is it true for both the logical AND and OR operators.
Thanks.
Logical && short circuits if the first operand evaluates to false (because false && x is false for all x)
Logical || short circuits if the first operand evaluates to true (because true || x is true for all x)
They both evaluate left-to-right.
It's left to right
First executes 1. Then executes (1 || 0) != 0. To do that it executes 1 || 0 -> true, so the whole thing is true.
First executes 1 - it's true, so it short circuits and returns true.
It's left to right. || short-circuits if first expression is true, && if first expression is false.
Both things are fundamentally different go read D Morgans Laws!s
lets break it down step-by-step:
(1 || 0) becomes true as 1 short-circuits the expression
so (1 || 0) != 0 is true
1 && true is true by the definition of the logical && operator
or is a define/keyword for || but the first section is already true, so we short-circuit the expression again and the code inside the if block is executed.
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.
Given that x = 2, y = 1, and z = 0, what will the following statement display?
printf("answer = %d\n", (x || !y && z));
It was on a quiz and I got it wrong, I don't remember my professor covering this, someone enlighten me please... I know the answer I get is 1, but why?
The expression is interpreted as x || (!y &&z)(check out the precedence of the operators ||, ! and &&.
|| is a short-circuiting operator. If the left operand is true (in case of ||) the right side operand need not be evaluated.
In your case x is true, so being a boolean expression the result would be 1.
EDIT.
The order of evaluation of && and || is guaranteed to be from left to right.
If I'm not mistaken, it will print 1. (Let's assume short circuiting is off)
(x || !y && z) or (true || !true && false) will first evaluate the ! operator giving (true || false && false)
Then the &&: (true || false)
Then || : true
Printf will interpret true in decimal as 1. So it will print answer = 1\n
Given that x = 2, y = 1, and z = 0,
what will the following statement
display?
printf("answer = %d\n", (x || !y && z));
Ok - feeling a bit guilty for the harsh quip re poor wording of the question, so I'll try to help you in a different way to the other answers... :-)
When you've a question like this, break it down into manageable chunks.
Try:
int x = 2, y = 1, z = 0;
printf("true == %d\n", 10 > 2); // prints "1"
printf("false == %d\n", 1 == 2); // prints "0"
printf("!y == %d\n", !y); // prints "0"
printf("(x || !y) == %d\n", x || !y); // "1" - SEE COMMENTS BELOW
printf("(!y || z) == %d\n", !y || z); // "0"
printf("(x || !y && z) == %d\n", x || !y && z); // "1"
In the output there, you've got everything you need to deduce what's happening:
true == 1 reveals how C/C++ convert truthful boolean expressions to the integral value 1 for printf, irrespective of the values appearing in the boolean expression
false == 0 reveals how C/C++ converts false expressions to "0"
(!y) == 0 because ! is the logical not operator, and C/C++ consider 0 to be the only integral value corresponding to false, while all others are true, so !1 == !true == false == 0
(x || !y) == 1, and you know !y is 0, so substituting known values and simplifying: (2 || 0) == 1 is equivalent to (true or false) == true... that's understandable as a logical rule
(!y || z) == 0 - substituting known values: (0 || 0) == (false or false) == false == 0
(x || !y && z) == 1: here's the crunch! From above, we know:
x || !y is 1/true, which if relevant would imply 1/true && z/0/false == 1/true <- this clearly doesn't make any sense, so it must not be the way C/C++ are calculating the answer!
(!y && z) is false, which if relevant would imply x/2/true || false == 1/true <- this is true, so it must be the implicit order.
In this way, we've derived the operator precedence - the order of evaluation of the || and && operators, from the results that the compiler is displaying, and seen that if and only if && is valuated before || then we can make some sense of the results.
answer = 1
or maybe:
answer = -27
2 || !1 && 0
2 || 0 && 0
2 || 0
true
true = non-zero value
printf("answer = %d",*true*); -> who knows
Most compilers will output answer = 1. I wouldn't confidently state that all compilers will do that though, but I am confident all compilers would return non-zero.
I'm not going to give you the outright answer because you could just compile it and run it, but the question is just testing to see if you know operator precedence.