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

### 38 comments to 4.1 — Blocks (compound statements)

Hi!

In the third program , what result would we get if we enter a negative integer ? Who don’t we have an else statement for negative integers ? Also , isn’t it necessary that every if statement needs to be followed by an else statement ?

I think the reason is because as we have taken data type as int, which is already unsigned and accepts only positive integral values. But if we had already specified the data type earlier , why did we need an If statement for accepting only positive numbers? Even if we did not have an if statement for accepting positive integral values , and the user entered a negative number , wouldn’t we get an error in either case ?

• nascardriver

> what result would we get if we enter a negative integer
No output

> isn’t it necessary that every if statement needs to be followed by an else statement
No, you don't need an else-statement

> we have taken data type as int, which is already unsigned
An int is signed by default

Run the program yourself, play around with some numbers and place breakpoints to get a better understanding of what's happening.

@nascardriver okay thank you !

• Ali Dahud

Hi Alex!

could you go through my code and correct the mistakes i made?
oh and please explain this piece:
tempNum = larger;
larger = smaller;
smaller = tempNum;

• nascardriver

Hi Ali!

Try correcting it and post your progress, if you have any questions feel free to ask.

It's swapping the values of @larger and @smaller. Imagine you have three baskets in front of you, one contains a red apple, one contains a green apple and one is empty.
You want the red apple to be in the green apple's basket and the green apple to be in the red apple's basket.
You are only allowed to hold one apple at a time.

PS: Please post your code in the corresponding lesson next time, this quiz is in lesson 4.1a

• Ali Dahud

isn't that some kind of sorting?

• nascardriver

No it's not, you only want two elements to change places.

EDIT: Well, technically, it might be sorting since you now have two elements in a specific order, but I would call it sorting when it's just two elements and not even a list/array.

• Ali Dahud

i remember the bubble sorting used a method like this. or at least some kind of like this

• Alex

Yes, many sorting methods work by swapping elements until the list of items is sorted. Bubble sort swaps adjacent elements. Selection sort swaps once per iteration.

Generally, when we transpose the value of two elements, we call it a swap. We call it a sort when we put all the elements in a list or array into some sorted order.

• Ali Dahud

Hi Alex, how many tutorials do you recommeend me on reading daily?

• Alex

I don't have a recommendation on this.

It really depends entirely on your learning capability, motivation, and how much time you want to spend experimenting with each topic on your own.

• i have the time to read as much as i want and my learning capability is pretty big i guess. with each topic i guess 20 minutes-1 hour or 1,5 hour depending if it has coding quizzes in it. so how  much should i read? oh and i lack motivation i guess so how should i motivate myself? for example i start watching videos, news while im supposed to learn.

• 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! : ))

• Alex

Thanks, fixed!

• 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

• Alex

No, why would it? 0 * 2 = 0.

• Nick

Oh I see now it is division by zero.

• Jack Oz

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

Best regards,
Jack

• Alex

I've used sockets in the distant past but I've probably forgotten more than I know about them. 🙁

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

• Alex

Good catch. I updated the conditional on the first if statement to handle this properly.

• 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):

• Alex

Thanks, fixed!

• 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!
🙂

• Alex

Yep, fixed. Thanks for pointing that out.

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

• Alex

Good call. I used to talk about local variables here, but moved it into section 1.

• Martin

Blockception.

• Ignacio

best comment so far!!! lol

• Pan

Great!

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

• As you note, using statements also use normal scoping rules -- if used inside a block, it applies to the block and all subblocks.

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