The Use of Braces {} in C++ Program Source Code


If you have not yet read the page on this site about Logical Control Structures, you would benefit from doing so to learn about the concept of branching.

Braces are the characters '{' (open brace) and '}' (close brace). They are used in the C++ Programming Language to enclose a sequence of statements that are intended to be seen as a single process. In C++, statements are separated by a terminating semicolon (;). They are grouped together by enclosing them in braces. You see this in even the simplest of programs involving only a single (main) function, as in:

/* greeting.cpp */
   #include <iostream>
   using namespace std;
   
   int main (void)
   {
   cout << "Hello\n";
   return 0;
   }

Notice the use of the braces around the last two statements to group them into a single process (identified by the label "main". In some sense, you can also think of the open and close brace as representing the "start" and "end" steps in many processes. All processes are, in essense, simply a sequence of steps. So we can code them as a series of statements (each terminated with a semicolon) enclosed in braces.

When a program requires branching (or breaking out of the normal sequence of steps in an algorithm to allow an alternative process to be performed or to allow the repetition of steps), we have statements to implement those control structures. These statements begin with keywords such as if, else, do, and while. Each of these statements is designed to execute only one step on their branches. This is because of the use of semicolons to terminate each statement. If more than one statement follows any of the branching keywords, then the computer would have no way of knowing when the branch structure was complete. The semicolon after the first statement following the keyword would be interpreted as the end of the branching structure. This is why we need to enclose any groups of statements that we want executed together within braces. The flowchart below shows a typical selection structure with processes on both the true and false legs. Readers who have difficulty rendering flowcharts can read the alternative [text-based outline] for this algorithm instead. Although braces are normally not used on flowcharts, a set of red ones have been added to the flowchart below to indicate how they would be positioned with respect to the else statement used to code the false leg of the selection structure shown.

flowchart of a selection structure

Selection structures such as this one are coded in C++ using the reserved words if and else. The general syntax for coding such a structure is:

if ( condition_in_the_diamond )
   statement_to_execute_if_condition_is_true ;
else
   statement_to_execute_if_condition_is_false ;

When it is necessary to perform multiple steps on a leg (such as the two steps on the false leg in this example), we must block them inside of a { and } pair to make them a single "compound statement". Thus, the C++ code for this flowchart segment would be:

if (A>0 && A<9)
  X=X*3;
else
  {
    X=1;
    A=0;
  }

Note the use of the braces around the statements comprising the false leg in the code above. If these had been omitted, the compiler would interpret that as meaning that the "else" statement ended with the semicolon terminating the code "X=1" rather than after the one after the code "A=0". The braces group the two statements together causing them to be executed as one process, both contained on the false leg of the selection.

When the repetition structure is being used to repeat more than one step, then we need to give equal consideration to the use of braces around those steps, to prevent the semicolon that terminates the first statement following the structure's keyword (such as "do" or "while") from terminating the entire loop body prematurely. We must remember to enclose all of the statements that comprise the body of the loop within braces to make them into a single compound statement.

The flowchart below shows a typical repetition structure for a counting loop. Readers who have difficulty rendering flowcharts can read the alternative [text-based outline] for this algorithm instead. Remember that braces are normally not used on flowcharts. The set of red ones have been added to the flowchart below to indicate how they would be positioned with respect to the statements used to code the "pass" through the loop.

flowchart of a selection structure

Repetition structures using a "pretest" (leading) decision such as this one are coded in C++ using the reserved word while. The general syntax for coding such a structure is:

control_variable = start_value;
while ( condition_in_the_diamond )
   statement_to_execute_if_condition_is_true ;

The issue of concern here is that the while statement must perform two steps. So once again, because it is necessary to perform multiple steps on a leg (in this case, the true leg that constitutes the repetitive pass of the loop), we must block them inside of a { and } pair to make them a single "compound statement". The C++ code for this flowchart segment would be:

C = 1;
while (C <= 5)
  {
    cout << C << endl;
    C = C + 1;
  }

Note the use of the braces around the statements comprising the repetitive pass of the loop in the code above. If these had been omitted, the compiler would interpret that as meaning that the "while" statement ended with the semicolon terminating the cout statement, rather than after the one after the code incrementing the counter C. The braces group the two statements together causing them to be executed as one process, both contained inside the loop pass.

In summary, the general rule of thumb related to the use of braces is:

Whenever it is necessary to perform multiple steps on a leg (regardless of the type of structure), we must block those steps inside of braces to make them a single compound statement.
PATH: Instructional Server> COP 2000> Examples>