Microsoft KB Archive/45907

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


QuickBasic's Hierarchy of Operations

Article ID: 45907

Article Last Modified on 11/21/2006

This article was previously published under Q45907

SUMMARY

The following information on "Hierarchy of Operations" was taken from Pages 45-46 of the "Microsoft QuickBasic 4.0: Basic Language Reference" manual for Versions 4.00 and 4.00b. This information also applies to the following products:

  1. Microsoft GW-Basic Versions 3.20, 3.22, and 3.23
  2. QuickBasic Versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50
  3. Microsoft Basic Compiler Versions 5.35, 5.36, 6.00, and 6.00b
  4. Microsoft Basic PDS Version 7.00.


MORE INFORMATION

The Basic operators have an order of precedence, that is, when several operations take place within the same program statement (without being grouped by parentheses), some operations are executed before others. Operations are executed in the following order:

  1. Arithmetic operations

    1. Exponential (^)
    2. Negation (-)
    3. Multiplication and division (*, /)
    4. Integer division (\)
    5. Modula arithmetic (MOD)
    6. Addition and subtraction (+, -)
  2. Relational operations (=, >, <, <>, <=, >=)
  3. Logical operations

    1. NOT
    2. AND
    3. OR
    4. XOR
    5. EQV
    6. IMP

An exception to the order of operations listed above occurs when an expression has adjacent exponentiation and negation operators. In this case, the negation is done first. For example, the following statement prints the value .0625 (equivalent to 4^-2), not -16 (equivalent to -(4^2)):

PRINT 4 ^ -2


If operators on the same precedence level appear in the same expression, they are computed from left to right.

For more information on the "Hierarchy of Operations," consult the Basic language reference manual for your version of Basic.


Additional query words: QuickBas BasicCom

Keywords: KB45907