When to use And, AndAlso and Or, OrElse? [duplicate]
I don't know when to use the syntaxes Or
or OrElse
and/or And
or AndAlso
, as I don't understand what difference this makes in the logic circuit.
Being in C#, And
= &
, AndAlso
= &&
e Or
= |
, OrElse
= ||
.
I need to test the following expression to know if the integer 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 should I use?
1 answers
Good Afternoon.
& and && are the same comparisons, however:
If you have a Expression1 & Expression2 , it will check both, soon you will spend more time.
If you have Expression1 && Expression2, it will check the first and will only check the second if the first is true.
If you are studying, use either, but there will come a time when the speed of your code will influence.
Or and OrElse follows the same logic.