Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 5: Decision
Last change: February 13 2016 18:47:58.
<<PreviousNext>>

Operational Precedence

Chapter 4 listed the operational precedence of various numeric operators used in an expression. All these rules still hold for all conditional expressions used in an If statement, but now there are two more types of operators: relational and logical operators. How are these new operators ranked? These new operators rank lower in their operational precedence than any of the numeric operators. In addition, the relational operators have precedence over the logical operators, among which Not has the highest precedence followed by And. The following is the list of operators again, ranking from the highest to the lowest in operational precedence:

  1. Power (^)
  2. Unary negation (-)
  3. Multiplication and division (*, /)
  4. Integer division (\)
  5. Remainder (Mod)
  6. Addition and subtraction (+, -)
  7. Equal  (=)
  8. Not equal (<>)
  9. Less than, Greater than (<, >)
  10. Greater than or equal to (>=)
  11. Less than or equal to (<=)
  12. Not
  13. And, AndAlso
  14. Or, OrElse and Xor

Too many rules to remember? If you think so, use parentheses to explicitly express the order of operations. Parentheses help add clarity to what needs to be done first.

Try This
Want to verify that the And operator actually takes precedence over the Or operator? Consider the following code:
MsgBox(True Or True And False)
If Or and And rank equal in precedence, the result should be False; however, if And has the precedence, the result should be True.
Enter the code in the form’s click event, run the program, and click the form. What do you see?
Now you have seen how relational and logical operators work. These operators are used to construct logical expressions, which most likely are used in If statements. It is time to take a closer look at the If statement (block).
Last change: February 13 2016 18:47:58.
<<PreviousNext>>