High Level Language Programming


 

Compound Statements

 

A compound statement in C++ is a sequence of statements that are executed as a unit. It looks like this in C++:

{
    <statement_1>;
    <statement_2>;
    :
    :
    <statement_k>;
}

Example from Finding Largest algorithm:

if (A[i] > largest_so_far)
{
    largest_so_far = A[i];
    location = i;

}

[Prev][TOC][Next]


Personally: I prefer ALWAYS to use block statements in if and while statements for clarity, even if there is only one statement involved. Some languages permit the omission of the block identifiers. Moreover I like to clearly identify complex block endings by a comment.

if (x > y) {y = y + 1} // end if;

while (y >= 3) {y = y - 3} // end while;


CS1104 Main Page
Last Updated 01/05/2000
© L.Heath, 2000