3C. Compound Statements

In several places, the syntax of C++ requires a single statement. What if you want to write a sequence of statements where one statement is required? There is a syntactic trick for that. Any sequence of statements enclosed in curly braces is a single compound statement. For example,

  {
    printf("I am happy to meet you.\n");
    printf("My name is Kim.\n");
  }
looks to you like a sequence of two statements, but is treated as one (compound) statement by a C++ compiler.

Indentation standard for compound statements

For this course, follow these standards for compound statements:

  1. Always write braces so that each right brace is directly below the matching left brace, as shown above.

  2. There must be no characters on or to the left of a line segment that connects matching left and right braces, as shown above.

  3. Indent the statements that belong to a compound statement from 2 to 4 spaces more than the indentation of the braces that surround them, as shown above.

    Use a consistent indentation amount. If you decide to indent 2 spaces per level, always indent 2 spaces per level in that file.

According to rules (2) and (3),

  {
  printf("I am happy to meet you.\n");
  printf("My name is Kim.\n");
  }
is not correctly indented, and
  {
printf("I am happy to meet you.\n");
printf("My name is Kim.\n");
  }
is very poorly indented.

Indentation is neither complicated nor difficult. Follow examples. You can expect to lose points for each poorly indented line.

If you submit a program that is very poorly indented throughout, it is very difficult for me to read and you can expect to lose 50% of the available points for doing that.