Mathematical order of Operator Precedence

For example, the expression 3+6 / 3 evaluates to 5, not 3.  Division has a higher priority than addition, so the division operation (6 / 3) is performed first.  Even though the compiler will not get confused, people still could, so a better programming style might be to use 3 + (6 / 3) or 3 + 6/3, either using parentheses or spacing to make the intent clear.  Otherwise it is easy to misread the statement as (3 + 6) / 3.

To handle operations of the same priority, PowerBASIC proceeds from left to right.  For example, in the expression 4 - 3 + 6, the subtraction (4 - 3) is performed before the addition (3 + 6), producing the intermediate expression 1 + 6.

Operations inside parentheses are of the highest priority and are always evaluated first.  Within parentheses, standard precedence is used.  Use parentheses like garlic: generously, but not to excess.

Another example of the effect of Order of Precedence on an expression follows:

x = -1^2

At first glance, the result of 1 may be the expected result (since -1 * -1 = 1); however, the unary negation operator has a lower precedence than exponentiation, so the expression is evaluated as x& = -(1^2) which gives a result value of -1.  As noted above, the use of parentheses can clarify the intended expression:

x = (-1)^2


See Also

Arithmetic Operators

Relational Operators