locked
Does VBA support short-circuit operators? RRS feed

  • Question

  • I don't think it does because I can't seem to find anything on them but I figured it was worth asking. In case my question isn't clear..

    There are some operators in languages like C# such as | which means Or. || also means Or, but it is a short-circuit Or, which means it does not evaluate the second operand if the first operand is evaluates to true. Same with & and && in C#.

    If it does, does anyone have a link to the documentation?

    Thanks for the help.
    Sunday, October 23, 2016 10:08 PM

Answers

  • No, it doesn't.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by HTHP Wednesday, November 2, 2016 12:30 PM
    Monday, October 24, 2016 12:32 AM
  • Hi HTHP,

    VBA does not support short-circuiting - apparently because it only has bitwise And/Or/Not etc operations.  From the VBA language specification: "Logical operators are simple data operators that perform bitwise computations on their operands."  In this light, it makes sense that VBA was designed with true = &H1111 and false = &H0000: this way logical statements can be evaluated as bitwise operations.

    so as a work around you can try to use nested if conditions.

    so if parent if condition get false then it will not go inside to check child if condition .

    you can see the example mentioned below.

    If cond1 And cond2 Then
        '...
    End If
    
    
    Change the approach with code mentioned below.
    
    If cond1 Then
        If cond2 Then
            '...
        End If
    End If

    also you can try to set flag variables and keep all the conditions separate.

    and you can check the flags to execute the condition.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by HTHP Wednesday, November 2, 2016 12:34 PM
    Monday, October 24, 2016 1:55 AM
  • Yes - only NOT and XOR are bitwise - the others are not.

    But didn't I just demonstrate that AND and OR are bitwise operations?

    And is not a bitwise operation the same as short circuiting?

    I assumed that a short-circuit is simply a true/false (bitwise) operation on a expression.

    if my assumed definition is wrong, then no problem.

    I don't think that is what is meant by "short circuit" in this context.  I believe we are talking about whether, in the case of an If statement like this:

        If (condition1) Or (condition2) Then

    ... both condition1 and condition2 are evaluated.  If both conditions are always evaluated, regardless of the truth value of condition1, then the Or operator is not short-circuited.  If, however, condition2 is not evaluated if condition1 is True, then the operator is short-circuited.

    We can demonstrate that the Or operator in VBA is not short-circuited quite simply, by observing this behavior in the Immediate window:

    I guess the question then is what is the "semantic" difference or "context" difference between a bitwise operation and a short-circuit? I was assuming that any short-circuit is simply a "logical" test based on bitwise operations. (and that seemed to be the context of the question)

    No, "short-circuit" logical operators are defined in C#, as the OP referred to in the thread-starting post.

    The iif(true/false,true part, false part) does seem to be what short-circuit means. So if the question is not about bitwise (and I rather much think Dirk is correct), then the iif() would be short-circuit like.

    The IIf() function in VBA is not short-circuit-like -- it always evaluates all arguments.  But the IIf() function in Jet SQL is short-circuited -- if the condition is True, the "false part" argument is not evaluated.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, October 25, 2016 7:53 PM

All replies

  • No, it doesn't.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    • Marked as answer by HTHP Wednesday, November 2, 2016 12:30 PM
    Monday, October 24, 2016 12:32 AM
  • Hi HTHP,

    VBA does not support short-circuiting - apparently because it only has bitwise And/Or/Not etc operations.  From the VBA language specification: "Logical operators are simple data operators that perform bitwise computations on their operands."  In this light, it makes sense that VBA was designed with true = &H1111 and false = &H0000: this way logical statements can be evaluated as bitwise operations.

    so as a work around you can try to use nested if conditions.

    so if parent if condition get false then it will not go inside to check child if condition .

    you can see the example mentioned below.

    If cond1 And cond2 Then
        '...
    End If
    
    
    Change the approach with code mentioned below.
    
    If cond1 Then
        If cond2 Then
            '...
        End If
    End If

    also you can try to set flag variables and keep all the conditions separate.

    and you can check the flags to execute the condition.

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by HTHP Wednesday, November 2, 2016 12:34 PM
    Monday, October 24, 2016 1:55 AM
  • The “and” and “or” , > , <, # are not bit wise.

    However, the xor operator is.

    So:

    00000001   (1)

    00000010   (2)

    ========

    00000011    (3)

    So if you xor the above (1 xor 2), you would get the value of 3.

    And thus:

    someVar = 1 xor 2

    You find the result is 3.

    So there is a bitwise operator – just not for “general” comparsions such as “and, or, >, <, #”

    Use of xor will allow you to do most bit operations if you use not() function – which ALSO is bitwise.

    Eg this code and output shows this in action:

      Dim i     As Byte

      Dim j     As Byte

     i = 1

      j = 2

      Debug.Print "EXAMPLE 1", i Xor j

      Debug.Print "EXAMPLE 2", Not (i Xor j)

      i = 0                   '00000000

      Debug.Print "EXAMPLE 3", Not (i)    '11111111

      Debug.Print "EXAMPLE 4", Not (&HFF)    ' 11111111 (255)

    Output in debug window:

    EXAMPLE 1      3

    EXAMPLE 2      252

    EXAMPLE 3      255

    EXAMPLE 4     -256

    So "and" can't be used but NOT and XOR can be and are bitwise. The result of the above can thus get you "most" required bitwise operations by use of XOR + NOT.

    Edit: So to answer  your question? Yes, VBA does have some bitwise operators - just not "and" and "or".

    Regards,

    Albert D. Kallal (Access MVP)

    Edmonton, Alberta Canada


    Monday, October 24, 2016 5:51 AM
  • The “and” and “or” , > , <, # are not bit wise.
    [...]
    Yes, VBA does have some bitwise operators - just not "and" and "or".

    Albert, I think you're mistaken about And and Or.  In the Immediate window, I get this:

        ?&h01 and &h02
         0
        ?&h01 or &h02
         3
        ?&h01 xor &h02
         3
        ?&h01 and &h03
         1
        ?&h01 or &h03
         3
        ?&h01 xor &h03
        2

    That looks like bitwise operations to me.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, October 25, 2016 4:38 PM
  • Yes - only NOT and XOR are bitwise - the others are not.

    And is not a bitwise operation the same as short circuiting?

    I assumed that a short-circuit is simply a true/false (bitwise) operation on a expression.

    if my assumed definition is wrong, then no problem.

    I guess the question then is what is the "semantic" difference or "context" difference between a bitwise operation and a short-circuit? I was assuming that any short-circuit is simply a "logical" test based on bitwise operations. (and that seemed to be the context of the question)

    So anyone can jump in and outline the fundamental difference between a bitwise operation and a short-circuit - I am open to me having miss-understood the term short-circuit. A quick google suggests I HAVE miss-understood the question. The iif(true/false,true part, false part) does seem to be what short-circuit means. So if the question is not about bitwise (and I rather much think Dirk is correct), then the iif() would be short-circuit like.

    So I "read" this question as a bitwise question - it looks like my assumption(s) are wrong.

    Regards,

    Albert D. Kallal (Access MVP)
    Edmonton, Alberta Canada


    Tuesday, October 25, 2016 7:21 PM
  • Yes - only NOT and XOR are bitwise - the others are not.

    But didn't I just demonstrate that AND and OR are bitwise operations?

    And is not a bitwise operation the same as short circuiting?

    I assumed that a short-circuit is simply a true/false (bitwise) operation on a expression.

    if my assumed definition is wrong, then no problem.

    I don't think that is what is meant by "short circuit" in this context.  I believe we are talking about whether, in the case of an If statement like this:

        If (condition1) Or (condition2) Then

    ... both condition1 and condition2 are evaluated.  If both conditions are always evaluated, regardless of the truth value of condition1, then the Or operator is not short-circuited.  If, however, condition2 is not evaluated if condition1 is True, then the operator is short-circuited.

    We can demonstrate that the Or operator in VBA is not short-circuited quite simply, by observing this behavior in the Immediate window:

    I guess the question then is what is the "semantic" difference or "context" difference between a bitwise operation and a short-circuit? I was assuming that any short-circuit is simply a "logical" test based on bitwise operations. (and that seemed to be the context of the question)

    No, "short-circuit" logical operators are defined in C#, as the OP referred to in the thread-starting post.

    The iif(true/false,true part, false part) does seem to be what short-circuit means. So if the question is not about bitwise (and I rather much think Dirk is correct), then the iif() would be short-circuit like.

    The IIf() function in VBA is not short-circuit-like -- it always evaluates all arguments.  But the IIf() function in Jet SQL is short-circuited -- if the condition is True, the "false part" argument is not evaluated.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, October 25, 2016 7:53 PM
  • As always – spot on Dirk – thanks a bunch.

    Yes, I did miss read the question. (and as for bitwise – you are 100% correct – by bad).

    To be fair, my comment about true/false and bitwise in reference to Boolean expressions in Access not being bitwise is due to true/false NOT being a number in Access unless you cast:Eg:

    ? true and false
    False

    Note how a Boolean is returned. Access has its own true/false type. And to be fair, anytime you cast out of Boolean, then “and,or” etc. are bitwise – but since true/false in Access is not a number type in Access then such Boolean expressions are not bitwise. The true/false and general Boolean expressions would have to be casted to a number data type to be bitwise.  So I guess better said is that the Boolean datatype in Access is not bitwise unless casted. SQL server for example does not have a Boolean type - it is a number type (bit).

    Regards,
    Albert D. Kallal (Access MVP)
    Edmonton, Alberta Canada

    Tuesday, October 25, 2016 9:06 PM
  • Quibbling a bit more <g> ...

    To be fair, my comment about true/false and bitwise in reference to Boolean expressions in Access not being bitwise is due to true/false NOT being a number in Access unless you cast:Eg:

    ? true and false
    False

    Note how a Boolean is returned.

    I think the *operators* are bitwise, but the data type of the result depends on the operands.  In an If statement ("If <condition> Then", the <condition> expression is converted to Boolean, and any numeric expression converts to False if it equals 0, and True if it is non-zero.

    The comparison operators {=,<>,<,>,<=,>=} always return a Boolean result, so any If statement of the form "If something comparison-operator otherthing Then" will always be evaluating a Boolean. But in the case of "If <some non-boolean> Then", the <some non-boolean> will be converted to Boolean, according to the rule I stated above.

    So in the case of an If statement like "If A Or B Then", where A and be are simple variables, the Or operator proceeds bitwise to get a result, and then that result is converted to Boolean for the If statement to work on.  But in the case of an assignment statement like "C = A Or B", C is assigned the result of the bitwise OR of A and B.

    Access has its own true/false type. And to be fair, anytime you cast out of Boolean, then “and,or” etc. are bitwise – but since true/false in Access is not a number type in Access then such Boolean expressions are not bitwise.

    I'm not sure I understand what you mean, but in VBA the keyword True is defined to be equal to -1, and False is defined to be equal to 0.  It says this in the language reference manual.  And consider this code:

        Dim A, B, C, D
       
        A = 1
        B = 2
        C = A Or B
        D = CBool(C)
       
        Debug.Print "C = "; C; ", of type "; TypeName(C)
        Debug.Print "D = "; D; ", of type "; TypeName(D)
       
        If C Then
            Debug.Print "C is True in If statement"
        End If
       
        If C = True Then
            Debug.Print "C is True in comparison"
        Else
            Debug.Print "C is False in comparison"
        End If
       
        If D = True Then
            Debug.Print "D is True in comparison"
        Else
            Debug.Print "D is False in comparison"
        End If

    When run, it produces this in the Immediate window:

        C =  3 , of type Integer
        D = True, of type Boolean
        C is True in If statement
        C is False in comparison
        D is True in comparison

    And, in the Immediate window, you can have even more fun:

        ?True and 3
         3
        ?TypeName(True and 3)
        Integer
        ?True or 3
        -1
        ?TypeName(True or 3)
        Integer

    That demonstrates the fact that the result of the Or operator is neither True nor False until it is converted to a Boolean.


    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Tuesday, October 25, 2016 9:54 PM