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;
Related
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.
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.
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.
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.
I'm just starting out using Lua, and I was wondering (because I can't find it on the website) if Lua has a OR operator, like how in other languages there is ||:
if (condition == true || othercondition == false) {
somecode.somefunction();
}
whereas in Lua, there is
if condition then
x = 0
end
how would i write an IF block in Lua to use OR?
With "or".
if condition or not othercondition then
x = 0
end
As the Lua manual clearly states.