• Keine Ergebnisse gefunden

REPEAT PROCESSCft)j GETCf) UNTIL EOFCf)

Im Dokument PASCAL-a6 USER'S GUIDE (Seite 105-110)

7.2.8

FOR Statements

The FOR statement executes its component statement repeatedly while a given finite progression of values is assigned to a variable, which is called the control variable of the FOR statement. The FOR statement is useful when the number of iterations is known at the time of the first iteration.

7-17

Expressions and Statements Pascal-86 User's Guide

The syntax of the FOR statement is:

FOR variable : = expression T 0 expression D 0 statement or:

FOR variable : = expression DOW N T 0 expression D 0 statement

where variable is a local variable of an ordinal type, and the two expressions are assignment-compatible with this type. The statement may be any Pascal statement;

since only one is permitted, you may wish to use a compound statement.

The control variable serves as a counter. The progression of values assigned to the control variable starts with the value of the first expression and ends with the value of the second expression. If the TO form of the statement is used, the values in the ordinal type of the control variable are stepped through in order; if the DO WNTO form is used, the values are stepped through in reverse order. On each iteration, the appropriate value is first assigned to the control variable, and then the statement is executed. If the starting value is greater than the ending value for the TO form, or if the starting value is less than the ending value for the DOWNTO form, the state-ment will never be executed.

An error is caused if the control variable is altered by the repeated statement or by any statement activated by the repeated statement. However, the compiler does not check for such alterations occurring in a procedure or function invoked from within the FOR loop. After a FOR statement is executed, the value of the control variable is defined only if the FOR statement is terminated by a GO TO statement leading out of it, in which case the control variable has the value it had at the time of the GOTO.

Apart from the restrictions just given, the FOR statement of the form:

FOR v : = e 1 T 0 e2 DOs is equivalent to the statement sequence:

BEG I N

END

t em p 1 ,= e1 j

t em p 2 ,= e2 j

IF temp1 ( = temp2 THEN BEGIN v ,= temp1j

Sj

WHILE v () temp2 BEGIN v ,= SUCC(v) j

Sj

END END

Similarly, a FOR statement of the form:

FOR v : = e 1 DOW N T 0 e2 DOs is equivalent to the statement sequence:

BEGIN

temp1 ,=

temp2 ,=

IF temp1 e1j e2j

) = temp2 THEN BEGIN

Pascal-86 User's Guide Expressions and Statements

v := temp1j

Sj

WHILE v () temp2 DO BEGIN v := PREDev)j

Sj

END END END

In both cases, tempI and temp2 are auxiliary variables, of the host type of the varia-ble v, that do not occur elsewhere in the program.

Examples of FOR statements:

FOR i:= 2 TO 63 DO

IF ali] ) max THEN max:=a[i]

FOR i:=1 TO n DO

FOR j:=1 TO n DO BEGIN

x : = 0 i

FOR k:=1 TO n DO

x ,= x + m1[i,k] * m2[k,j]i

m[i,j] ,= x END

FOR c:=blue DOWN TO red DO display(c)

7.2.9 WITH Statements

The WITH statement allows you to access the components of a record variable as if they were simple variables. Inside the WITH statement, including its component statement, all the field identifiers of the given record variables are defined as variable identifiers. Thus the WITH statement effectively extends the scope of the field identifiers of the records listed, so that they may be accessed as simply and efficiently as local variables.

The syntax of the WITH statement is:

WIT H variable [, variable]... D 0 statement

where each variable must be a variable of a record type, and statement may be any Pascal statement. Since only one statement is permitted, you may wish to use a compound statement here.

The statement:

WIT H v 1 , v 2 , v 3,

... ,

vn DO S

is equivalent to:

WIT H v 1 DO WIT H v2 DO

WIT H v3 DO

WITH vn DO S

The record variables vI through vn may be embedded inside each other (representing records within records) or completely separate.

7-19

Expressions and Statements Pascal-86 User's Guide

If the selection of a variable in the list following the keyword WITH involves the indexing of an array or the dereferencing of a pointer, then these actions are executed before the component statement is executed.

To illustrate the WITH statement, let us assume that the following type definitions and variable declarations are given:

TYPE d ate

studentrec RECORD studentname: namei birthdate: datei

WITH currentrec) studentname) birthdate DO BEG I N

currentrec.studentname.middleinit .= 'K' cur r e n t r e c. b i r t h d ate . day • = 2 8 i

currentrec.birthdate.month .= 5i currentrec.birthdate.year .= 1948i

Likewise,the WITH statement:

WITH todaysdate DO

Pascal-86 User's Guide Expressions and Statements

is equivalent to:

IF todaysdate.month 12

THEN BEGIN

todaysdate.month

.=

1;

todaysdate.year

.=

todaysdate.year+1;

END

ELSE todaysdate.month todaysdate .month+ 1

7.2.10 GOTO Statements

The GOTO statement, a simple statement, specifies that further processing is to continue at some other part of the program, namely, at the statement marked by the given label. The GOTO statement is presented last in this chapter because it is considered good programming practice to avoid GO TO statements whenever possible.

Eliminating all or most GOTO statements improves the clarity and reliability of your programs.

The syntax of the GOTO statement is:

GOT 0 label where

label is a statement label.

The label must have been declared in a label declaration (4.2) whose scope includes the block in which the GOTO falls, and it must appear in the label field of some statement within its declared scope.

The following restrictions apply to the placement of GO TO statements:

• A GOTO statement leading to the label that prefixes a statement S causes an error unless the GO TO statement is activated either by S or by a statement in the statement sequence (list of statements separated by semicolons) to which S immediately belongs. In other words, jumps into subordinate statements are not permitted.

• A GOTO statement may not refer to a case constant within a CASE statement.

To avoid unreachable statements, a GOTO statement must be the last statement in a statement sequence (list of statements separated by semicolons), or else it must be followed by a labeled statement.

A GO TO statement leading out of a procedure causes the termination of the procedure containing the GO TO and all procedures activated by the procedure containing the label. If more than one activation of the target procedure exists, the activation selected is the one containing the variables that are accessible at the GOTO statement. This is usually the most recent activation of the procedure.

Examples of GO TO statements:

GoTo 3500

GOT 0 1

7-21

CHAPTER 8 PREDEFINED PROCEDURES AND FUNCTIONS

This chapter defines the predefined, or built-in, procedures and functions in Pascal-86. You may invoke these from any part of a program-the procedures by means of procedure statements, the functions by means of function designators within expressions.

You may also redefine any of these procedures or functions within your program by declaring your own routines with the same names. Within the scope of an explicit procedure or function declaration, this declaration overrides the predefined procedure or function.

The following kinds of predefined procedures and functions are provided In

Pascal-86:

• Functions that operate on values of ordinal type Predicates or Boolean functions

Arithmetic functions operating on integer or real values

Transfer functions that perform numeric conversions on real values Procedures for allocation of dynamic variables

Data transfer procedures for packing and unpacking of arrays Procedures for handling files and text files

For each predefined procedure and function, this chapter gives a definition of what it does, the syntax of the procedure statement or function designator to invoke it, other pertinent discussion, and examples. In the syntax, optional blanks within the parameter lists have been omitted for readability.

NOTE

The predefined procedures and functions cannot be passed as arguments to procedural or functional parameters.

An expression is of an integer type if it is the same as one of the predefined types INTEGER, or is a valid subrange of these types (5.3.1). An expression is of a~eal type if it is the same as one of the predefined types REAL,

Im Dokument PASCAL-a6 USER'S GUIDE (Seite 105-110)