A compound operator is a programming language construct consisting of several commands (operators) of a programming language, but participating in the program as a single operator.
Definition
The concept of a compound operator appeared in the Algol programming language . The structural operators introduced in this language ( branching , loop ) were organized in such a way that only one language command could be used in them. In order not to limit the programmer , the concept of a compound operator was introduced into the language: any set of operators located between the begin and end keywords, from the point of view of the translator, became one command (operator) and could be used anywhere in the program where one operator should be used.
Use
The notion of compound operator was inherited by Pascal and many other programming languages based on Algol. In Pascal, the conditional if statement, while and for loops require the same statement as branches and body, therefore, if necessary, place several commands in the branches of the conditional statement or the body of the loop, using compound statements:
if condition then
begin {start of compound statement}
... {several statements}
end {end of compound statement}
else
begin {start of compound statement}
... {several statements}
end ; {end of compound statement}
while condition do
begin {start of compound statement}
... {several statements}
end ; {end of compound statement}
for variable : = value 1 to value 2 do
begin {start of compound statement}
... {several statements}
end ; {end of compound statement}
In C, the compound operator is limited to curly braces, which shortens the program text, but does not fundamentally differ from Pascal and Algol:
if ( condition )
{ // start of a compound statement
... // several operators
} // end of compound statement
else
{ // start of a compound statement
... // several operators
} // end of compound statement
while ( condition )
{ // start of a compound statement
... // several operators
} // end of compound statement
do
{ // start of a compound statement
... // several operators
} // end of compound statement
while ( condition );
for ( initialization ; condition ; operator )
{ // start of a compound statement
... // several operators
} // end of compound statement
Abandoning compound statements
A number of programming languages have abandoned the use of compound statements. In them, in any syntactic constructions, it is possible to use several statements written sequentially; as a result, there is no need for specially organized compound statements. Nevertheless, even in such languages, some elements, for example, bodies of procedures and functions, are actually compound operators, although they are not called so.