• Keine Ergebnisse gefunden

1 THEN BEGIN

Im Dokument INTRODUCTION AND FEATURES (Seite 136-144)

IF J

=

1 THEN

WRITELN(II equals JI) ELSE

WRITELN(IDONE only if I = 1 and J <> 11)

{This ELSE is paired with the most deeply nested IF. Thus, the second WRITELN is executed only if I = 1 and J <> I.}

IF I 1 THEN BEGIN

IF J = 1 THEN WRITELN('I equals J')

Examples of CASE statements:

CASE OPERATOR OF

The OTHERWISE clause contains additional statements to be executed in the event that the CASE index value is not in the given set of CASE constant values. Note that OTHERWISE cannot be used with RECORD declarations. If the CASE index value is not in the set and no OTHERWISE clause is present one of the of two things happen:

1. If the $RANGECK is on, a runtime error is generated.

2. If the $RANGECK is off, the result is undefined.

Depending on optimization, the code generated by the compiler for a CASE statement may be either a "jump table" or series of comparisons (or both). If it is a jump table, a jump to an arbitrary location in memory can occur if the control variable is out of range and the range checking switch is off.

Repetitive Statements

Repetitive statements specify repeated execution of a statement.

These statements are functionally equivalent to a GOTO but easier to use.

The WHILE Statement

The WHILE statement repeats a statement zero or more times, until a Boolean expression becomes false.

Examples of WHILE statements:

WHILE P <> NIL DO P := NEXT (P) WHILE NOT MICKEY DO

BEGIN

NEXTMOUSEi

MICE := MICE + 1 END

The WHILE statement should be used when no iterations of the loop are desired. The REPEAT statement should be used to execute loops where at least one iteration of the the loop is desired.

The REPEAT Statement

The REPEAT statement repeats a sequence of statements one or more times, until a Boolean expression becomes true.

Examples of REPEAT statements:

REPEAT

READ (LINEBUFF);

COUNT := COUNT + 1 UNTIL EOF;

REPEAT GAME UNTIL TIRED;

You should use the REPEAT statement to execute statements, one or more times until a condition becomes true. This differs from the WHILE statement in which a single statement may not be executed at all.

The FOR Statement

The FOR statement indicates to the compiler to execute a statement repeatedly while a progression of values is assigned to a variable, called the control variable of the FOR statement.

The val ues assigned start wi th a val ue called the ini tial val ue and end with one called the final value.

The FOR statement has two forms, one where the control var iable increases in value and one where the control variable decreases in value:

FOR I := 1 TO 10 DO {I is the control variable.}

SUM := SUM + VICTORVECTOR [I];

FOR CH := 'z' DOWNTO 'A' bo

WR I TE. (CH) ;

{CH is the control variable.}

You can also use a FOR statement to step through the values of a set, as follows:

FOR TINT := LOWER (SHADES) TO UPPER (SHADES) DO IF TINT IN SHADES

THEN PAINT_AREA (TINT);

The following are explicit rules defined within ISO pascal regarding the control variables in FOR statements:

1. It must be of an ordinal type.

2. It must also be an entire variable, not a component of a structure.

3. It must be local to the immediately enclosing program, procedure, or function and cannot be a reference parameter of the procedure or function.

However, in this extended Pascal, the control var iable may also be any STATIC variable, such as a variable declared at the program level, unless the variable has a segmented ORIGIN attribute.

4. No assignments to the control variable are allowed in the repeated statement. This error is caught by making the control variable READONLY within the FOR statement;

it is not caught when a procedure or function invoked by

the repeated statement alters the control variable. The control variable cannot be passed as a VAR (or VARS) parameter to a procedure or function.

5. The initial and final values of the control variable must be compatible with the type of the control variable. If the statement is executed, both the initial and final values must also be assignment compatible with the control variable. The initial value is always evaluated first, and then the final value.

Both are evaluated only once before the statement executes.

The statement following the DO is not executed at all if:

1. The initial value is greater than the final value in the TO case.

2. The initial value is less than the final value in the DOWNTO case.

The sequence of values given the control variable starts with the initial value. This sequence is defined with the SUCC function for the TO case or the PRED function for the DOWNTO case until the last execution of the statement, when the control variable has its final value.

The value of the control variable, after a FOR statement terminates naturally (whether or not the body executes), is undefined. It may vary due to optimization and, if $INITCK is on, may be set to an uninitialized value. However, the value of the control variable after leaving a FOR statement with GOTO or BREAK is defined as the value it had at the time of exit.

At the standard level, the body of a FOR statement mayor may not be executed, so a test is necessary to see whether the body should be executed at all. However, if the control variable is of type WORD (or a subrange) and its initial value is a constant zero, the body must be executed no matter what the final val ue.

In this case, no extra test need be executed and no code is generated to perform such a test.

You may use temporary control variables:

FOR VAR control-variable

The prefix VAR causes the control variable to be declared local to the FOR sta tement (i. e., at a lower scope) and need not be declared in a VAR section. Such a control variable is not available outside the FOR statement, and any other variable with the same identifier is not available within the FOR statement itself. Other synonymous variables are, however, available to procedures or functions called within the FOR statement.

Examples of temporary control variables:

FOR VAR I := 1 TO 100 DO SUM := SUM + VICTOR [I]

FOR VAR COUNTDOWN := 10 DOWNTO LIFT OFF DO MONITOR ROCKET

The BREAK and CYCLE Statements

In theory, a program using the BREAK and CYCLE statements does not need to use any GOTO statements. Each of these two statements has two forms, one with a loop label and one without.

A loop label is a normal GOTO label prefixed to a FOR, WHILE, or REPEAT statement. You should use integers for labels referenced by GOTOs and identifiers for loop labels.

Examples of CYCLE and BREAK statements:

LABEL SEARCH, CLIMB;

SEARCH: WHILE I <= I TOP DO

IF PILE [I] TARG~T THEN BREAK SEARCH E LS E I :

=

I + 1;

FOR I := 1 TO N DO

IF NEXT [I] NIL THEN BREAK;

CLIMB: WHILE NOT ITEM~.LEAF DO BEGIN

IF ITEM~.LEFT

<>

NIL

THEN [ITEM := ITEM~.LEFT; CYCLE CLIMB];

IF ITEM~.RIGHT

<>

NIL

THEN [ITEM := ITEM~.RIGHT; CYCLE CLIMB];

WRITELN ('Very strange node');

BREAK CLIMB END;

The WITH Statement

The WITH statement opens the scope of a statement to include the fields of one or more records, so you can refer to the fields directly. For example, the following statements are equivalent:

WITH PERSON DO WRITE (NAME, ADDRESS, PHONE)

WRITE (PERSON.NAME, PERSON.ADDRESS, PERSON. PHONE)

The record given may be a variable, constant identifier,

structured constant, or function identi fier; it may not be a component of a PACKED structure. If you use a function identifier, it refers to the function's local result variable.

If the record given in a WITH statement is a file buffer variable, the compiler issues a warning, since changing the position in the WITH statement may cause an error.

The record given can also be any expression in parentheses, in which case the expression is evaluated and the result assigned to a temporary (hidden) variable. If you want to evaluate a function designator, you must enclose it in parentheses.

You can give a list of records after the WITH, separated by commas. Each record must be of a different type from all the others, since the field identifiers refer only to the last instance of the record wi th the type. These statements are equivalent:

WITH PMODE, QMODE DO statement

WITH PMODE DO WITH QMODE DO statement

Any record var iable of a WITH sta temen t tha t is a componen t of another variable is selected before the statement is executed.

Active WITH variables should not be passed as VAR or VARS parameters, nor can their pointers be passed to the DISPOSE procedure. However, these errors are not caught by the compiler.

Assignments to any of the record variables in the WITH list or components of these var iables are allowed, as long as the WITH record is a variable.

Every WITH statement allocates an address variable that holds the address of the record. If the record variable is on the heap, the pointer to it should not be DISPOSEd withih the WITH statement. If the record variable is a file buffer, no I/O should be done to the file within the WITH statement.

Sequential Control

To increase execution speed or to ensure correct evaluation, it is often useful in IF, WHILE, and REPEAT statements to treat the Boolean exprsssion as a series of tests. If one test fails, the remaining tests are not executed. The sequential control operators provide for the following tests:

1. AND THEN

X AND THEN Y is false if X is false; Y is only evaluated if X is true.

2. OR ELSE

OR ELSE Y is true if X is true; Y is only evaluated if X is false.

If you use several sequential control operators, the compiler evaluates them strictly from left to right. You may only include these operators in the Boolean expression of an IF, WHILE, or UNTIL clause; they may not be used in other Boolean expressions.

Furthermore, they may not occur in parentheses and are evaluated after all other operators.

Examples of sequential control operators:

IF SYM <> NIL AND THEN SYM~.VAL < 0 THEN NEXT SYMBOL

WHILE I <= MAX AND THEN VECT [I] <> KEY DO I := I + 1;

REPEAT GEN (VAL)

UNTIL VAL

=

0 OR ELSE (QU DIV VAL)

=

0;

WHILE POOR AND THEN GETTING POORER OR ELSE BROKE AND THEN BANKRUPT DO

GET RICH

CHAPTER 10

Im Dokument INTRODUCTION AND FEATURES (Seite 136-144)