What is the operator precedence order in Visual Basic 6.0?
Asked Answered
M

3

17

What is the operator precedence order in Visual Basic 6.0 (VB6)?

In particular, for the logical operators.

Merat answered 10/9, 2008 at 20:14 Comment(0)
D
18

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

Dobby answered 10/9, 2008 at 20:16 Comment(4)
What is the order? From lowest to highest?Lakesha
The presentation of the answer can be slightly misleading. The order inside the three categories is first-to-last, but the ordering of the categories themselves is last-to-first: In actuality, arithmetic operations are evaluated before comparison operations, which are evaluated before logical operations. For a clearer presentation, I would suggest to present the answer by putting arithmetic operations first, followed by comparison operations, followed by logical operations. Good reference also here: msdn.microsoft.com/en-us/vba/language-reference-vba/articles/…Danuloff
@MarkkuKero Just fixed the ordering of the sections, and also changed the formatting to use semantic headings.Gyral
There is no 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) < 0Chalcedony
G
10

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.

Gyral answered 10/9, 2008 at 20:36 Comment(7)
Joel, does this mean that an OR operation will not (ever) be able to skip the right hand side if the left evaluates to true, and the same for AND and false?Merat
@[Oskar]: Yes. VB.Net defines the execution order and has the AndAlso and OrElse logical operators to do short circuit evaluation. But VB6 will always evaluate both sides of an expression. You have to simulate it with additional If/Else statements.Gyral
Yes. Most modern languages support short-circuit evaluation (en.wikipedia.org/wiki/Short-circuit_evaluation) but VB6 (and VBNet without AndAlso) does not.Gagarin
In the K&R book it's advised that your code should not relay on logic ops being short-circuited always - the standard doesn't enforce this, nor order of evaluation. Even in Perl, where they're always short-circuited, it's considered bad taste for your code to depends on this.Hazaki
Very few languages specify the exact order of evaluation in such a statement - usually compilers are given liberty to do it the way it's considered more efficient/fast/safe/etc. One should not write code dependent on the order of execution in any language!Hazaki
really a side track, but in Perl it is very common to rely on precedence in constructs like <dosomething> or die "Couldn't do it"; but agree, life is better when the person before you didn't rely on it in complex statementsMerat
If you wrote code that depended on the order of execution in an expression like that, you deserve everything you get! "Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live."Iveson
I
3

Use parentheses


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.

Iveson answered 5/3, 2009 at 15:58 Comment(2)
Ooh, I feel your pain. I've added some more to my answer. The operator precedence is in the VB6 helpIveson
+1 for the advice, since it is still good advice. On a side note, I don't understand why people try to cram so many operators into a single line of code in the first place. Sometimes I wish languages defined no precedence, so that people had to use parentheses to make it clear what the heck they were trying to accomplish. I guess there's always Lisp...Velum

© 2022 - 2024 — McMap. All rights reserved.