0

I am trying to understand the scope of the for loop by testing out the following three examples:

Example 1:

int a=15; for(a=1;a<3;a++) { cout<<a<<endl; } cout<<a<<endl; 

Here the console displays 1 2 3. However, I would have thought that it should display 1 2 15, since normally it seems that the scope of the variable a inside the for loop remains inside the for loop.

Example 2:

for (int a=1; a<3; a++) { cout<<a<<endl; } cout<<a<<endl; 

In this example the compiler does not recognize the variable a after the for loop - as expected. So how was it able to be recognised it in Example 1?

Example 3:

int a=15; for(int a=1;a<3;a++) { cout<<a<<endl; } cout<<a<<endl; 

In this case the console displays 1 2 15. What is the difference between this and Example 1?

Thank you in advance for your help.

3 Answers 3

3

Analyzing your example 1:

int a=15; 

This line declares a variable a. This variable can be read from or written to. It is valid until the end of the block where it is declared. Here, there is no such block visible; in a real program, blocks are code between { and }.

for(a=1;a<3;a++) { cout<<a<<endl; } 

In these lines, you modify the variable from the first line. It is still the same variable, it’s only the value of the variable that changes. Imagine a variable like a sheet of paper. The sheet stays the same for the whole time, it’s just that you write different numbers on it (with a pencil) and erase them (with a rubber gum).

cout<<a<<endl; 

Since there was no corresponding }, the variable a is still visible here.


Example 3 differs in one word from example 1: the int in the for loop. This int is a new variable declaration. Now you have two variables with the name a. Using the analogy of the paper sheets, you just grabbed another sheet of paper and laid it above your first sheet. The first sheet thereby becomes invisible.

At the end of the for loop, at the closing }, you take the upper sheet (which at that point has the number 3 written on it) and throw it in the waste bin. This makes the outer sheet appear again, which still has the value 15 written on it.

Sign up to request clarification or add additional context in comments.

Comments

2

In example 1, you declare a outside the scope of the for loop, and then do not re-declare it (i.e. int a). For this reason, the variable a both inside and outside the loop is the same. In the first iteration, it is 1, then gets incremented to 2. It still meets the for loop criteria, so it executes again, and then increments to 3. Now, a doesn't meet the for loop criteria, so it leaves the loop with a value of 3. Finally, it is printed again, and its value is 3.

The reason that a is not recognized outside the loop in example 2 is that a was never declared outside the loop. It is a variable local to the for loop, and therefore does not exist in any scope outside that loop.

In example 3, you declare a a second time, when in the for loop declaration you say int a = 1 rather than just a = 1. This means that there is an a local to the for loop, and an a outside this loop, which maintains its value from before the loop executing, since they are two different variables - they have the same name but different scopes.

2 Comments

Thank you! However, why is it that, for Example 1, the modification of 'a' inside the for loop is carried over outside the for loop? @FCo
This is because a inside the loop is the exact same variable as a outside the loop. Any modifications to the variable value still hold. They are the same since a was not re-declared in the for loop declaration.
1

Solution For Ex 1: In the first example you have declared var 'a' at the first line i.e before for loop is executed,initially the value is 15 but then in for loop you initialize it to 1,for loop iterates two times thus resulting in 1 and 2.In the third iteration 'a' is incremented thus now a=3 but condition fails as 'a' is not less than 3.So it prints 3 at the end.

Solution For Ex 2: As variable is declared in for loop its scope lies within the for loop itself thus the variable is not accessible outside for loop.Thus Error

Solution For Ex 3: The difference between the Ex1 and Ex3 is in Example 3 we have two copies of 'a',one is outside for loop whose value is 15 and one inside the for loop whose scope is within the for loop.So inside for loop var 'a' results into 1 2 as output and the outer var a results into 15 at the end.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.