• Keine Ergebnisse gefunden

SIMPLE TYPE EXPRESSIONS

Im Dokument Pascal Reference (Seite 148-155)

As a rule, the operands and the value resulting from an operation are all of the same type.

Occasionally, however, the type of an operand is changed to the type required by an operator.

This conversion occurs on two levels: one for constant operands only, and one for all operands.

INTEGER to WORD conversion occurs for constant operands only; conversion from INTEGER to REAL and from INTEGER or WORD to INTEGER4 occurs for all operands.

If necessary in constant expressions, INTEGER values change to WORD type.

If

NOTE

Be careful when mixing INTEGER and WORD con-stants in expressions. For example, if CBASE is the constant l6#C000 and DELTA is the constant -1, the following expression gives a WORD overflow:

WRD (CBASE) + DELTA

The overflow occurs because DELTA is converted to the WORD value l6#FFFF, and l6#C000 plus 16#FFFF is greater than MAXWORD. However, the following would work:

WRD (ORD (CBASE) + DELTA)

This expression gives the INTEGER value -16385, which changes to WORD 16#BFFF.

conversion is needed by an operator or for an assignment, the compiler makes the following conversions:

0 from INTEGER to REAL or INTEGER4

0 from WORD to INTEGER4

Expressions 11-3

The following rules determine the

DIV MOD

AND OR XOR

These are the operators for integer division quotient and remainder, respectively. The left operand

(dividend) is divided by the right operand (divisor).

Examples of integer division:

123 MOD "5

=

3 bi twise logical functions. Oper-ands must be INTEGER or WORD or

Given the following initial INTEGER values,

x =

2#1111000011110000 Y

=

2#1111111100000000

AND, OR, XOR, and NOT perform the following functions:

X AND Y

X OR Y

X XOR Y

NOT X

1111000011110000 1111111100000000 1111000000000000 1111000011110000 1111111100000000 1111111111110000 1111000011110000 1111111100000000 0000111111110000 1111000011110000 0000111100001111

BOOLEAN EXPRESSIONS

The Boolean operators at the standard level are:

NOT AND OR

< >

<> <= >=

XOR is available at the extend level.

You can also use P <> 0 as an exclusive OR func-tion. Since FALSE < TRUE, P <= 0 denotes the Boolean operation lip implies 0."

The Boolean operators AND and OR are not the same as the WORD and INTEGER operators of the same name that are bitwise logical functions. The Boolean AND and OR operators mayor may not evaluate their operations. The following example illustrates the danger of assuming that they do not:

WHILE (I <= MAX) AND (V [IJ <> T) DO I := I + I:

If array V has an upper bound MAX, then the evaluation of V [IJ for I > MAX is a run-time error. This evaluation mayor may not take place.

Sometimes both operands are evaluated during optimization, and sometimes the evaluation of one can cause the evaluation of the other to be skipped. In the latter case , either operand can be evaluated first.

Instead, use the following construction:

WHILE I <= MAX DO

IF V [IJ <> T THEN I := I + 1 ELSE BREAK:

(See the subsection "Sequential Control" in Sec-tion 12, "Statements," for informaSec-tion on using AND THEN and OR ELSE to handle situations, such as the previous example, where tests are examined sequentially. )

The relational operators produce a Boolean result.

The types of the operands of a relational operator (except for IN) must be compatible. If they are not compatible, one must be REAL and the other compatible with INTEGER.

Expressions 11-7

Reference types can only be compared with = and

< > • To compare an address type with one of the other relational operators, you must use address

field notation, as shown:

IF (A.R < B.R) THEN <statement>:

(See Section 8 "Reference and Other Types" for a discussion of the address type.)

Except for the string types STRING and LSTRING, you cannot compare files, arrays, and records as wholes. Two STRING types must have the same upper bound to be compared: two LSTRINGs can have dif-ferent upper bounds.

In LSTRING comparison, characters past the current length are ignored. If the current length of one LSTRING is less than the current length of the other and all characters up to the current length of the shorter are equal, the compiler assumes the shorter one is IIl ess than II the longer one. How-ever, two LSTRINGs are not considered equal unless all current characters are equal and their current lengths are equal. (See the subsection IIUsing STRINGs and LSTRINGslI in Section 6, "Arrays, Records, and Sets,1I for more information.)

The six relational operators =, <>, <=, >=, <, and

> have their normal meaning when applied to numeric, enumerated, CHAR, or string operands.

The subsection, "Set Expressions," discusses the meaning of these relational operators (along with the relational operator IN) when applied to sets.

Since the relational operators in Boolean expres-sions have a lower precedence than logical AND and OR, the following equivalent statements are incorrect:

IF I < 10 AND J = K THEN IF I < (10 AND J) K THEN Instead, you must write:

IF (I < 10) AND (J K) THEN

Note that the following is also not allowed, if I is not an INTEGER constant:

$IF I $THEN

The inclusion of special "not-a-number" (NaN) val ues means that a comparison between two real numbers can have a result other than less than, equal, or greater than. The numbers can be un-ordered, meaning one or both are NaNs. An unordered result is the same as "not equal, not less than, and not greater than."

For example, if variables A or B are NaN values:

0 A < B is false.

0 A <= B is false.

0 A > B is false.

0 A >= B is false.

0 A =B is false.

0 A <> B is, however, true.

REAL comparisons do not follow the same rules as other comparisons in many ways. A < B is not always the same as NOT (B <= A): this prevents some optimizations otherwise done by the compiler.

If A is a NaN, then A <> A is true: in fact, this is a good way to check for a NaN value.

Expressions 11-9

SET EXPRESSIONS

Im Dokument Pascal Reference (Seite 148-155)