tags:

views:

1020

answers:

6

In particular, for the logical operators

+4  A: 

Logical Operation Precedence Order

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

Comparison Operation Precedence Order

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

Arithmetic Operation Precedence Order

  1. ^
  2. -
  3. *, /
  4. \
  5. Mod
  6. +, -
  7. &

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

Found via a Google search for visual basic 6 logical operator precedence.

Jeremy Banks
+4  A: 

It depends on whether or not you're in the debugger. Really. Well, sort of.

Parentheses come first, of course. Then comparisons (>, <, =, etc). Then the logical operators. The trick is that the order of execution is not defined. That means given the following expression:

If A < B And B < C Then

you are guaranteed that the < operators will both be executed before the logical And comparison. But you are not guaranteed which 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 that they're different. So the actual precedence doesn't change, but the order of execution might.

Joel Coehoorn
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!
Joe Pineda
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."
MarkJ
A: 

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?

Oskar
Yes. Most modern languages support short-circuit evaluation (http://en.wikipedia.org/wiki/Short-circuit_evaluation) but VB6 (and VBNet without AndAlso) does not.
Kris Erickson
Joe Pineda
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 statements
Oskar
+1  A: 

@[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.

Joel Coehoorn
+1  A: 

Yes, VB6 always evaluates both sides of an Or or And operator.

For example, the following will always throw an error:

Dim a as Form
If a is Nothing Or Not a.Visible Then
    MsgBox "VB6 will never reach here and will instead throw an error."
End If
rpetrich
+1  A: 

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 VB6 manual topic Operator Precedence. Unfortunately this topic doesn't seem to be in the MSDN online VB6 manual, so I will paste the logical operator information here. If you installed the VB6 help (the MSDN library) you will have it on your machine.

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.

MarkJ
A good option if I didn't have to read someone else's code ...
Oskar
Ooh, I feel your pain. I've added some more to my answer. The operator precedence is in the VB6 help
MarkJ
+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...
Mike Spross