Module 3

27

description

Module 3

Transcript of Module 3

Page 1: Module 3
Page 2: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

MODULE 03

Assignments and Expressions

MODULE 03 Page 1 of 20

Page 3: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

ASSIGNMENT STATEMENTFormat: variable = value;Used to place a value into a variable.

The 'value' specified 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 specified 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 first 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 different for different for different data types, and so they need to be looked at individually.

MODULE 03 Page 2 of 20

Page 4: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

CHARACTER ASSIGNMENTIf 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 fit 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 different 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 fit 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 finally 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 chopped down to fit into A. When assigning strings, PL/1 always truncates on the right, taking only enough characters to fill 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.

MODULE 03 Page 3 of 20

Page 5: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

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 first 4 characters and assigning them to A.

MODULE 03 Page 4 of 20

Page 6: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

EXPRESSION EVALUATIONWherever 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 finding out whether some data that has come into our program is valid.

An expression in PL/1 can take many different 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 find the net pay value by subtracting the tax from the gross pay. We will have specified 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 first 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.

1 + 2

The operands are 1 and 2, and the operator is +. We can also have:

MODULE 03 Page 5 of 20

Page 7: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

JOURNEYS + 2

THIS_MONTH_SALARY + YEAR_TO_DATE_SALARY

In the same way we can have:

JOURNEYS - 13

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 + 5

HOURLY_RATE * NO_OF_HOURS * OVERTIME_PERCENT

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 finds them. In the first 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 final result. In the second example, OVERTIME_PERCENT is multiplied by the product of HOURLY_RATE and NO_OF_HOURS.

MODULE 03 Page 6 of 20

Page 8: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

ARITHMETIC ASSIGNMENTS OF EXPRESSIONSAs 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 FIXED DEC(5,2);

DCL HOURS FIXED DEC(3,1);

DCL GROSS FIXED DEC(7,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 7 of 20

Page 9: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

COMPARISON OPERATORSThe arithmetic operators worked on two arithmetic values to give a result which was also an arithmetic value. The comparison operators are different, 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 > 5AGE = RETIREMENT_AGE

EOF_RTS ¬= '1'BNAME = 'DAVID'

In each of the above cases two things are true:

Ì 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.

MODULE 03 Page 8 of 20

Page 10: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

Comparing CHARACTER string valuesThis 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 SequenceThis 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 valuesBit data will be covered in more detail later in the course.

Comparing Arithmetic valuesWhen 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 9 of 20

Page 11: Module 3

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 first 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(5);DCL B CHAR(7);DCL C CHAR(1);

A = 'ABCDE';B = 'LMNOPQR';C = 'X';

.......then these assignments of the results of concatenation operations would have the effects described:

A = A || 'F';

No effect. 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 || 'XYZ';

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 final value of B is therefore 'XXYZ^^^'.

Note: Concatenation is useful and efficient on CHAR strings. Although it is legal to use it on BIT values, it is rarely done and not recommended.

MODULE 03 Page 10 of 20

Page 12: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

PRIORITY OF OPERATORSSo far the expressions we've seen have only contained one type of operator. We also need to look at the effect 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 / first, from left to right, giving:

1 + 6 / 4

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:

MODULE 03 Page 11 of 20

Page 13: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

A + B * C / D

then PL/1 would first 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 12 of 20

Page 14: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

USING PARENTHESESBecause the priority of the operators may not give us the effect 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 first. 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)

+ (Q1 * Q2 )

+ (R1 * R2);

MODULE 03 Page 13 of 20

Page 15: Module 3

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 14 of 20

Page 16: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

MULTIPLE ASSIGNMENTSThere 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.

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

MODULE 03 Page 15 of 20

Page 17: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

insufficient digits to the left or right then zeros are appended. Note that your program is in error if significant (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 insufficient characters or bits then character or bit zeros are added on the right.

MODULE 03 Page 16 of 20

Page 18: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

OPERATIONAL EXPRESSIONSA 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 first operation.

Within an operational expression without parentheses, operations are carried out in the following priority sequence:

** ¬* /+ -| |comparisons

MODULE 03 Page 17 of 20

Page 19: Module 3

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 first 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

¬< Not less than

¬> Not greater than

MODULE 03 Page 18 of 20

Page 20: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

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 first 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

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.

MODULE 03 Page 19 of 20

Page 21: Module 3

BASIC PL/1 ASSIGNMENTS & EXPRESSIONS

MULTIPLE ASSIGNMENT

Format: variable, variable,.... = value;

Effect: 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 20 of 20