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

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

Related

what is for (; e > 0; e >>= 1)? [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 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Can anyone explain to me what's happening in line number 4, and how to understand these types of loops in future.
I was solving this problem. I have used basic approaches like pow(2,n) and (1<<n), but it overflows. Then I got this solution, but I'm unable to understand that fourth line. I know how to use for() loops in C++, but I'm a bit confused because of starting, i.e. nothing is there i.e. for(; e > 0; e >>= 1).
long long modpow(long long b, int e)
{
long long ans = 1;
for (; e > 0; e >>= 1)
{
if (e & 1)
ans = (ans * b) % mod;
b = (b * b) % mod;
}
return ans;
}
The for loop has 3 components:
for (a; b; c) {
}
a runs at the start. The loop will break when b is no longer true, and after each iteration c is executed.

How to use OR operator on three operands? [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
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.

Checking equality/inequality of multiple variables [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 2 years ago.
Improve this question
Let's say we have four variables: int a, b, c, d;. I need to check if excatly two of them are equal.
So for example: 1 1 9 5 is true, but 3 9 8 4 and 3 3 3 1 are false.
Of course writing an if statement for this would take a lot of time, won't be easily readable and it would be easy to make a mistake writing it.
What is the best way to write such statement?
There are several ways to do this.
One involves explicitly checking all the conditions. Since you have 4 variables, you only 6 conditions to check. These can easily be counted.
int n = (a == b) + (a == c) + (a == d) + (b == c) + (b == d) + (c == d);
Then check if n is 1. This works because a boolean value will be converted into an int (value 1 for true, 0 for false).
Another possibility is to store them all in a container (like a vector), sort it, then count the number of adjacent identical values.

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.

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