Hierarchy of Logical Operators

 

Like arithmetic expressions we must understand how expressions that contain more than one kind of operator are evaluated. How shall we evaluate the following?

Given 

intYearsEmployed  = 6

decSales = 300500

decPreviousSales = 250000

 

If intYearsEmployed > 4 _

Or decSales > 300000 _

And decPreviousSales > 300000

     decCommission = decSales * .03

Else

     decCommission = decSales * .02

End If

 

 

Order of precedence of logical operators is: NOT, AND, OR. That is, an AND operation will be performed before an OR operation if both operations are included in a condition, unless parentheses are used to override priority of logical operators. Expressions in parentheses are processed first.  Thus the previous expression is equivalent to


 
If intYearsEmployed > 4 _

Or (decSales > 300000 And decPreviousSales > 300000) Then

decCommission = decSales * .03

Else

     decCommission = decSales * .02

End If

 

 

We can change the meaning with parenthesis

 

If (intYearsEmployed > 4 Or decSales > 300000) _

And (decPreviousSales > 300000) Then