Microsoft KB Archive/45907
Article ID: 45907
Article Last Modified on 11/21/2006
This article was previously published under Q45907
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:
- Microsoft GW-Basic Versions 3.20, 3.22, and 3.23
- QuickBasic Versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50
- Microsoft Basic Compiler Versions 5.35, 5.36, 6.00, and 6.00b
- Microsoft Basic PDS Version 7.00.
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:
- Arithmetic operations
- Exponential (^)
- Negation (-)
- Multiplication and division (*, /)
- Integer division (\)
- Modula arithmetic (MOD)
- Addition and subtraction (+, -)
- Relational operations (=, >, <, <>, <=, >=)
- Logical operations
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