A compound statement (also called a block, or block statement) is a group of zero or more statements that is treated by the compiler as if it were a single statement.
Blocks begin with a {
symbol, end with a }
symbol, with the statements to be executed being placed in between. Blocks can be used anywhere a single statement is allowed. No semicolon is needed at the end of a block.
You have already seen an example of blocks when writing functions, as the function body is a block:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
int add(int x, int y) { // start block return x + y; } // end block (no semicolon) int main() { // start block // multiple statements int value {}; // this is initialization, not a block add(3, 4); return 0; } // end block (no semicolon) |
Blocks inside other blocks
Although functions can’t be nested inside other functions, blocks can be nested inside other blocks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
int add(int x, int y) { // block return x + y; } // end block int main() { // outer block // multiple statements int value {}; { // inner/nested block add(3, 4); } // end inner/nested block return 0; } // end outer block |
When blocks are nested, the enclosing block is typically called the outer block and the enclosed block is called the inner block or nested block.
Using blocks to execute multiple statements conditionally
One of the most common use cases for blocks is in conjunction with if statements
. By default, an if statement
executes a single statement if the condition evaluates to true
. However, we can replace this single statement with a block of statements if we want multiple statements to execute when the condition evaluates to true
.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> int main() { // start of outer block std::cout << "Enter an integer: "; int value {}; std::cin >> value; if (value >= 0) { // start of nested block std::cout << value << " is a positive integer (or zero)\n"; std::cout << "Double this number is " << value * 2 << '\n'; } // end of nested block else { // start of another nested block std::cout << value << " is a negative integer\n"; std::cout << "The positive of this number is " << -value << '\n'; } // end of another nested block return 0; } // end of outer block |
If the user enters the number 3, this program prints:
Enter an integer: 3 3 is a positive integer (or zero) Double this number is 6
If the user enters the number -4, this program prints:
Enter an integer: -4 -4 is a negative integer The positive of this number is 4
Block nesting levels
It is even possible to put blocks inside of blocks inside of blocks:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
int main() { // nesting level 1 std::cout << "Enter an integer: "; int value {}; std::cin >> value; if (value > 0) { // nesting level 2 if ((value % 2) == 0) { // nesting level 3 std::cout << value << " is positive and even\n"; } else { // also nesting level 3 std::cout << value << " is positive and odd\n"; } } return 0; } |
The nesting level (also called the nesting depth) of a function is the maximum number of blocks you can be inside at any point in the function (including the outer block). In the above function, there are 4 blocks, but the nesting level is 3 since you can never be inside more than 3 blocks at any point.
It’s a good idea to keep your nesting level to 3 or less. Just as overly-long functions are good candidates for refactoring (breaking into smaller functions), overly-nested functions are also good candidates for refactoring (with the most-nested blocks becoming separate functions).
Best practice
Keep the nesting level of your functions to 3 or less. If your function has a need for more, consider refactoring.
![]() |
![]() |
![]() |