Performing Arithmetic |
11.1
The basic arithmetic operators in PerfectScript are the
following:
|
| + | addition |
| - | subtraction |
| * | multiplication |
| / | division |
| % | modulus
(gives the remainder when performing division) |
Arithmetic can only be performed on numbers, not strings.
(See the topic strings v.
numbers earlier in the tutorial.)
The items that are used by the operators to obtain the result are
called operands, and the combination of operands and
operator(s) is called an expression. What happens when
the expression is performed is called the result. For
example, in
2 + 4 = 6, 2 and 4 are the operands,
+ is the operator, 2+4 is the expression, and 6 is the result.
Don't worry about remembering these terms; they are given to help
with the explanations of other topics in the tutorial.
Assigning arithmetic results
| vNum := ?Page + 5 | Adds 5 to the value contained in the system variable ?Page, and assigns the result to vNum | ||
| x := y - 5 | Subtracts 5 from the value of y, and assigns the result to x | ||
| vTotal := vEach * 10 | Multiplies the value of vEach by 10 and assigns the result to vTotal | ||
| vHalf := vWhole / 2 | Divides the values of vWhole by 2 and assigns the result to vHalf | ||
| vOdd := vAmt % 2 | Divides the value of vAmount by 2, and assigns the remainder to vOdd |
Order of precedenceThe important rule for order of precedence of operations in simple arithmetic is: multiplication and division are performed before addition and subtraction. Operations with the same order of precedence, i.e., multiplication and division, and addition and subtraction, are performed in order, left to right.
By putting expressions within parentheses, you can change the the order in which expressions are evaluated, because operations in parentheses are always performed first. For example, (10+5)*3 gives a result of 45, and 5*(3+10) gives a result of 65. If you are unsure about the order of precedence of operations, it is safest to put all operations within parentheses: (10+1)*(8-3). The operations within the parentheses are performed separately first, and then the results of those operations are used for the operation outside the parentheses, to give the final result. If this is part of an assignment to a variable, the final result is then assigned to the variable on the left side of the assignment.
© 1999 Seth H. Katz
All rights reserved
E-MAIL TO AUTHOR
![]() |
![]() |
![]() |