Compound statements
A compound statement (also called a block, or block of statements) 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, and the statements to be executed are 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> int add(int x, int y) { // start of block return x + y; } // end of block (no semicolon) int main() { // start of block // multiple statements int value { 3 }; // not a block, this is just part of the initialization syntax std::cout << add(value, 4); return 0; } // end of block (no semicolon) |
Using blocks to execute multiple statements conditionally
By default, the statement conditionally executed by an if or else must be a single statement. However, by using a compound statement, we can instead conditionally execute multiple statements. To do so, we simply replace the single statement after the if and/or else with a compound statement.
Here’s an example of an if-else that uses a compound statement for both the if and else statements:
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() { std::cout << "Enter an integer: "; int value {}; std::cin >> value; if (value >= 0) { // both of the following statements will execute if the condition is true std::cout << value << " is a positive integer (or zero)\n"; std::cout << "Double this number is " << value * 2 << '\n'; } else { // both of the following statements will execute if the condition is false std::cout << value << " is a negative integer\n"; std::cout << "The positive of this number is " << -value << '\n'; } return 0; } |
If the users 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
Nested blocks
Although functions can’t be nested inside other functions, blocks can be nested inside other blocks (as shown in the prior example).
The outermost block (typically the block defining the function body) is called the outer block. The block inside is typically called an inner block or nested block.
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 |
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 maximum number of nested levels in a function is called the nesting depth of a function. For example, in the above function, there are 4 blocks total, but the nesting depth is 3 (because you can never go more than 3 blocks deep within the function).
C++ supports a nesting depth of 256 levels, which is far more than you’ll ever need. In fact, it’s a good idea to keep your nesting depth 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.
Best practice
Keep the nesting depth of your functions to 3 or less (unless you have a compelling reason to do otherwise). If your function has a need for more, consider refactoring.
![]() |
![]() |
![]() |