OS/2 Procedures Language 2/REXX


Inf-HTML [About][Toc][Index] 0.9b (c) 1995 Peter Childs


Working with Arithmetic


Your REXX procedures may need to include arithmetic operations of 
addition, subtraction, multiplication, and division.  For example, you may 
want to assign a numeric value to two variables and then add the 
variables. 
Arithmetic operations are performed the usual way.  You can use whole 
numbers and decimal fractions.  A whole number is an integer, or any 
number that is a natural number, either positive, negative, or zero, that 
does not contain a decimal part (for example, 1, 25, or 50).  A decimal 
fraction contains a decimal point (for example, 1.45 or 0.6). 
Before you see how these four operations are handled in a procedure, here 
is an explanation of what the operations look like and the symbols used. 
 These are just a few of the arithmetic operations used in REXX. 
Note:    The examples contain a blank space between numbers and operators 
         so that you can see the equations better, but the blank is 
         optional. 
 
 Operators - The symbols used for arithmetic (+ , -, *, /) are called 
 operators because they operate on the adjacent terms.  In the following 
 example, the operators act on the numbers (terms) 4 and 2: 

 SAY 4 + 2             /* says "6" */
 SAY 4 * 2             /* says "8" */
 SAY 4 / 2             /* says "2" */
 
 
 Addition - The operator for addition is the plus sign (+).  An 
 instruction to add two numbers is: 

 SAY 4 + 2
 
 
 The answer you see on your screen is 6. 
 Subtraction - The operator for subtraction is the minus sign (-).  An 
 instruction to subtract two numbers is: 

 SAY 8 - 3
 
 
 The answer on your screen is 5. 
 Multiplication - The operator for multiplication is the asterisk (*).  An 
 instruction to multiply two numbers is: 

 SAY 2 * 2
 
 
 The answer on your screen is 4. 
 Division - For division, there are several operators you can use, 
 depending on whether or not you want the answer expressed as a whole 
 number.  For example, for a simple division, the symbol is one slash (/). 
 An instruction to divide is: 

 SAY 7 / 2
 
 
 The answer on your screen is 3.5. 
 To divide and return just a remainder, the operator is two slashes (//). 
  To divide, and return only the whole number portion of an answer and no 
 remainder, the operator is the percent sign (%). 
 For examples showing you how to perform four arithmetic operations on 
 variables, select the Examples pushbutton. Arithmetic Examples
 Evaluating Expressions - Expressions are normally evaluated from left to 
 right.  An equation helps to illustrate this point.  Until now, you have 
 seen equations with only one operator and two terms, such as 4 + 2. 
  Suppose you had this equation: 

 9 - 5 + 4 =
 
 
 The 9 - 5 would be computed first.  The answer, 4, would be added to 4 
 for a final value: 8. 
 Some operations are given priority over others.  In general, the rules of 
 algebra apply to equations.  In this equation, the division is handled 
 before the addition: 

 10 + 8 / 2 =
 
 
 The value is 14. 
 If you use parentheses in an equation, the interpreter evaluates what is 
 in the parentheses first.  For example: 

 (10 + 8) / 2 =
 
 
 The value is 9.   

Inf-HTML End Run - Successful