 [NEXT] [PREV]    HANDBOOK / CASE STUDY / The Source Language /

# Statements

Gentle
Applications
Concepts
Examples
Handbook
Support

Statements denote algorithmic actions, and are said to be executable.

```
Stat ::= AssignStat | CondStat | LoopStat | ProcStat
```

### Statement sequences

A statement sequence specifies that its component statements are to be executed in the same sequence as they are written.

```
StatSeq ::= Stat { ';' Stat }
```

### Assignment Statements

The assignment statement serves to replace the current value of a variable by a new value specified as an expression.

```
AssignStat ::= Var ':=' Expr
```
Examples:

```
i := i+1
r := r*3.141592
b := i<1
a := r
```
The variable and the expression must be of identical type, with the following exception being permitted: The type of the variable is REAL, and the type of the expression is INTEGER. In any case, the variable must be of a simple type.

### Procedure Statements

A procedure statement serves to execute the procedure denoted by the procedure identifier. The procedure statement may contain a list of actual parameters which are substituted in place of their corresponding formal parameters defined in the procedure declaration. The correspondence is established by the positions of the parameters in the lists of actual and formal parameters respectively. There exist two kinds of parameters: value parameters and variable parameters.

In the case of a value parameter, the actual parameter must be an expression (of which a variable is a simple case). The corresponding formal parameter represents a local variable of the called procedure, and the current value of the expression is initially assigned to this variable. Value parameters must have a simple type. In the case of a variable parameter, the actual parameter must be a variable of the same type, and the corresponding formal parameter represents this actual variable during the entire execution of the procedure. If this variable is a component of an array, its index is evaluated when the procedure is called. A variable parameter must be used whenever the parameter represents a result of the procedure.

```
ProcStat ::= Id [ '(' Expr { ',' Expr } ')' ]
```
Examples:

```
next   Transpose(a,m,n)
```

### Conditional Statements

The if statement specifies that a statement be executed only if a certain condition (Boolean expression) is true. If it is false, the statement following the delimiter ELSE is to be executed.

```
CondStat ::= 'IF' Expr 'THEN' StatSeq
'ELSE' StatSeq 'END'
```
Examples:

```
IF i < 0 THEN i := 1 ELSE i := 2 END
```
The expression between the delimiters IF and THEN must be of type Boolean.

### Repetitive Statements

The while statement specifies that a certain statement is to be executed repeatedly.

```
LoopStat ::= 'WHILE' Expr 'DO' StatSeq 'END'
```
The expression controlling repetition must be of type Boolean. The statement is repeatedly executed as long as the expression is true. If it evaluates to false at the beginning, the statement is not executed at all. The while statement

```
WHILE b DO s END
```
is equivalent to

```
IF b
THEN s; WHILE b DO s END
ELSE (* nothing *)
END
```
Examples:

```
WHILE a [i] < r DO i := i + 1 END

WHILE i < n DO
r := 2 * r;
i := i + 1
END
```

### Declarations

Procedure declarations serve to define parts of programs and to associate identifiers with them so that they can be activated by procedure statements.

```
Block    ::= 'DECLARE' Decl { ';' Decl }
'BEGIN' StatSeq 'END'
Decl     ::= VarDecl | ProcDecl
```
The procedure heading specifies the identifier naming the procedure and the formal parameter identifiers (if any). The parameters are either value or variable parameters.

```
[ '(' Formal { ';' Formal } ')' ]
Formal   ::= [ 'VAR' ] Id ':' Type
```
If a formal starts with the delimiter VAR it specifies a variable parameter, otherwise a value parameter.

The statement sequence of the block specifies the algorithmic actions to be executed upon an activation of the procedure by a procedure statement.

All identifiers introduced in the formal parameter part of the procedure heading and in the declaration part of the associated block are local to the procedure declaration which is called the scope of these identifiers. They are not known outside their scope. In the case of local variables, their values are undefined at the beginning of the statement part.

The use of the procedure identifier in a procedure statement within its declaration implies recursive execution of the procedure.

Examples of procedure declarations:

```
DECLARE
j: INTEGER;
BEGIN
i := 0;
WHILE NOT (0 < i) DO READ (i) END
END

PROCEDURE Sort
(VAR a: ARRAY [1..10] OF REAL; n: INTEGER);
DECLARE
i: INTEGER; j: INTEGER; k: INTEGER; h: REAL;
BEGIN
i := 1;
WHILE i < n DO
(* a , ... , a [i] is sorted *)
j := i; k := i;
WHILE j < n DO
(* a [k] = min {a [i], ... , a [j]} *)
j := j + 1;
IF a [j] < a [k] THEN k := j ELSE k := k END
END;
h := a [i]; a [i] := a [k]; a [k] := h;
i := i + 1
END
END
```

[NEXT] [PREV]