2
I don’t know when to use the syntax Or
or OrElse
and/or And
or AndAlso
, because I don’t understand what difference it makes in the logic circuit.
Being in C#, And
= &
, AndAlso
= &&
and Or
= |
, OrElse
= ||
.
I need to test the following expression to know if the whole lastToken
is zero (0) and also if it is greater than 2:
if(lastToken == 0 & lastToken > 2) { ... }
But I don’t know if I use &
or &&
.
Which one should I use?
Let me get this straight,
&
checks the two expressions if both aretrue
, the expression itself is true, if one is false, the expression is false?– CypherPotato
Yes, and that could be a waste of time. Logically if an expression with AND has at least one False, there is no need to check the other, so there is &&, it checks the first and ends there even if it is false.
– user92257
Did you forget to mention that
&
is an arithmetic operator while&&
is a logical operator. The expression21 & 1
is valid but the expression21 && 1
is not valid.– Bruno Costa
I particularly don’t like to use the operator
&
(that will always be called to mebitwise-and
) if it is not an operation that I know beforehand that I will do operations on the bits. Like, when I want that3 & 1
be true and6 & 1
be false– Jefferson Quesado