How to use OR operator on three operands? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Any ideas, or tips (links to tutorials), are much appreciated, I'd be happy to take a reference if this has been addressed elsewhere.
I haven't been able to find anything referencing how to use the OR on three operands here is the question that I got wrong.
Let A = true, B = false, and C = true. Evaluate the following:
(3 != 5) && !(A || B || C)
Response: True
Score: 0 out of 1 No
Is this a trick question?

Firstly, evaluate the lefthand operand of (3 != 5) && !(A || B || C).
It is 3 != 5, which is true.
Then, evaluate the righthand operand of (3 != 5) && !(A || B || C).
It is !(A || B || C).
To evaluate this, let's evaluate the operand of ! operator, which is A || B || C.
|| operator has left-to-right assosiativity, so A || B || C is treated as (A || B) || C.
Now A is true, so A || B is true without seeing the value of B. You can say that A || B || C is true from this.
A || B || C is true, so !(A || B || C) is false. Therefore the original expression (3 != 5) && !(A || B || C) is false.

Related

What does `!((n % 5 != 0) || (n % 20 == 0))` trasnform into? and why? I don't seem to get it [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Title. I need to know what !((n % 5 != 0) || (n % 20 == 0)) transforms into and why. I say transforms because it has the ! in the beginning.
I tried transforming it to ((n%5==0) || (n%20==0)) but I am pretty sure this is not the right answer.
Thanks!!
Negation is harder than it looks.
"A or B" is true if at least one of A and B is true.
Thus its negation, "not (A or B)", must be true if neither A nor B is true, which is the same as both A and B being false.
That is, the negation is equivalent to "(not A) and (not B)".
And that leads you to !(n % 5 != 0) && !(n % 20 == 0), or (n % 5 == 0) && (n % 20 != 0).
This is one of DeMorgan's laws, which you can memorise, but they are not diffult to "discover" for yourself, and you just need to remember to "invert" the operation as well as the operands.
Assuming you mean to use DeMorgan's Law, you can distribute the NOT into the expressions by NOT'ing each expression and flipping OR's to AND's (and vice versa).
So
!((n % 5 != 0) || (n % 20 == 0))
Can become
(!(n % 5 != 0) && !(n % 20 == 0))
Which can become
((n % 5 == 0) && (n % 20 != 0))
Original: !((n % 5 != 0) || (n % 20 == 0))
Applying De Morgan's laws: (!(n % 5 != 0) && !(n % 20 == 0))
Making it clearer (assuming n is something like int and operators are not overloaded):
((n % 5 == 0) && (n % 20 != 0))
Now you have the result.

Get rid of if statement [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
In code I come to situation like this:
if (a && b || c && d || e && f || g && h){
// do something
}
Like this:
if len(env.workers) == 0 && env.minQueue.Len() == 0 || len(env.workers) == len(env.daemonList) && env.minQueue.Len() == 0 || len(env.workers) > 0 && len(env.workers) == len(env.daemonList) {
env.shouldStop = true
return nil
}
But it's hard to debug and find errors. Is there any way to use more friendly constructuion to replace such statement.
As #Eugene mentioned it's always good idea to break long expressions like this into multiple smaller expressions.
exp1 = a && b
exp2 = c && d
exp3 = exp1 || exp2
exp4 = e && f
exp5 = g && h
exp6 = exp4 || exp5
exp7 = exp3 || exp6
if(exp7){
//doSomething
}
This may look absurd in beginning but believe me it has long way to go, at any point you can come back to the above code and easily understand what's cooking there. In fact if you like using debuggers then doing this would make your life way easier.
Also in point of performance, all you are doing is making extra 7 boolean variables. It's insignificant when code readability is concerned. And the thumb rule for better code readability is naming your variable right, not exp1,2,....
You use len(env.daemonList), len(env.workers) and env.minQueue.Len() multiple times. Storing them in variables not only shortens up that long condition, but also gives you variables that can be referenced when debugging.
You could write it as:
w_len = len(env.workers)
d_len = len(env.daemonList)
q_len = env.minQueue.Len()
if w_len == 0 && q_len == 0 || w_len == d_len && q_len == 0 || w_len > 0 && w_len == d_len {...
Now, of course the problem here is that while shorter, the names aren't as descriptive. You could give them better names at the cost of verbosity. How much you want to lean in each direction is a matter of taste and context.
This also doesn't "get rid" of the if like the title states, but that's not always a great goal to have. ifs aren't necessarily bad.

Boolean and logical operators using characters, combining multiple tests [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm having an issue with boolean and logical operators. I'm trying to get wantsToppings to evaluate to true if toppings equals 'T' or 't', but this code evaluates to true, regardless of user input. I'm not sure what I am missing, but I know I gotta be missing something.
Thanks for any help.
cout << "Toppings wanted (T for toppings/N for no toppings)? ";
cin >> toppings;
if (toppings == 't' || 'T'){
wantsToppings = true;
} else {
wantsToppings = false;
}
You are missing how logical operators work. Here is what you do:
if (toppings =='t' || 'T')
and here is how it's done:
if (toppings =='t' || toppings == 'T')
You don't really need the complexity of the if either, it could just be:
wantsToppings = (toppings == 't' || toppings == 'T');
The expression
if (toppings == 't' || 'T')
does not mean to toppings is either of 't' or 'T', but rather essentially (it's in fact a little bit more complicated than this once you factor in lazy evaluation):
evaluates each sub-expression (the expression toppings == 't' and the expression 'T')
convert results of those expressions to boolean values if required
perform the logical or (||) of the above boolean values
Now 'T' is a char which gets promoted to the boolean value true, hence the result is always true.
As others have pointed out, the expression you are looking for is
if (toppings == 't' || toppings == 'T')

C++: Why does this logical expression evaluate to false? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
int a = 10, b = 12, c = 8
!((a < 5) || (c < (a + b)))
I just tried it in a compiler and it was false.
The inner expression:
(a < 5) || (c < (a + b))
evaluates a < 5 as false (since a is 10) and c < (a + b) as true (since 8 is less than 10+12). Performing a Boolean "or" operation on false and true gives you true.
And, given that the next thing you do to that value is the ! (inversion), that true turns into a false.
c < (a + b) == 8 < (10 + 12) == 8 < 22 == true
a < 5 == 10 < 5 == false
(a < 5) || (c < (a + b)) == false || true == true
!((a < 5) || (c < (a + b))) == !(true) == false

bool x = (A || B && C) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Is this expression equivalent to ((A || B) && C) or to (A || (B && C))?
For example:
Let A=1, B=0, C=0. What is the outcome of this expression? Is it 0 (case 1) or 1 (case 2)?
The && operator has higher precedence than the || in C++ (and most similar languages), so your second alternative is correct, i.e.:
(A || B && C) == (A || (B && C))
You can check out the precedence of all operators in C++ here:
http://en.cppreference.com/w/cpp/language/operator_precedence