next up previous contents index
Next: Assembler statements Up: Statements Previous: Simple statements

Structured statements

Structured statements can be broken into smaller simple statements, which should be executed repeatedly, conditionally or sequentially:


Structured statements

syntdiag3360

Conditional statements come in 2 flavours :


Conditional statements

syntdiag3386

Repetitive statements come in 3 flavours:


Repetitive statements

syntdiag3420

The following sections deal with each of these statements.

Compound statements

Compound statements are a group of statements, separated by semicolons, that are surrounded by the keywords Begin and End. The Last statement doesn't need to be followed by a semicolon, although it is allowed. A compound statement is a way of grouping statements together, executing the statements sequentially. They are treated as one statement in cases where Pascal syntax expects 1 statement, such as in if ... then statements.


Compound statements

syntdiag3452

The Case statement

Free Pascal supports the case statement. Its syntax diagram is


Case statement

syntdiag3528

syntdiag3532

syntdiag3536

The constants appearing in the various case parts must be known at compile-time, and can be of the following types : enumeration types, Ordinal types (except boolean), and chars. The expression must be also of this type, or an compiler error will occur. All case constants must have the same type.

The compiler will evaluate the expression. If one of the case constants values matches the value of the expression, the statement that containing this constant is executed. After that, the program continues after the final end.

If none of the case constants match the expression value, the statement after the else keyword is executed. This can be an empty statement. If no else part is present, and no case constant matches the expression value, program flow continues after the final end.

The case statements can be compound statements (i.e. a begin..End block).

Remark: Contrary to Turbo Pascal, duplicate case labels are not allowed in Free Pascal, so the following code will generate an error when compiling:


listing3492
The compiler will generate a Duplicate case label error when compiling this, because the 3 also appears (implicitly) in the range 1..5. This is similar to Delhpi syntax.

The following are valid case statements: 'b' : WriteLn ('B pressed');
listing3496
Or 'b' : WriteLn ('B pressed');
listing3498

listing3500

The If..then..else statement

The If .. then .. else.. protottype syntax is


If then statements

syntdiag3604

The expression between the if and then keywords must have a boolean return type. If the expression evaluates to True then the statement followingthen is executed. If the expression evaluates to False, then the statement following else is executed, if it is present.

Be aware of the fact that the boolean expression will be short-cut evaluated. (Meaning that the evaluation will be stopped at the point where the outcome is known with certainty)

Also, before the else keyword, no semicolon (;) is allowed, but all statements can be compound statements.

In nested If.. then .. else constructs, some ambiguity may araise as to which else statement paits with which if statement. The rule is that the else keyword matches the first if keyword not already matched by an else keyword.

For example:
listing3565
Despite it's appreance, the statement is syntactically equivalent to
listing3567
and not to
listing3569
If it is this latter construct you want, you must explicitly put the begin and end keywords. When in doubt, add them, they don't hurt.

The following is a valid statement:
listing3574

The For..to/downto..do statement

Free Pascal supports the For loop construction. A for loop is used in case one wants to calculated something a fixed number of times. The prototype syntax is as follows:


For statement

syntdiag3690

syntdiag3694

syntdiag3698

syntdiag3702

Statement can be a compound statement. When this statement is encountered, the control variable is initialized with the initial value, and is compared with the final value. What happens next depends on whether to or downto is used:

  1. In the case To is used, if the initial value larger than the final value then Statement will never be executed.
  2. In the case DownTo is used, if the initial value larger than the final value then Statement will never be executed.
After this check, the statement after Do is executed. After the execution of the statement, the control variable is increased or decreased with 1, depending on whether To or Downto is used.

The control variable must be an ordinal type, no other types can be used as counters in a loop.

Remark: Contrary to ANSI pascal specifications, Free Pascal first initializes the counter variable, and only then calculates the upper bound.

The following are valid loops:
listing3656

The Repeat..until statement

The repeat statement is used to execute a statement until a certain condition is reached. The statement will be executed at least once. The prototype syntax of the Repeat..until statement is


Repeat statement

syntdiag3746

This will execute the statements between repeat and until up to the moment when Expression evaluates to True. Since the expression is evaluated after the execution of the statements, they are executed at least once.

Be aware of the fact that the boolean expression Expression will be short-cut evaluated. (Meaning that the evaluation will be stopped at the point where the outcome is known with certainty)

The following are valid repeat statements
listing3723

The While..do statement

A while statement is used to execute a statement as long as a certain condition holds. This may imply that the statement is never executed. The prototype syntax of the While..do statement is


While statements

syntdiag3808

This will execute Statement as long as Expression evaluates to True. Since Expression is evaluated before the execution of Statement, it is possible that Statement isn't executed at all. Statement can be a compound statement.

Be aware of the fact that the boolean expression Expression will be short-cut evaluated. (Meaning that the evaluation will be stopped at the point where the outcome is known with certainty)

The following are valid while statements:
listing3776
They correspond to the example loops for the repeat statements.

The With statement

 

The with statement serves to access the elements of a recordgif or object or class, without having to specify the name of the each time.

The syntax for a with statement is


With statement

syntdiag3860

The variable reference must be a variable of a record, object or class type. In the with statement, any variable reference, or method reference is checked to see if it is a field or method of the record or object or class. If so, then that field is accessed, or that method is called.

Given the declaration:
listing3828
The following statements are completely equivalent:
listing3830
and
listing3832

The statement
listing3834
is equivalent to
listing3836
This also is a clear example of the fact that the variables are tried last to first, i.e., when the compiler encounters a variable reference, it will first check if it is a field or method of the last variable. If not, then it will check the last-but-one, and so on.

The following example shows this;
listing3839
The output of this program is

2 2
Showing thus that the X,Y in the WriteLn statement match the T record variable.

Exception Statements

As of version 0.99.7, Free Pascal supports exceptions. Exceptions provide a convenient way to program error and error-recovery mechanisms, and are closely related to classes.

Exception support is explained in chapter (gif)


next up previous contents index
Next: Assembler statements Up: Statements Previous: Simple statements

Michael Van Canneyt
Fri Sep 25 09:15:40 MEST 1998