Programmer Guide > Statement Types > Blocks of Statements
  

Blocks of Statements
BEGIN
Statement1
...
Statementn
END
A block of statements is simply a group of statements that are treated as a single statement. Blocks are necessary when more than one statement is the subject of a conditional or repetitive statement, as in the FOR, WHILE, and IF statements.
In general, the format of a FOR statement with a block subject is:
FOR variable = expression, expression DO BEGIN
statement1
statement2
...
...
...
statement
n
ENDFOR
All the statements between the BEGIN and the ENDFOR are the subject of the FOR statement. The group of statements is executed as a single statement and is considered to be a compound statement. Blocks may include other blocks.
Syntactically, a block of statements is composed of one or more statements of any type, started by a BEGIN identifier and ended by an END identifier. PV-WAVE allows the use of blocks wherever a single statement is allowed. Blocks may also be nested within other blocks.
For example, the process of reversing an array in place might be written:
FOR I = 0, (N - 1) / 2 DO BEGIN
T = ARR(I)
ARR(I) = ARR(N - I - 1)
ARR(N – I – 1) = T
ENDFOR
The three statements between the BEGIN and ENDFOR are the subject of the FOR statement. Each statement is executed one time during each iteration of the loop. If the statements had not been enclosed in a block, only the first statement (T = ARR(I)) would have been executed each iteration, and the remaining two statements would have each been executed only once after the termination of the FOR statement.
To ensure proper nesting of blocks of statements, the END terminating the block may be followed by the block type as shown in End Statements. The compiler checks the end of each block, comparing it with the type of the enclosing statement.
 
note
Any block may be terminated by the generic END, although no type checking will be performed.
 
Table 4-2: End Statements  
End Statement
Usage
ENDCASE
CASE statement
ENDELSE
IF statement, ELSE clause
ENDFOR
FOR statement
ENDIF
IF statement, THEN clause
ENDREP
REPEAT statement
ENDWHILE
WHILE statement
Listings produced by the PV-WAVE compiler indent each block four spaces to the right of the previous level to improve the legibility of the program structure.

Version 2017.1
Copyright © 2019, Rogue Wave Software, Inc. All Rights Reserved.