What is the operator precedence order in Visual Basic 6.0 (VB6)?
In particular, for the logical operators.
What is the operator precedence order in Visual Basic 6.0 (VB6)?
In particular, for the logical operators.
^
-
(unary negation)*
, /
\
Mod
+
, -
(binary addition/subtraction)&
=
<>
<
>
<=
>=
Like
, Is
Not
And
Or
Xor
Eqv
Imp
Source: Sams Teach Yourself Visual Basic 6 in 24 Hours — Appendix A: Operator Precedence
Comparison Operation Precedence Order
in VB6 (and in VB.Net) and all comparison operators are the same precedence and are evaluated left-to-right. For instance 2 < 0 = 0
is not evaluated as 2 < (0 = 0)
according to the table (equality having higher precedence) but just left-to-right like (2 < 0) = 0
the same way 0 = 2 < 0
is not 0 = (2 < 0)
but left-to-right (0 = 2) < 0
–
Chalcedony It depends on whether or not you're in the debugger. Really. Well, sort of.
Parentheses come first, of course. Then arithmateic (+,-,*,/, etc). Then comparisons (>, <, =, etc). Then the logical operators. The trick is the order of execution within a given precedence level is not defined. Given the following expression:
If A < B And B < C Then
you are guaranteed the <
inequality operators will both be evaluated before the logical And
comparison. But you are not guaranteed which inequality comparison will be executed first.
IIRC, the debugger executes left to right, but the compiled application executes right to left. I could have them backwards (it's been a long time), but the important thing is they're different. The actual precedence doesn't change, but the order of execution might.
EDIT: That's my advice for new code! But Oscar is reading someone else's code, so must figure it out somehow. I suggest the VBA manual topic Operator Precedence. VBA is 99% equivalent to VB6 - and expression evaluation is 100% equivalent. I have pasted the logical operator information here.
Logical operators are evaluated in the following order of precedence:
Not And Or Xor Eqv Imp
The topic also explains precedence for comparison and arithmetic operators.
I would suggest once you have figured out the precendence, you put in parentheses unless there is some good reason not to edit the code.
© 2022 - 2024 — McMap. All rights reserved.