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

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

Related

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.

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.

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

c++ validate range of characters [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
I'm writing a function that is passed a character as the first argument and an integer as the second. I need to validate both simultaneously. Specifically, the character must be between A and J (including A and J), and the integer must be between 1 and 10 (including 1 and 10).
The line I wrote is:
if (toupper(row) < 'A' || toupper(row) > 'J' || col < 1 || col > 10)
{
return 0;
}
else
{ ... rest of function ... }
but this is not working correctly. In my book I read that you could perform comparisons with characters because they really are just integers themselves, but I can't figure out why ths won't work. Could anyone point me in the right direction?
Edit to address some comments
0 is the number we're supposed to return if the input is not valid.
This line of code is part of a project that is being graded by a "test driver" that my teacher wrote. The test driver is reporting that my function is not returning the correct result when the input is invalid (character that is not between A or J, or a number that is lower than 1 or greater than 10).
I structured my code so that if the statement above is true, then it returns the code we were supposed to return, otherwise it proceeds with the rest of the function... So I can't figure out why his test driver is telling me that I'm not returning the code when given invalid input. The other problem is he doesn't let us see what the test driver is sending to our function, so I have no way of trouble shooting this.
I think that you shouldn't use toupper. Why?
Because maybe your professor use invalid input like:
a, 5
and you shouldn't allow lower cases to pass your test.
So in the end your if statement:
if ((row >= 'A' && row <= 'J') && (col > 0 && col < 11))
From your post it is not clear what does not work. You wrote if statement without any compound statement. So what is the criterion that something is wrong?!
For example you could write
if (toupper(row) < 'A' || toupper(row) > 'J' || col < 1 || col > 10) return false;
Take into account that negation of expression
if ((toupper(row) >= 'A' && toupper(row) <= 'J') && (col > 0 && col < 11) )
as it is written by #Ardel is equivalent to
if ( !( ( toupper(row) >= 'A' && toupper(row) <= 'J') && (col > 0 && col < 11 ) ) )
that in turn is equivalent to
if ( !( toupper(row) >= 'A' && toupper(row) <= 'J') || !(col > 0 && col < 11 ) ) )
that is equivalent to
if ( !( toupper(row) >= 'A' ) || !( toupper(row) <= 'J') || !(col > 0 ) || !( col < 11 ) )
that is equivalent to
if ( toupper(row) < 'A' || toupper(row) > 'J' || col <= 0 ) || col >= 11 )
that is at last equivalent to
if (toupper(row) < 'A' || toupper(row) > 'J' || col < 1 || col > 10) return false;
That is your original expression.
So there is no any sense in your post and in the answer of #Ardel.
So I do not understand for example why the answer of #Ardel was uo voted. Maybe it was up voted by whose who is unable to do such conversions as the negation of boolean expressions?:)
I can suppose (moreover after thinking about I am sure) that you should not apply function toupper to the character. For example
if ( row < 'A' || row > 'J' || col < 1 || col > 10) return 0;
The other problem is that you did not say what the function shall do if this condition will be passed successfuly. Maybe inside the function body you should reassign row the following way
row -= 'A';
that to use it as integer value between 1 and 10 inclusively.

C vs. Python - operator precedence in conditional statements [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How does the C process a conditional statement such as n >= 1 <= 10?
I initially thought that it would get evaluated as n >= 1 && 1 <= 10, as it would be evaluated in Python. Since 1 <= 10 is always true, the second porition of the and is redundant (the boolean value of X && True is equivalent to the boolean value of X).
However, when I run it with n=0, the conditional gets evaluated to true. In fact, the conditional always seems to evaluate to true.
This was the example I was looking at:
if (n >= 1 <= 10)
printf("n is between 1 and 10\n");
>= operator is evaluated from left to right, so it is equal to:
if( ( n >= 1 ) <= 10)
printf("n is between 1 and 10\n");
The first ( n >= 1 ) is evaluated either as true or false, which is equal to either 1 or 0. Then that result of 1 or 0 is compared to result <= 10 which will always evaluate to true.
Thus the statement printf("n is between 1 and 10\n"); will always be printed
It's evaluated left to right like this:
n = 5;
if (n >= 1 <= 10)
// then
if (1 <= 10)
// then
if (1)
It first checks if n >= 1. If it is, it evaluates to 1, otherwise 0. This leads to the next evaluation, 1 <= 10, which evaluates to 1 as well. Note that this also succedes:
n = 5;
if (n >= 3 == 1)
Because it's evaluated like this:
n = 5;
if (n >= 3 == 1) // but you should never write code like this
// then
if (1 == 1)
// then
if (1)
Also note why it works with n = 0
n = 0;
if (n >= 1 <= 10)
// then
if (0 <= 10) // 0 isn't greater or equal to 1, so 0 (false) is "returned"
// then
if (1) // but 0 is less than or equal to 10, so it evaluates as 1 (true)