Compound statements

Navigation:  Les scripts > Les autres scripts > Statements >

Compound statements

Previous pageReturn to chapter overviewNext page

A compound statement is a sequence of other (simple or structured) statements to be executed in the order in which they are written. The compound statement is bracketed by the reserved words begin and end, and its constituent statements are separated by semicolons. For example:

 

begin

 

 Z := X;

 X := Y;

 Y := Z;

end;

 

The last semicolon before end is optional. So we could have written this as

 

begin

 

 Z := X;

 X := Y;

 Y := Z

end;

 

Compound statements are essential in contexts where Object Pascal syntax requires a single statement. In addition to program, function, and procedure blocks, they occur within other structured statements, such as conditionals or loops. For example:

 

begin

 

 I := SomeConstant;

 while I > 0 do

 begin

   ...

   I := I - 1;

end;

end;

 

You can write a compound statement that contains only a single constituent statement; like parentheses in a complex term, begin and end sometimes serve to disambiguate and to improve readability. You can also use an empty compound statement to create a block that does nothing:

 

begin

 

end;