What is the operator precedence order in Visual Basic 6.0?

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.


Arithmetic Operation Precedence Order

  1. ^
  2. - (unary negation)
  3. *, /
  4. \
  5. Mod
  6. +, - (binary addition/subtraction)
  7. &

Comparison Operation Precedence Order

  1. =
  2. <>
  3. <
  4. >
  5. <=
  6. >=
  7. Like, Is

Logical Operation Precedence Order

  1. Not
  2. And
  3. Or
  4. Xor
  5. Eqv
  6. Imp

Source: Sams Teach Yourself Visual Basic 6 in 24 Hours — Appendix A: Operator Precedence

Tags:

Vb6