C# Bool operand usage - boolean

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'

Related

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

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.

How can I assign a boolean condition result to a scalar variable in perl?

I am doing the following, but it is not working properly:
my $enabled = $hash &&
$hash->{'key'} &&
$hash->{'key'}->{'enabled'} &&
$hash->{'key'}->{'active'};
Is this an acceptable way to assign a boolean value to a scalar variable?
My code is misbehaving in strange ways, as it is, and I believe it is because of this assignment. I have validated that the individual values exist for all of these keys and are set to a value.
P.S. Sorry for being a noob! I googled for ~10 minutes and couldn't find the answer anywhere.
The Perl boolean operators like &&, ||, and, or don't return a boolean value, they return the value of one of their arguments:
say 2 && 3;
outputs 3.
You can force it to a boolean with the double negation trick:
say !!(2 && 3);
# or
say not not 2 && 3;
outputs 1.

MATLAB logical operators: && vs &

If I want to ensure that an if statement only executes if BOTH of two conditions are true, should I be using & or && between the clauses of the statement?
For example, should I use
if a == 5 & b == 4
or
if a == 5 && b == 4
I understand that the former is elementwise and the latter is capable of short-circuiting but am not clear on what this means.
For a scalar boolean condition I'd recommend you use &&. Short-circuiting means the second condition isn't evaluated if the first is false, but then you know the result is false anyway. Either & or && one will be true only if both sides of the expression are true, but & can return a matrix result if one of the operands is a matrix.
Also, I believe in Matlab comparisons should be done with ==, not with = (assignment).

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)

What's the difference between & and && in MATLAB?

What is the difference between the & and && logical operators in MATLAB?
The single ampersand & is the logical AND operator. The double ampersand && is again a logical AND operator that employs short-circuiting behaviour. Short-circuiting just means the second operand (right hand side) is evaluated only when the result is not fully determined by the first operand (left hand side)
A & B (A and B are evaluated)
A && B (B is only evaluated if A is true)
&& and || take scalar inputs and short-circuit always. | and & take array inputs and short-circuit only in if/while statements. For assignment, the latter do not short-circuit.
See these doc pages for more information.
As already mentioned by others, & is a logical AND operator and && is a short-circuit AND operator. They differ in how the operands are evaluated as well as whether or not they operate on arrays or scalars:
& (AND operator) and | (OR operator) can operate on arrays in an element-wise fashion.
&& and || are short-circuit versions for which the second operand is evaluated only when the result is not fully determined by the first operand. These can only operate on scalars, not arrays.
Both are logical AND operations. The && though, is a "short-circuit" operator. From the MATLAB docs:
They are short-circuit operators in that they evaluate their second operand only when the result is not fully determined by the first operand.
See more here.
& is a logical elementwise operator, while && is a logical short-circuiting operator (which can only operate on scalars).
For example (pardon my syntax).
If..
A = [True True False True]
B = False
A & B = [False False False False]
..or..
B = True
A & B = [True True False True]
For &&, the right operand is only calculated if the left operand is true, and the result is a single boolean value.
x = (b ~= 0) && (a/b > 18.5)
Hope that's clear.
&& and || are short circuit operators operating on scalars. & and | operate on arrays, and use short-circuiting only in the context of if or while loop expressions.
A good rule of thumb when constructing arguments for use in conditional statements (IF, WHILE, etc.) is to always use the &&/|| forms, unless there's a very good reason not to. There are two reasons...
As others have mentioned, the short-circuiting behavior of &&/|| is similar to most C-like languages. That similarity / familiarity is generally considered a point in its favor.
Using the && or || forms forces you to write the full code for deciding your intent for vector arguments. When a = [1 0 0 1] and b = [0 1 0 1], is a&b true or false? I can't remember the rules for MATLAB's &, can you? Most people can't. On the other hand, if you use && or ||, you're FORCED to write the code "in full" to resolve the condition.
Doing this, rather than relying on MATLAB's resolution of vectors in & and |, leads to code that's a little bit more verbose, but a LOT safer and easier to maintain.