Search

6.1 — Compound statements (blocks)

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:

Blocks inside other blocks

Although functions can’t be nested inside other functions, blocks can be nested inside other blocks:

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:

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:

The nesting level (also called the nesting depth) of a function is the maximum number of nested 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 in this program you can never be inside more than 3 blocks at any point.

The C++ standard says that C++ compilers should support 256 levels of nesting -- however not all do (e.g. as of the time of writing, Visual Studio supports somewhere between 100 and 110).

Despite what C++ technically supports, 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 hard to read and are 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 nested levels, consider refactoring your function into sub-functions.


6.2 -- User-defined namespaces and the scope resolution operator
Index
O.4 -- Converting between binary and decimal

84 comments to 6.1 — Compound statements (blocks)

  • how do I keep on updating the turtle in this program so I don't have to keep on making a new turtle every time the user enters a new number?

  • Parsa

    I think the last point needs to be rephrased as others have mentioned. I personally got confused and thought that the nesting capacity is limited to 3.

  • SuperNoob

    "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."

    I thought it's not possible to have more than 3 blocks in any functions at any point. But techincally it's actually possible. Maybe you mean it's prohibited for readability purpose. So I guess, if the sentence was a bit clear, it would be easier to understand for everyone.

    I would say:
    "In the above function, there are 4 blocks, but the nesting level is 3 since having more than 3 blocks decreases readability and is not suggested."

    Example code of 5 nesting levels (not suggested):

    • ewrilan

      you can never be inside more than 4 blocks at any point of the code

    • Blar

      I think what is meant there is that there are indeed four blocks in total: main function (1) first if (2) second if (3) else (4), but there is a depth of three because indeed in THAT function you can't go deeper. This because two of them are in the same depth. And then he advices us not to go deeper than that in our own code.

  • Chayim

    "since you can never be inside more than 3 blocks at any point" Why?

    • vitunpassi

      It just means that when running that particular program you can never be in more than 3 blocks at any point because there are no blocks nested to be deeper than that.

      • darrylkid

        The author did not intend to say "you can never be inside more than 3 blocks at any point" in a general way. This only applies to the specific example.

        You can theoretically have as many nested blocks as you'd like. However, any more than 3 nested blocks can result in difficult to read and maintain code:

  • Chayim

    When "value" is -4 how does it become 4 by -value, and how does -value work?

  • Suyash Mallik

    Why can blocks only be inside functions?
    I tried, and this does not work:

    • Chayim

      Nothing makes sense in this code not only your question about blocks.

      • Suyash Mallik

        if you could make blocks outside functions, then this code would compile and execute without any problems. There would be no duplicate initialisation error, as is the case in this sample. Hence, I deduced that blocks cannot be made outside of functions.

    • Waldo Lemmer

      1. x from line 2 is in scope from line 2 - 8. When you get to line 4, x from line 2 is still in scope, so you can't initialize x again.

      -

      2. Global variables (variables outside of functions) that aren't constant are bad.

      -

      I hope this is easy to understand, I'm not great at explaining things. I'd take a look at '2.4 — Introduction to local scope' and '2.9 -- Naming collisions and an introduction to namespaces' if I were you.

Leave a Comment

Put all code inside code tags: [code]your code here[/code]