Statements

Statements describe algorithmic actions that can be executed. Labels can prefix statements, and these labels can be referenced by goto statements.

Statements - 图1statement

A label is either a digit sequence in the range 0 to 9999 or an identifier.

There are two main types of statements: simple statements and structured statements.

Simple statements

A simple statement is a statement that doesn’t contain any other statements.

simple statement

Assignment statements

Assignment statements replace the current value of a variable with a new value specified by an expression. They can be used to set the return value of the function also.

assignment statement

Statements - 图2Statements - 图3

The expression must be assignment-compatible with the type of the variable or the type of the function result. See the section “Type compatibility” on page 25.

Some examples of assignment statements follow:

X := Y + Z;

Done := (I >= 1) and (I < 100);

Hue1 := [Blue, Succ(C)];

I := Sqr(J) - I * K;

Procedure statements

A procedure statement activates a procedure specified by a procedure identifier, a method designator, a qualified-method designator, or a procedural-type variable reference. If the corresponding procedure declaration contains a list of formal parameters, then the procedure statement must have a matching list of (parameters listed in definitions are formal parameters; in the calling statement, they are actual parameters). The actual parameters are passed to the formal parameters as part of the call. For more information, see Chapter 8, “Procedures and functions.”

procedure statement

Some examples of procedure statements follow:

PrintHeading;

Transpose(A, N, M);

Find(Name, Address);

Goto statements

A goto statement transfers program execution to the statement marked by the specified label. The syntax diagram of a goto statement follows:

goto statement

When using goto statements, observe the following rules:

  • The label referenced by a goto statement must be in the same

    block as the goto statement. In other words, it’s not possible to jump into or out of a procedure or function.

  • Jumping into a structured statement from outside that structured

    statement (that is, jumping to a deeper level of nesting) can have undefined effects, although the compiler doesn’t indicate an error. For example, you shouldn’t jump into the middle of a for loop.

Good programming practices recommend that you use goto statements as little as possible.

Structured statements

Structured statements are constructs composed of other statements that are to be executed in sequentially (compound and with statements), conditionally (conditional statements), or repeatedly (repetitive statements).

structured statement

Compound statements

The compound statement specifies that its component statements are to be executed in the same sequence as they are written. The component statements are treated as one statement, crucial in contexts where the Object Pascal syntax only allows one statement. begin and end bracket the statements, which are separated by semicolons.

Statements - 图4compound statement

Here’s an example of a compound statement:

begin

Z := X;

X := Y;

Y := Z;

end;

Conditional statements

A conditional statement selects for execution a single one (or none) of its component statements.

conditional statement

If statements

The syntax for an if statement reads like this:

Statements - 图5if statement

The expression must yield a result of the standard type Boolean. If the expression produces the value True, then the statement following then is executed.

If the expression produces False and the else part is present, the statement following else is executed; if the else part isn’t present, execution continues at the next statement following the if statement.

The syntactic ambiguity arising from the construct

if e1 then if e2 then s1 else s2;

is resolved by interpreting the construct as follows:

####### Note

No semicolon is allowed preceding an else clause.

if e1 then begin

if e2 then

s1 else s2

end;

Usually, an else is associated with the closest if not already associated with an else. Two examples of if statements follow:

if X < 1.5 then

Z := X + Y

else

Z := 1.5;

if P1 <> nil then

P1 := P1^.Father;

Case statements

The case statement consists of an expression (the selector) and a list of statements, each prefixed with one or more constants (called case constants) or with the word else. The selector must be of a byte-sized or word-sized ordinal type, so string types and the integer type Longint are invalid selector types. All case constants must be unique and of an ordinal type compatible with the selector type.

Statements - 图6case statement case of

;

end

;

Statements - 图7case

else part

The case statement executes the statement prefixed by a case constant equal to the value of the selector or a case range containing the value of the selector. If no such case constant of the case range exists and an else part is present, the statement following else is executed. If there is no else part, execution continues with the next statement following the if statement.

These are examples of case statements:

case Operator of

Plus: X := X + Y;

Minus: X := X - Y;

Times: X := X * Y;

end;

case I of

0, 2, 4, 6, 8: Edit1.Text := 'Even digit';

1, 3, 5, 7, 9: Edit1.Text := 'Odd digit';

10..100: Edit1.Text := 'Between 10 and 100';

else

Edit1.Text := ’Negative or greater than 100';

end;

Ranges in case statements must not overlap. So for example, the following case statement is not allowed:

case MySelector of

5: Edit1.Text := 'Special case'; 1..10: Edit1.Text := 'General case';

end;

Repetitive statements

Repetitive statements specify certain statements to be executed repeatedly.

repetitive statement

If the number of repetitions is known beforehand, the for statement is the appropriate construct. Otherwise, the while or repeat statement should be used.

The Break and Continue standard procedures can be used to control the flow of repetitive statements: Break terminates a repetitive statement, and Continue continues with the next iteration of a repetitive statement. For more details on these standard procedures, see the Visual Component Library Reference.

Repeat statements

A repeat statement contains an expression that controls the repeated execution of a statement sequence within that repeat statement.

repeat statement

Statements - 图8

The expression must produce a result of type Boolean. The statements between the symbols repeat and until are executed in sequence until, at the end of a sequence, the expression yields True. The sequence is executed at least once because the expression is evaluated after the execution of each sequence.

These are examples of repeat statements:

repeat

K := I mod J;

I := J;

J := K;

until J = 0;

repeat

Write('Enter value (0..9): '); Readln(I);

until (I >= 0) and (I <= 9);

While statements

A while statement contains an expression that controls the repeated execution of a statement (which can be a compound statement).

Statements - 图9while statement

The expression controlling the repetition must be of type Boolean. It is evaluated before the contained statement is executed. The contained statement is executed repeatedly as long as the expression is True. If the expression is False at the beginning, the statement isn’t executed at all.

These are examples of while statements:

while Data[I] <> X do I := I + 1;

while I > 0 do begin

if Odd(I) then Z := Z * X; I := I div 2;

X := Sqr(X);

end;

while not Eof(InFile) do begin

Readln(InFile, Line);

Process(Line);

end;

For statements

The for statement causes a statement to be repeatedly executed while a progression of values is assigned to a control variable. Such a statement can be a compound statement.

Statements - 图10for statement

control variable

initial value

final value

The control variable must be a variable identifier (without any qualifier) that is local in scope to the block containing the for statement. The control variable must be of an ordinal type. The initial and final values must be of a type assignment-compatible with the ordinal type. See Chapter 7 for a discussion of locality and scope.

When a for statement is entered, the initial and final values are determined once for the remainder of the execution of the for statement.

The statement contained by the for statement is executed once for every value in the range initial value to final value. The control variable always starts off at initial value. When a for statement uses to, the value of the control variable is incremented by one for each repetition. If initial value is greater than final value, the contained statement isn’t executed. When a for statement uses downto, the value of the control variable is decremented by one for each repetition. If initial value value is less than final value, the contained statement isn’t executed.

If the contained statement alters the value of the control variable, your results will probably not be what you expect. After a for statement is executed, the value of the control variable value is undefined, unless execution of the for statement was interrupted by a goto from the for statement.

With these restrictions in mind, the for statement

for V := Expr1 to Expr2 do Body;

is equivalent to

begin

Temp1 := Expr1;

Temp2 := Expr2;

if Temp1 <= Temp2 then begin

V := Temp1;

Body;

while V <> Temp2 do

begin

V := Succ(V);

Body;

end; end;

end;

and the for statement

for V := Expr1 downto Expr2 do Body;

is equivalent to

begin

Temp1 := Expr1;

Temp2 := Expr2;

if Temp1 >= Temp2 then begin

V := Temp1;

Body;

while V <> Temp2 do begin

V := Pred(V);

Body;

end; end;

end;

where Temp1 and Temp2 are auxiliary variables of the host type of the variable V

and don’t occur elsewhere in the program.

These are examples of for statements:

for I := 2 to 63 do

if Data[I] > Max then

Max := Data[I]

for I := 1 to 10 do for J := 1 to 10 do begin

X := 0;

for K := 1 to 10 do

X := X + Mat1[I, K] * Mat2[K, J]; Mat[I, J] := X;

end;

for C := Red to Blue do Check(C);

With statements

The with statement is shorthand for referencing the fields of a record, and the fields and methods of an object. Within a with statement, the fields of one or more specific record variables can be referenced using their field identifiers only. The syntax of a with statement follows:

with statement

Statements - 图11

record or object variable reference

Given this type declaration,

type

TDate = record

Day : Integer;

Month: Integer;

Year : Integer;

end;

var OrderDate: TDate;

here is an example of a with statement:

with OrderDate do if Month = 12 then begin

Month := 1;

Year := Year + 1

end else

Month := Month + 1;

This is equivalent to

if OrderDate.Month = 12 then begin

OrderDate.Month := 1;

OrderDate.Year := TDate.Year + 1

end else

OrderDate.Month := TDate.Month + 1;

Within a with statement, each variable reference is first checked to see if it can be interpreted as a field of the record. If so, it’s always interpreted as such, even if a variable with the same name is also accessible. Suppose the following declarations have been made:

type

TPoint = record X, Y: Integer; end;

var

X: TPoint;

Y: Integer;

In this case, both X and Y can refer to a variable or to a field of the record. In the statement

with X do begin

X := 10;

Y := 25;

end;

the X between with and do refers to the variable of type TPoint, but in the compound statement, X and Y refer to X.X and X.Y.

The statement

with V1, V2, ... Vn do S;

is equivalent to

with V1 do with V2 do ƒ

with Vn do

S;

In both cases, if Vn is a field of both V1 and V2, it’s interpreted as V2.Vn, not V1.Vn.

If the selection of a record variable involves indexing an array or dereferencing a pointer, these actions are executed once before the component statement is executed.

C h a p t e r