Is there ever a circumstance in which I would not want to use the AndAlso
operator rather than the And
operator? …or in which I would not want to use the OrElse
operator rather than the Or
operator?
From MSDN:
Short-Circuiting Trade-Offs
Short-circuiting can improve performance by not evaluating an expression that cannot alter the result of the logical operation. However, if that expression performs additional actions, short-circuiting skips those actions. For example, if the expression includes a call to a Function procedure, that procedure is not called if the expression is short-circuited, and any additional code contained in the Function does not run. If your program logic depends on any of that additional code, you should probably avoid short-circuiting operators.
Is there ever a circumstance in which I would not want to use the AndAlso operator rather than the And operator?
Sure: if you want to make sure that both sides of the expression are evaluated. This might be the case if, for example, both sides are method calls that return booleans as a result of some other operation that has a side effect.
But in general, use AndAlso
/OrElse
whenever you would use &&
/||
in C/C++/C#, which of course is the vast majority of the time.
They are completely different functionalities in VB.net and both have their use cases (though for the most common use both will work)
AndAlso
and OrElse
are conditional operators, they return a true or false boolean value and only evaluate what they need to until they reach the result that cannot be altered (false for AndAlso, true for OrElse). These are the equivalent of &&
and ||
in languages like C# and Java.
And
and Or
are bitwise operators, they combine the bits of whatever is passed to them into a result, and since those bits can always be altered by further evaluation, need to evaluate everything. These are the equivalent of &
and |
in languages like C# and Java.
Most of the time, when you're looking at an and/or operation, you want conditional results. Bitwise operations will produce a value that is truthy or falsey and will (usually) match the true or false results from proper conditional operators, they will just take a bit longer on average since they evaluate everything.
Bitwise operators are meant to be used when you care about the specific bits in a value, often used for storing settings or permissions in as compact a way as possible, and for reading and applying them as fast as possible. For example the read/write/execute permissions on files in Unix based systems use bit flags this way:
0b001 - execute permission
0b010 - write permission
0b100 - read permission
so, in decimal, a value of 7 can do all three, a value of 4 can only read, 6 can read and write, etc. If someone is trying to write to a file it checks their permissions by using And 0b010
and gets a truthy or falsy result based on the bit in that position. To update the value to let users execute a file, you would use Or 0b001
on their current permissions and store the result.
The difference comes here:
0b001 (truthy) And 0b010 (truthy) = 0b000 (falsy)
0b001 (truthy) AndAlso 0b010 (truthy) = True
© 2022 - 2024 — McMap. All rights reserved.
And
andOr
– Poleaxe