Does VB6 short circuit conditional tests? That is to say, can I be sure a statement like...
If index <= array_size And array(index) > something Then
will never burst the array, whatever the value of index might happen to be?
VB has operators AndAlso and OrElse, that perform short-circuiting logical conjunction. Why is this not the default behavior of And and Or expressions since short-circuiting is useful in every case. Strangely, this is contrary to most languages where && and || perform short-circuiting.
A logical operation is said to be short-circuiting if the compiled code can bypass the evaluation of one expression depending on the result of another expression.
OrElse is a short-circuiting operator, Or is not. By the definition of the boolean 'or' operator, if the first term is True then the whole is definitely true - so we don't need to evaluate the second term. Or doesn't know this, and will always attempt to evaluate both terms.
No, VB6's And
and Or
don't short-circuit (which is the reason why the short-circuit versions are called AndAlso
and OrElse
in VB.net — backward compatibility).
In addition to the If/Then/Else/End If
block, VB6 also supports a single-line If/Then/Else
construct. You can nest these to achieve simple short-circuiting. However, since it's a single-line statement, you must perform your desired action on the same line as well. For example:
' From (no short-circuit)
If index <= array_size And array(index) > something Then
' To (short-circuit)
If index <= array_size Then If array(index) > something Then ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With