You are on page 1of 26

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

MODULE 03 Page 0 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
MODULE 03
Assignments and
Expressions
MODULE 03 Page 1 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
ASSIGNMENT STATEMENT
Format: variable = value;
Used to place a value into a variable.
The 'value' specifed can be anything in PL/1 that gives us a value. We
have already seen constants and variables, and both of these could be
used as values. We'll soon see expressions, which are another way of
providing a value. This could be a value that we have calculated, for
example the sum of the values in two other variables, or it could be
simply the value of a single other variable or constant. In the latter case
we are simply making another copy of the value.
Examples:
DCL A CHAR(4);
DCL B CHAR(4);
A = 'FOOD';
B = A;
Before the assignment, we have declared A and B as being variables,
but have not specifed any value for them. Any reference to A or B
would therefore be in error, as we mustn't refer to a variable without a
value.
After the frst assignment, A has a value. Its value is the four-character
string'FOOD'.
After the second assignment, B too has a value. It is assigned the value
that A presently has. Whatever the value of A at the moment that the
assignment statement took place would be the value assigned to B.
In the two assignment examples above, all the data items were 4
characters in length. So we assigned a CHAR(4) constant to a variable
of exactly the right data type and size to receive that value. More often,
the variable on the left of the assignment doesn't have exactly the same
attributes as the value we're trying to assign to it. Therefore we need the
rules for the way in which an assignment takes place. These rules are
MODULE 03
Page 2 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
diferent for diferent for diferent data types, and so they need to be
looked at individually.
MODULE 03 Page 3 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
CHARACTER ASSIGNMENT
If the variable on the left of the assignment statement is a CHAR
variable, then we have a CHAR assignment statement. Whatever we
code on the right of the assignment has to be converted, or adjusted in
some way, to ft the data attributes of the variable on the left.
Example:
DCL A CHAR(4);
DCL B CHAR(10);
A = 'ABCD';
B = A;
A = 'ABCDEFG';
A = B;
Here we have four assignment statements. We need to consider them
individually, because they each demonstrate diferent points.
A = 'ABCD';
Here the attributes of the value on the RHS are CHAR(4). The attributes
of the value on the LHS are CHAR(4). These are the same, and so
nothing needs to be done. The value on the RHS will ft exactly into the
variable on the LHS. After this assignment, the value of A is 'ABCD'.
B = A;
Here the attributes of the value on the RHS are CHAR(4). The variable
on the LHS has attributes CHAR(I0). PL/1 needs to take the CHAR(4)
value and convert it into a CHAR(I0) value. It does this by "padding" the
value on the right with enough spaces to make it long enough. The
value that fnally gets assigned to B is 'ABCD^^^^^^'(where we have
used '^' to indicate a single space character.
A = 'ABCDEFG';
In this case the length of the RHS is 7, but the length of the variable on
the LHS is 4. The RHS value therefore needs to be "truncated" or
MODULE 03
Page 4 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
chopped down to ft into A. When assigning strings, PL/1 always
truncates on the right, taking only enough characters to fll the target
variable. In this case, the value of A after the assignment would be
'ABCD' and the 'EFG' wouldn't make it to A.
A = B;
In this case, the value resulting from the LHS is again longer than is
needed. The assignment will truncate the value of B, taking only
the frst 4 characters and assigning them to A.
MODULE 03 Page 5 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
EXPRESSION EVALUATION
Wherever we in our program need to produce a new value, we work it
out as an expression. This applies not only to "sums", but also to string
operations such as extracting the Surname from the complete name or
fnding out whether some data that has come into our program is valid.
An expression in PL/1 can take many diferent forms, but there is one
thing that is always true. The way of evaluating (working out) the
expression is controlled by strict rules, and the result of the expression is
always predictable in form, if not in value. For example, within our
program, we may need to fnd the net pay value by subtracting the tax
from the gross pay. We will have specifed that the gross pay is being
held as a FIXED DEC variable with precision (7,2). Similarly the tax may
be held as a FIXED DEC (5,0) value. We will code an expression in our
program to subtract these two to give us the value for the net pay. PL/1
will decide at compilation time the precision of the result of the
expression, irrespective of the value it will have.
The frst type of expression we'll come across is an operational
expression. These are the type of expression we're used to, from
working out our budget at home, or from basic Algebra at school. Here
we have an operator (such as + or -) and usually two operands - the
values to be operated upon.
Arithmetic Operators.
The four basic arithmetic operators in PL/1 are:
add subtract multiply divide (as we would expect)
and the symbols used for them are:
+ - * / respectively.
If we're going to code an arithmetic operation in our program, we code:
operand operator operand
where the operands are suitable values to be used with that operator.
So, for example, we would ensure that if we were doing an arithmetic
operation, that both of our operators had numeric values.
MODULE 03
Page 6 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
1 + 2
The operands are 1 and 2, and the operator is +. We can also have:
JOURNEYS + 2
THIS_MONTH_SALARY + YEAR_TO_DATE_SALARY
In the same way we can have:
JOURNEYS 1!
ANNUAL_RATE * NO_OF_YEARS
TOTAL_DISTANCE " NO_OF_LEGS
In each of the above cases, PL/1 evaluates the expression and the result
of the expression is then used in whatever way is determined by the
context. This may be in another operation, the value resulting From the
operation may be used as an operand in the next operation:
THIS_YEAR + LAST_YEAR + #
HOURLY_RATE * NO_OF_HOURS * O$ERTIME_%ERCENT
The above two expressions each have 2 operators. In this case, where
we have + and +, or * and * , PL/1 simply starts at the left and does the
operations from left to right as it fnds them. In the frst example, PL/1
adds THIS_YEAR and LAST_YEAR to give an intermediate result. This
intermediate result is then added to 5 to give the fnal result. In the
second example, OVERTIME_PERCENT is multiplied by the product of
HOURLY_RATE and NO_OF_HOURS.
MODULE 03 Page 7 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
ARITHMETIC ASSIGNMENTS OF EXPRESSIONS
As we can now code arithmetic assignments and arithmetic expressions,
it's only a small step to combining them and coding statements such as:
DCL RATE FI&ED DEC(#'2);
DCL HOURS FI&ED DEC(!'1);
DCL GROSS FI&ED DEC(('2);
GROSS = RATE ) HOURS;
First PL/1 evaluates the expression on the right hand side. This is done
with no consideration of the use that will be made of the result. Having
worked out an "answer" for the expression, PL/1 then looks to see where
the answer is going. As it is an assignment, then the answer is adjusted
to match with the attributes of the left hand side, and then assigned
across. Its important to note that the evaluation of the RHS is done
before, and independently of, the assignment to the LHS.
Note: ON-TYNE DO NOT USE THE SYMBOLS / AND * FOR
MULTIPLYING AND DIVIDING, THEY USE BUILTIN FUNCTIONS
CALLED MULTIPLY AND DIVIDE. THESE ARE COVERED LATER .
MODULE 03
Page 8 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
COMPARISON OPERATORS
The arithmetic operators worked on two arithmetic values to give a result
which was also an arithmetic value. The comparison operators are
diferent, in that they can operate on any data type, but the result they
give will always be a BIT string of length 1.
The operators are:
= Equal to
> Greater than
< Less than
>=Greater than or equal to
<=Less than or equal to
= Not equal to
> Not greater than
< Not less than
Note that the last 5 are composite symbols, the two characters which
form the symbol must not have a space between them.
We use the comparison operators in the same way that we used the
arithmetic operators. We code an operand on each side which are the
two values to be compared:
NO_OF_CHILDREN * #
AGE = RETIREMENT_AGE
EOF_RTS = '1'B
NAME = 'DA$ID'
In each of the above cases two things are true:
MODULE 03 Page 9 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
The operands on either side of the comparison operator are suitable
for being compared (eg. both character string, both FIXED
DECIMAL).
The result of the operation is a BIT(1) value - '1'B if the comparison
was true, '0'B otherwise.
Comparing CHARACTER string values
This is done on a character-by-character basis, starting at the left or
beginning of the string. If the two strings are not of the same length, the
shorter of the two is imagined to be padded on the right with spaces to
be of the same length as the longer. If all corresponding characters are
the same, then the two strings are equal. If a position in the strings is
found where the characters are not equal, then the string which has the
lower character in that position is the lesser of the two strings. "Lower" in
terms of comparing characters means "earlier in the collating
sequence".
The Collating Sequence
This the order of characters that can be held in the storage of an IBM
computer. It's called EBCDIC. Although it includes both printable and
non-printable characters, when dealing with CHAR data in PL/1
programs we are concerned mainly with printable characters. The
sequence for printable characters is:
lowest
Space
Punctuation in the order . < ( + | & * ) ; - /
, % _ > ? : # @ ' =
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
highest
Comparing BIT string values
Bit data will be covered in more detail later in the course.
MODULE 03
Page 10 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
Comparing Arithmetic values
When we compare FIXED DEC with FIXED DEC, or FIXED BIN with
FIXED BIN, then we have a normal arithmetic comparison of the values -
irrespective of the precision. So:
5 > 6 not true result'0'B
-1 < 1 true result'1'B
5 = 5.5 not true result '0'B
MODULE 03 Page 11 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
STRING OPERATOR (||)
This is known as "concatenation", and means gluing together end to
end. When we concatenate two strings of the same type, we form a
longer string of the same type, whose length is the sum of the lengths of
the operands, and whose value is the value Of the frst followed by the
value of the second. Thus 'ABC' || 'PQ' gives us 'ABCPQ', a CHAR(3)
value concatenated with a CHAR(2) value gives us a CHAR(5) value.
If we started with this coding:
DCL A CHAR(#);
DCL B CHAR(();
DCL C CHAR(1);
A = 'ABCDE';
B = 'LMNO%+R';
C = '&';
.......then these assignments of the results of concatenation operations
would have the efects described:
A = A ,, 'F';
No efect. A has length 5, value 'ABCDE'. The result of the
concatenation is therefore a string of length 6 (5+1) value 'ABCDEF'.
When we assign a 6-character value to a 5-character variable, the value
is truncated on the right to be the correct length. Therefore the value
assigned into A is simply 'ABCDE'.
B = A ,, C ,, C;
B is set to 'ABCDEXX'. The total length of the RHS is 7 (5+1+1) and its
value is 'ABCDEXX'. Therefore when it is assigned, the whole value is
taken by B.
B = C ,, '&Y-';
The RHS has length 4, and value 'XXYZ'. When this is assigned into B,
with length 7, it gets padded on the right with 3 spaces. The fnal value
of B is therefore 'XXYZ^^^'.
MODULE 03
Page 12 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
Note: Concatenation is useful and efcient on CHAR strings.
Although it is legal to use it on BIT values, it is rarely done and not
recommended.
MODULE 03 Page 13 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
PRIORITY OF OPERATORS
So far the expressions we've seen have only contained one type of
operator. We also need to look at the efect of coding an expression with
a mixture of operators.
Example:
1 + 2 * 3 / 4
Suppose we entered the above calculation on a pocket calculator. We
might get the answer 2.25, or we might get the answer 2.5.
It all depends on the sequence in which the operations were performed.
(Mental exercise - see how the two possible results were obtained).
PL/1 uses a strict priority sequence to decide in which order the
operations are to be performed. In an expression, if there is no reason
to act otherwise, PL/1 performs operations in the following order:
* and / (multiply and divide)
+ and - (add and subtract)
| | (concatenation)
= etc. (comparisons)
So in an expression which contained only the above operators, PL/1
would do:
all the * and / (from left to right)
all the + and - (from left to right)
all the | | (from left to right)
all the comparisons (from left to right)
In PL/1, the value of 1 + 2 * 3 / 4 would therefore be 2.5, because it
would do the * and / frst, from left to right, giving:
1 + 6 / 4
MODULE 03
Page 14 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
and then: 1 + 1.5
and then it would do any + and - left, giving: 2.5
In the same way, if we coded in PL/1:
A + B * C / D
then PL/1 would frst multiply B and C. It would take the result of the
multiplication and divide it by D, and would take the result of the division
and add that to A.
Example:
A + B > C - D
Here there are no * and /, so PL/1 does + and -. It adds A and B and
remembers the result. It then subtracts D from C, and compares that
with the remembered result of A + B. The value of the complete
expression is the result of the comparison operation.
Note: Although the only operands in this expression are FIXED
DEC, the value of the expression is a BIT(1) value, because the last
operation to be performed was the comparison.
MODULE 03 Page 15 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
USING PARENTHESES
Because the priority of the operators may not give us the efect that we
require, we can control the sequence of operations in an expression by
using parentheses (brackets).
Example:
A = B + C * D;
In this case, the expression is: B + (product of C and D)
A = (B + C) * D;
In this case, the expression is: (sum of B and C) times D.
Putting part of the expression in parentheses tells PL/1 to evaluate that
frst. Parentheses take priority over all of the operators. If there are
parentheses within parentheses, then PL/1 does the innermost ones 1st
Example:
A = (((B - C) / D) + (A + B / F));
This evaluated in the sequence:
B - C
(B - C) / D
B / F
A + (B / F)
((B - C) / D) + (A + B / F)
Even when the parentheses are not required, it is sometimes worth
putting them in, just to improve the clarity of the coding. This
statements:
A = P1 * P2 + Q1 * Q2 + R1 * R2;
might be better as:
A = (P1 * P2) + (Q1 * Q2) + (R1 * R2);
or even as:
A = (P1 * P2)
MODULE 03
Page 16 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
+ (Q1 * Q2 )
+ (R1 * R2);
MODULE 03 Page 17 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
EXPONENTIATION (WRITTEN AS **)
This has been left till last among the operators, because it is only rarely
used in commercial work, and then only in a limited fashion.
The expression A ** B means 'A raised to the power of B'. If B was 2,
then it would be 'A squared'. If B were 3 then 'A cubed', and so on.
Occasionally one has a need for "squared", and very occasionally for
"cubed". If you need to use exponentiation in a program, then make
sure that the second operand is an integer, and preferably a constant
integer.
Exponentiation has a higher priority than the other arithmetic operators,
the priority table including ** is as follows:
** (exponentiation and not)
* and / (multiply and divide)
+ and - (add and subtract)
| | (concatenation)
= etc (comparisons)
Note also that exponentiation is done from right to left so that:
A ** B ** C
is equivalent to
A ** (B ** C)
MODULE 03
Page 18 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
MULTIPLE ASSIGNMENTS
There are times when we want to take a value, and assign it to more
than one variable.
Example:
A = B / C;
D = B / C;
To save on coding, and the overhead of working out the RHS twice in
this case, we are allowed to combine the two simple assignments into a
single multiple assignment.
A,D = B / C;
With a Multiple Assignment, we code two or more variables on the LHS
of the assignment and separate them by commas. We only code a
single value (constant, variable, expression) on the RHS. Here PL/1
evaluates the RHS, once only, and then assigns it individually to each of
the "target" variables on the LHS. As always, the RHS is evaluated
without reference to the target or targets. The result of B / C has a
certain set of attributes. This result data item is then assigned
individually to A and to D.
ASSIGNMENT STATEMENT
Format: variable_name = value;
Variable_name must be the name of a declared PL/1 variable.
Value must be a value suitable for the variable to hold.
Value may be constant, variable or expression.
Execution: PL/1 evaluates, if necessary, the value on the right of the
assignment.
PL/1 "adjusts" the value if necessary to the attributes of the variable on
the left of the assignment.
MODULE 03 Page 19 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
PL/1 places the "adjusted" value in the variable.
The form of adjustment depends on the type of the assignment, which
depends on the data type of the target variable.
Arithmetic assignments: The value is aligned with the decimal point of
the target. Excess digits to the left or right are truncated. if there are
insufcient digits to the left or right then zeros are appended. Note that
your program is in error if signifcant (left-hand) digits are truncated.
String Assignment: The value is aligned at the left of the target. Excess
bits or characters on the right are truncated. If there are insufcient
characters or bits then character or bit zeros are added on the right.
MODULE 03
Page 20 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
OPERATIONAL EXPRESSIONS
A simple operational expression consists of:
operand-1 operator operand-2
where the operator is one of:
+ - * /
< > >= <= = = < >
operand-1 is:
value (constant or variable)
operational expression
(operational expression)[operational expression in parentheses]
operand-2 is:
value (constant or variable)
(operational expression)
Rules: The operands must be of a data type suitable for the operation
All the operators described above are evaluated from left to right
except ** which is evaluated from right to left.
If an operand of an expression is itself an operation in
parentheses, then the operation in parentheses will be performed
before the frst operation.
Within an operational expression without parentheses, operations
are carried out in the following priority sequence:
**
* /
+ -
MODULE 03 Page 21 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
| |
comparisons
MODULE 03
Page 22 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
OPERATORS
Arithmetic
Add(+), Subtract(-), Multiply(*), Divide(/), Exponentiate(**)
Operands: Both operands must be of a type suitable for
arithmetic (eg. both FIXED DEC or both FIXED BIN). For
exponentiation, the second operand should be an integer constant.
Result: The result is an arithmetic value of the same type as
the operands, and of a precision large enough to hold any result value.
Concatenation II
Operands: Both operands must be of a type suitable for a
concatenation operation, eg. both CHARACTER).

Result: The result is a string of the same type as the
operands. Its length is the sum of the two operand lengths. Its value is
the frst operand followed by the second operand value.
Comparison:
> Greater than
< Less than
= Equal to
>= Greater than or equal to
<= Less than or equal to
= Not equal to
MODULE 03 Page 23 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
< Not less than
> Not greater than
Operands: Both operands must be of a type suitable for comparing
(eg. both CHAR, both FIXED DEC).
If the operands are CHAR, then the shorter is considered to be padded
to the length of the longer with spaces.
If the operands are BIT, then the shorter is considered to be padded to
the length of the longer with BIT '0'.
Result: The result of the operation is a BIT value of length 1. Its
value is '1'B if the comparison is TRUE, '0'B if the comparison is FALSE.
Arithmetic comparisons are done on the algebraic value. Signs are
taken into consideration.
CHAR comparisons are performed according to the collating sequence
(qv). If all corresponding characters in the two strings are equal, then
the strings are equal. Otherwise, the string with the higher character in
the frst position of inequality is the higher.
COLLATING SEQUENCE.
For the purpose of comparison, the characters available for data are
arranged in a 'collating sequence'. This is also shown earlier in the
notes. For printable characters it is as follows:
lowest
Space
Punctuation in the order . < ( + | & * ) ; - /
, % _ > ? : # @ ' =
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
MODULE 03
Page 24 of 20
BASIC PL/1 ASSIGNMENTS & EXPRESSIONS
0123456789
highest
If in doubt, it is very easy to work out - just look up the HEX values of all
EBCDIC characters on your EBCDIC character code chart...... the
collating sequence is just in HEX order.
MULTIPLEASSIGNMENT
Format: variable, variable,.... = value;
Efect:The value on the right of the assignment = is evaluated. It is then
assigned in turn to each of the variables listed on the left of the =.
As with a simple assignment, the value is evaluated with no concern for
the eventual target of the result.
Each of the simple assignments implied by the multiple assignment acts
in the same way as a normal non-multiple assignment.
MODULE 03 Page 25 of 20

You might also like