When to use And, AndAlso and Or, OrElse? [duplicate]

this question already has answers here : is there equivalent to AndAlso and OrElse in C#? (2 responses) Closed 3 years ago .

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?

Author: CypherPotato, 2017-09-24

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.

 2
Author: , 2017-09-24 18:16:26