1

In VBA, when you use the OR statement, will the processor check through all criteria regardless of whether it has already found a FALSE statement?

i.e. for:

If CriteriaA OR CriteriaB OR CriteriaC OR CriteriaD Then
    Exit Function
End If

If CriteriaA is false, will the other criteria be checked unnecessarily? And is CriteriaA always checked first?

I'm wondering since each of these checks takes a significant amount of time and I only need to know whether one is false. In this case, I have put the most commonly failed criteria check first in the list, so that it will cause the function to fail before having to do any more work. Is this a correct assumption?

Community
  • 1
  • 1
teepee
  • 2,620
  • 2
  • 22
  • 47
  • 1
    Yes, **VB** (any version) is not like C/C++ in this aspect. – A.S.H Nov 23 '15 at 22:00
  • Another duplicate [here](http://stackoverflow.com/questions/2739451/prevent-excel-from-evaluating-unneeded-expressions-in-or) and [here](http://stackoverflow.com/questions/21532476/how-does-vba-treat-or-statements). – findwindow Nov 23 '15 at 22:04
  • @A.S.H thanks for that. What is the advantage of not having short-circuited evaluations? I.e. why is VB designed like this? – teepee Nov 23 '15 at 22:07
  • I guess it is a historical reason. My two cents is, VB, read `Basic`, was designed `basically` for novice programmers, and if you ask any of them, this is the way they read it. I have even met professional C++ programmers who did not know about the *short-circuiting* paradigm, so they fell into the mistake of thinking that all their sub-expressions will be evaluated. Think of it when these sub-expressions have side effects! – A.S.H Nov 23 '15 at 22:12
  • what are the practical applications of this? – Alex Gordon Nov 23 '15 at 22:15
  • 1
    @IIIIIllllllllIlllllIIIIIIIIlll As I said, think of it when one or more of the terms has side effects, i.e. it is a subroutine that evaluates to Boolean BUT in the mean time modifies something. For a beginner programmer, when he sees such a statement, he will think that **all** the routines in the statement are actually called, whatever the outcome of the statement! This is true for the **Basic** family, but not for the C family. – A.S.H Nov 23 '15 at 22:19
  • @A.S.H No that is not why. Commercial, professional-quality BASIC compilers have been available since the mid-seventies, and VB has always been more related to them than to the toy BASIC interpreters like GW-Basic. The real traditional reason is that BASIC used integers for multiple purposes, both as a proxy for the boolean data-type in logical expressions, and as a binary bit-mask data-type. Since it only had one `OR` operator (unlike C's `||` and `|`) it could not which was intended in a conditional expression. It fixed this by using bit-masks then reducing it to boolean only at the end. – RBarryYoung Nov 23 '15 at 22:26
  • @RBarryYoung it makes sense as well. The OR operation is the biwise operation so its final outcome cannot be known until the very end. However, it is difficult to tell what were precisely the reasons (with s) for this design choice (why not providing another boolean Or like C did?). There are many of them I guess. – A.S.H Nov 23 '15 at 22:39
  • 1
    @A.S.H Most languages at the time (that's actually when I started programming) did not have a lot of operators. C was very unusual in that regard and easily had the most operators of any widely used language (except maybe APL, which was pretty specialized). Most languages (COBOL, BASIC, Fortran, even Pascal and Algol) only had about a dozen, and thus didn't have extras for bitwise vs logical. I can promise you, the discussion at the time was *not* why do most languages have so few operators, but rather "*why does C have so many?*" – RBarryYoung Nov 23 '15 at 22:48
  • 1
    @RBarryYoung thanks for this historical enlightenment. :) – A.S.H Nov 23 '15 at 22:58

2 Answers2

2

Yes, VBA is not a short-circuiting language, so it always checks all conditions of a conditional expression.


The answer to the question "Why wouldn't a language use short-circuiting" is here.

Community
  • 1
  • 1
RBarryYoung
  • 55,398
  • 14
  • 96
  • 137
  • interesting, so it tends to be slower than the "short-circuiting ones"? So I would assume that this: `IF A OR B OR C OR D Then Exit Function` is slower than `IF A Then Exit Function: Elseif B Then Exit Function: Elseif C Then Exit Function`...? – teepee Nov 23 '15 at 22:02
  • @teepee Well, VBA is a generally non-Optimized language, so generally, it is going to be slower than an optimized one. As for the example in your question, short-circuiting may be faster if A is true, but if only D is true, then not short-circuiting may well be faster (because it doesn't have to branch-test after each condition), but even that depends on implementations. – RBarryYoung Nov 23 '15 at 22:12
  • @teepee The real reason that VB (VB6, VBA, VB.net) doesn't do short-circuiting here is 1) because it doesn't do true boolean `OR` operations (`||` in c languages), but rather does integer bit-mask test and operations (`|` in c languages) and 2) so that function calls in expressions are always made the same number of times (for "consistency"). – RBarryYoung Nov 23 '15 at 22:12
  • OK thank you that's all good information. Thanks for your help. – teepee Nov 23 '15 at 22:16
2

VBA will not short-circuit an OR operator (neither will VB, that requires OrElse).

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • Why would they design it that way? Seems inefficient, but there must be pros as well... – teepee Nov 23 '15 at 22:09
  • Legacy cruft mostly. Because some old code would do something like: `if (something) or (Increment(variable)>1) then` and if it short circuited, then variable wouldn't be incremented. – Robert McKee Nov 23 '15 at 22:11
  • Eww. Not exactly a good look for them. OK makes sense. Thanks for your help. – teepee Nov 23 '15 at 22:12