Why is `!!(a||!a)` always true? - boolean

I'm a Java rookie and in School we got some homework. I shall explain boolean terms but I don't understand one of them.
The question is
Why is this expression always true?
!!(a||!a)
I understand the part in the brackets but what do the two exclamation marks in front of it?
If the first a = true --> !a = not true --> !! ( double negation = true?) a = true and the second !a = not true --> !!a = true --> !!!a = not true
and if I'm right , why is this expression alsways true? That beats me.
Could any of you explain that to me?
Thanks for your help!

First off, !! expression cancels out: it’s the same as expression, because the negation of a negation is the original value.
So we’re left with a || ! a, which is a disjunction. So the result is true if at least one of the sub-expressions a or !a is true.
And lastly, a is true if a is true (duh). And ! a is true if a is false. Thus, regardless of the value of a, the overall expression is true.

If "a" is TRUE add "!a" is FALSE
Knowing a is true and !a is false (! is for the negation; in this case, negating true -> false)
If a||!a means true or false, and from that expression you get true...
You can see it like this:
!!(true).
What is the result of a double negation? True.
Then, true(true) = (aka a||!a), which finally makes your expression !!(a||!a) always true.
If "a" is FALSE and "!a" is TRUE
Knowing a is false and !a is true (! is for the negation; in this case, negating false -> true)
If a||!a means false or true, and from that expression you get true...
You can see it like this:
!!(true).
What is the result of a double negation? True.
Then, true(true) = (aka a||!a), which finally makes your expression !!(a||!a) always true.

!!(a||!a)
Using one of the axioms of Boolean Algebra, a double negation cancels out everything.
so it would be equivalent to say (a||!a).
Assuming, we set a = true; many programing languages use lazy evaluation of conditional statements, the conditional statement would evaluate to true and execute whatever was inside the body of the if statement.
If we set a = false , then the right hand side of the conditional statement becomes true... as !a is the inverse of a meaning if a == false, !a == true.

Related

DART, How to perform "Greaterthan and No Equal" together?

I am new to dart,
I was doing experiment with dart and suddenly got a problem,
I am using this logic to obtain it, correct me if i am wrong.
int a = 8;
int b = 7;
if (!(a <= b)){
print("A is Less");
}else{
print("B is Less");
}
Comments on your question recommend ">" which is correct, but I thought I could help you understand your sample if construct and why it prints the opposite of what is accurate.
Any conditions inside an if must evaluate to a single boolean: true or false. In your if you test "a is less than or equal to b", which will yield 'true' or 'false', then you invert that boolean value with the boolean "!" not operator. Boolean 'not' changes true to false, and false to true.
Given the values you set for a and b, your if statement therefore checks if a is less than or equal to b, which is "false", then inverts that "false" to "true" using "!" not, and therefore prints "A is Less", which is an incorrect statement.
FWIW, your logic will also print "B is less" when a = b;

C# Bool operand usage

I want to have the following results
A B =
T T T
F T F
T F F
F F F
I am using this clause to achieve it
A || B
but it does not provide correct results.
Am I using a wrong operand?
Allthough all other answers are correct, I'd like to point out that the operator you are asking for is neither || nor &&. The operator that actually does exactly what you are asking for is & (and the equivalent that you are mistakenly using would be |).
And, what is the difference? || and && are short circuiting operators. What does that mean? It means that whatever is on the right side of the operator is only evaluated if the left hand side is true. This does not happen with the non short circuit versions of the operators (the truly bolean logical and and or operators):
public bool True()
{
Console.WriteLine("True called.");
return true;
}
public bool False()
{
Console.WriteLine("False called.");
return false;
}
var b1 = False() && True(); //b1 will be false and "False called." will be
//printed on the console.
var b2 = False() & True(); //b2 will be false and "False called. True called."
//will be printed on the console.
You want an and, but you used an or.
Am I using a wrong operand?
Yes you are.
You need the AND && operator which will be true only if all of the conditions are true.
You are using the OR || operator which will give you true even if one of the conditions are true.
Use the AND && operator instead of OR ||
Use && (which means 'and')
|| means 'or'

Computing && and ||

I have a value in Scala defined as
val x = SomeClassInstance()
val someBooleanValue = x.precomputedValue || functionReturningBoolean(x)
functionReturningBoolean has a long runtime and to avoid recomputing functionReturningBoolean(x), I am storing it in x.precomputedValue.
My question is: if x.precomputedValue is true, will functionReturningBoolean(x) ever be computed?
More Generally: as soon as the compiler sees a value of true in an "OR" statement, will it even look at the second condition in the statement?
Similarly, in an "AND" statement, such as a && b, will b ever be looked at if a is false?
My question is: if x.precomputedValue is true, will functionReturningBoolean(x) ever be computed?
No. && and || in Scala short-circuit. You can tell from the documentation:
This method uses 'short-circuit' evaluation and behaves as if it was declared as def ||(x: => Boolean): Boolean. If a evaluates to true, true is returned without evaluating b.
More Generally: as soon as the compiler sees a value of true in an "OR" statement, will it even look at the second condition in the statement? Similarly, in an "AND" statement, such as a && b, will b ever be looked at if a is false?
Yes. All expressions in Scala must be well-typed statically, whether they will be executed at runtime or not.

Working with values of 0 and 1 (Boolean)

Is
f = a'ab;
the same as
f = 1
Is this possible? I got this when I was simplifying something.
If that's meant to represent (and I think this is the likely case):
NOT-a AND a AND b
then, no, it's false no matter the values of a or b. That's because one of a or NOT-a is definitely false, FALSE AND anything is false and the operation is associative: (a AND b) AND c == a AND (b AND c).
If it's meant to represent:
NOT-a OR a OR b
then, yes, it's true no matter the values of a or b. That's because one of a or NOT-a is definitely true and TRUE OR anything is true. The associativity rules also apply here.

Unable to add a condition to a while loop in Matlab

I have a while loop which looks like this:
while ((min_t_border>0) && (colided_border_num > 0) && (~(min_t>0)))
...
end
I want to add to it another condition: (exit_border_point ~= false) or (exit_border_point)
when I put ether of the conditions above in an if statement it works. But when I try to add it as an additional condition to the while, or even when I try to add another condition to the if, for example I've tried if ((exit_border_point ~= false) && (true)) it tells me:
"Operands to the || and && operators must be convertible to logical scalar values."
What am I doing wrong?
*exit_border_point gets ether a (3x1) vector or false
Since exit_border_point can be a vector, try using the any or all functions, like this:
if (~any(exit_border_point))
As you can probably guess, any returns true if anything in the array evaluates to true and all returns true if everything in the array is true. They're kind of like vector equivalents to || and &&.
For the condition to make sense in the context of an if or while statement, it should evaluate to a scalar.
Thus, you should write
all(exit_border_point)
(which is equivalent to all(exit_border_point == true)), if you want true if all are true. Replace all with any if you want to exit the while-loop as soon as any exit_border_point is true.
Note that && and || only work for scalars. They're short-cut operators in that the second statement won't be evaluated if the first one determines the outcome (e.g. evaluates to false in case of &&. If you want to element-wise compare arrays, use & and |.
If exit_border_point is a 3x1 vector then (exit_border_point ~= false) also returns a 3x1 vector, hence the error. Use this condition instead:
~isequal(exit_border_point, false)