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;
Structured statements: Overview
混合语句是其他语句(简单语句或结构语句)的序列;混合语句用于顺序执行。混合语句用保留字begin和end括在一起,混合语句的构成语句之间用分号(;)隔开。例如:
begin
Z := X;
X := Y;
Y := Z;
end;
保留字end之前的最后一个分号(;)是可选的,因此上面的混合语句也可以写成:
begin
Z := X;
X := Y;
Y := Z
end;
混合语句在Object Pascal语法需要单个语句的上下文中是最基本的组成部分。混合语句除了出现在程序、函数、过程等块中,还出现在其他结构语句中,如条件或循环。例如:
begin
I := SomeConstant;
while I > 0 do
begin
...
I := I - 1;
end;
end;
混合语句可以只含有单个构成语句;此时保留字begin和end就象联合项中的圆括号,用于对代码消除歧义和提高可读性。甚至可以用一个空的混合语句创建一个什么也不做的块:
begin
end;