18.10.2014 Views

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SIMSCRIPT II.5 Programming Language

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>SIMSCRIPT</strong> <strong>II.5</strong> <strong>Programming</strong> <strong>Language</strong><br />

1.5 Arithmetic Expressions<br />

Arithmetic expressions are formed by combining variables and constants with arithmetic operators.<br />

The arithmetic operators are + (add), - (subtract), * (multiply), / (divide), and ** (exponentiate).<br />

Two of these operators, plus and minus, can be used as unary operators, that is, with a single variable<br />

or constant. The constants +1 and -1 are examples of the use of plus and minus as unary operators<br />

on the constant 1. All of the operators can be used as binary operators, that is, with two<br />

variables (or constants or a variable and constant). If we let A and B represent either a variable or a<br />

constant, then + A and - A are examples of plus and minus as unary operators, and A + B, A **<br />

B are examples of arithmetic expressions that use binary operators.<br />

The simplest expression consists of a single constant, or a single variable, perhaps preceded by a<br />

unary plus or minus operator. An expression, + A, may be written as A, with the unary plus implied.<br />

This is not possible, of course, with the unary minus operator.<br />

All binary operators must be explicitly expressed, and no two operators can appear consecutively.<br />

For example, multiplication of the variables A and B must be written as A * B, and not AB. The<br />

latter would be interpreted as the value of a variable called AB. Addition of the expressions A and<br />

-B can be written as A + (-B) or A - B, but not A + -B. This last example shows that parentheses<br />

must be used to separate unary and binary operators. Parentheses may also be used (1) to clarify<br />

the operations in an expression to make it more readable, or (2) to specify the order in which the<br />

operations in an expression are to be performed.<br />

Simple expressions can be connected by any of the arithmetic operators (+, -, *, /, **) to<br />

form compound expressions. The "parentheses rule" states that expressions are evaluated from left<br />

to right, removing parentheses before applying operator hierarchy rules. Imbedded parentheses are<br />

evaluated from the inside out. Thus:<br />

a + (b*c) + d<br />

is evaluated by first computing the value of (b*c) and then adding this value to a and d.<br />

When parentheses are omitted, the hierarchy of operations is:<br />

1. Exponentiation **<br />

2. Multiplication and division * and /<br />

3. Addition and subtraction + and -<br />

This hierarchy specifies the order in which the different operations are performed relative to one<br />

another. Exponentiation is performed before multiplication or division, and either of these before<br />

addition or subtraction. For example, the expression A + B/C + D**E * F - G is taken to mean<br />

A + (B/C) + (D E * F) - G. If precedence is not completely specified by these rules, the<br />

operation specified farthest to the left in the expression is performed first, as in A * B/C, which is<br />

computed as (A * B) / C.<br />

4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!