Let's say I have this code (bool1, bool2 and bool3 are booleans [should be obvious)):
if (bool1 && bool2 || bool3)
When is this if-Statement true? So what if only bool3 is true and the other two booleans are false. So I want to know if it is equal to
if ((bool1 && bool2) || bool3)
or
if (bool1 && (bool2 || bool3))
I know I can simply put some more brackets in there, but my code would be shorter if not.
You need to check operator's precedence table for your language. For C++ it is:
http://en.cppreference.com/w/cpp/language/operator_precedence
13 && Logical AND
14 || Logical OR
bool1 && bool2 || bool3 is (bool1 && bool2) || bool3
It is not about if-statement it is about evaluating boolean expressions.
Related
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
I'm a bit stumped as to why... if (data.query && data.query != '[object Object]') works
but if (data.query && data.query != ('[object Object]' || data.query != 'undefined'))
How can you have multiple conditions inside of a jade if?
Checking for typeof on the data.query for the undefined did the trick.
if (data.query && data.query != '[object Object]' || (typeof data.query !== 'undefined'))
Is it possible to write a one-line if then else statement in a language that supports short-circuiting? It doesn't have to be language specific, just pseudocode.
In case you didn't know, short-circuiting means that a language will evaluate exp1 || exp2 || exp3 || exp4... (|| is the or operator) by first executing exp 1, then if it returns true, execute exp 2, and so on... However, the moment exp n returns false, it does not continue evaluating exp n+1 and anything after that.
Let's say you want to express:
if p then f() else g()
Using only || and &&, both short circuiting. You can do that like this:
(p && ( f() || 1 )) || g()
To test it, a quick script:
$ perl -E '$p=1; ($p && ( f() || 1 )) || g(); sub f { say "f() called" } sub g { say "g() called" }'
f() called
$ perl -E '$p=0; ($p && ( f() || 1 )) || g(); sub f { say "f() called" } sub g { say "g() called" }'
g() called
$
In C, C++, C# and Java, you can do this (all 4 support short circuiting):
a = a>b ? a : b;
This would assign a the bigger of the two, either a or b.
This is called the ternary operator. First comes the condition to test a>b, then a question mark, then the true value (e.g. a), then a colon, then the false value (e.g. b).
You can't by simply short circuitry. That's meant to shorten the number of conditions being evaluated by skipping all conditions after the first that would make the whole condition true or false (based on the operator).
E.g.:
boolean x = true || 1 == 2;
The || evalutation won't check the value of 1 == 2, since true will already turn the || true, no matter what follows.
In a similar way for &&:
boolean y = 2 < 3 && 5 > 6;
To expand a bit on that, I could write:
boolean y = 2 < 3 && 5 > func();
In either case, the second half of the condition won't be checked (and func() won't be invoked) because the first half gives false.
There's no way to do something else, only with this behaviour.
In CoffeeScript, is there a way to simplify the following:
if(value === "something" || value === "else" || value === "wow"){}
I've tried:
if value is "something" or "else" or "wow"
But this produces the literal output of this:
if(value === "something" || "else" || "wow){}
Is there a way to check if a string is one of multiple values (OR or AND) in CoffeeScript?
I think you probably want
if value in ['something', 'else', 'wow']
I have to write an ADT character stack to handle postfix notation for boolean values.
This is an example of one of the postfix notations.
T T && F || ! ( this will be the input text)
I know that this evaluates to false.
My question is.
What does this notation end up looking like in code.
(once the input string has been parsed)
My guess is:
if ( !( true && true || false) )
//do something;
else
//do something else
I'm fairly sure this is wrong and I am way off the mark,
I have tried all sorts of combinations.
I can't get it to return false.
You usually interpret RPN stack-wise, with binary operators popping two arguments off the stack, and unary one.
T T — pushes two true values onto the stack, stack is: T T,
&& — pops two values, pushes and: T && T, stack is: T,
F — pushes false onto the stack, stack is: T F,
|| — pops two values, pushes or: T || F, stack is: T,
! — pops one value, pushes negated version: !T, stack is: F.
So your final result is the result of these operations (written from bottom to top):
bool result = !(false || (true && true));
You just need to read it from the end; it might be a bit easier when converted to prefix notation first (from bottom to top):
! ( || ( F && ( T T ) ) )
Then you an just move the binary operators inside the parentheses:
! ( || ( F && ( T T ) ) )
-----> ----->
! ( F || ( T && T ) )
Your main problem shoud be because you're returning the opposite from what the if condition evaluates to (see ablm comment). But in general you should wrap every evaluated expression in parenthesis to ensure a correct result:
return !((true && true) || false);
This way, even if the precedence of the operators in your target language (C/C++) is different from the intended order of computation, the expression will still be evaluated how you want to.
if ( !( true && true || false) ) <-- this is false
return false; <-- this evaluates if expression inside if is true, which is not
else
return true; <--- if expression inside if is false then this(true) is returned.
So for proper return type you will need to interchange returns.
if ( !( true && true || false) )
return true;
else
return false;