Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does VB6 short-circuit complex conditions?

Tags:

vb6

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?

like image 216
Brian Hooper Avatar asked Oct 25 '10 13:10

Brian Hooper


People also ask

Does visual Basic short circuit?

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.

What is short-circuiting VB net?

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.

What is the difference between OR and OrElse in VB net?

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.


2 Answers

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).

like image 151
kennytm Avatar answered Oct 17 '22 02:10

kennytm


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 ...
like image 35
Bond Avatar answered Oct 17 '22 00:10

Bond