Search

4.1 — Blocks (compound statements)

Blocks (compound statements)

A block of statements, also called a compound statement, is a group of 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 any place where 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:

Blocks can be nested inside of other blocks. As you have seen, the if statement executes a single statement if the condition is true. However, because blocks can be used anywhere a single statement can, we can instead use a nested block of statements to make the if statement execute multiple statements if the condition is true!

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

It is even possible to put blocks inside of blocks inside of blocks:

There is no practical limit to how many nested blocks you can have. However, it is generally a good idea to try to keep the number of nested blocks to at most 3 (maybe 4) blocks deep. If your function has a need for more, it’s probably time to break your function into multiple smaller functions!

Summary

Blocks allow multiple statements to be used wherever a single statement can normally be used. They are extremely useful when you need a set of statements to execute together.

4.1a -- Local variables, scope, and duration
Index
3.x -- Chapter 3 comprehensive quiz

25 comments to 4.1 — Blocks (compound statements)

  • Amir

    Hi Alex. The Output of the first program has a mistake!

    This is the code :

    below it we have this output :

       Enter an integer: 3
       3 is a positive integer
       Double this number is 6

    I guess it should be like this :

       Enter an integer: 3
       3 is a positive integer (or zero)
       Double this number is 6

    Thank You! : ))

  • Nick

    Thank you so much for making all these.

    On the 2ed code example if you put in 0 won’t it fail when it gets to

  • Jack Oz

    Alex thank you so much your time, you help many people. Are you familiar with socket programming in C++?

    Best regards,
    Jack

  • Rajeg

    Hey Alex,

    maybe you should add a special case for 0 on your second program, now it says that 0 is a negative integer.

  • Jim

    Alex,

    On line 18 of the second program you have:
    cout << "The positive of this number is " << -value << endl;.

    Since you entered -4 for the input, how does the program know to print out 4 (positive)? when no math is ever done?

    Does this somehow say take value and multiply it times -1 ?
    In math we learned (-4 * -1)= 4.

  • Mr D

    In the last code example above, you’re missing a semi-colon at the end of the line(4):

  • techsavvy....aye

    A minor note in the last program
    it should be just value instead of nValue shouldn’t it?
    In block inside of block inside of block!
       🙂

  • Todd

    Typo.

    I would change the title,

    "4.1 — Blocks (compound statements) and local variables"

    to the title referenced on your main page

    "4.1 — Blocks (compound statements)"

    since you don’t talk about local variables here.

  • Zidane

    The best tutorials I’ve ever seen 😛

  • posentel

    It may be helpful to point out that since a block replaces a single statement (that ends with a semicolon), the block supercedes the use of a semicolon. No semicolon is necessary to end a block.

  • Fluke

    Now i see a benefit from declare_variable_when_needed instead of all at the top of the function.
    Cheers 🙂

  • davidv

    Hi Alex,

    Fabulous work, first of all.

    I have a question: how does “using namespace std” work with the nesting? It seems that once written in a block, it is valid for the sub-blocks as well. If it is so, wouldn’t it be easier to simply declare it at the beginning rather than several times inside functions?
    Thanks.

  • learning c++

    Hey Alex when you read this would like to suggest adding something like a review at the end of every section just quickly reminding people like for example on the variables section 1 it would say all the variable types with quick meanings and examples maybe.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter