Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the scope of the counter variable in a for loop?

Tags:

c

scope

c#

I get the following error in Visual Studio 2008:

Error 1 A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'child' scope to denote something else

This is my code:

for (int i = 0; i < 3; i++)
{
  string str = "";
}

int i = 0; // scope error
string str = ""; // no scope error

I understand that str ceases to exist once the loop terminates, but I also thought that the scope of i was confined to the for loop as well.

So i has the same scope as a variable declared just outside of the for loop?

Edit:

Just to be clear, I am using C#. I am debating removing the "C" tag. However, since the correct answer explains the difference between the two, I figure it makes sense to leave both tags.

I had an error in my code comment above:

for (int i = 0; i < 3; i++)
{
  string str = "";
}

int i = 0; // scope error
string str = ""; // also scope error,
                 // because it's equivalent to declaring
                 // string str =""; before the for loop (see below)
like image 477
JohnB Avatar asked Aug 06 '10 18:08

JohnB


People also ask

What is a counter value in a for loop?

Loop counters change with each iteration of a loop, providing a unique value for each individual iteration. The loop counter is used to decide when the loop should terminate and for the program flow to continue to the next instruction after the loop.

What is the scope of a for loop in Python?

In Python, for-loops use the scope they exist in and leave their defined loop-variable behind in the surrounding scope. This also applies if we explicitly defined the for-loop variable in the global namespace before. In this case, it will rebind the existing variable.

What is the data type of the counter used in for loop?

States simply "integer".


4 Answers

I think you're all confusing C++ and C#.

In C++, it used to be that the scope of a variable declared in a for expression was external to the block that followed it. This was changed, some time ago, so that the scope of a variable declared in a for expression was internal to the block that followed it. C# follows this later approach. But neither has anything to do with this.

What's going on here is that C# doesn't allow one scope to hide a variable with the same name in an outer scope.

So, in C++, this used to be illegal. Now it's legal.

for (int i; ; )
{
}
for (int i; ; )
{
}

And the same thing is legal in C#. There are three scopes, the outer in which 'i' is not defined, and two child scopes each of which declares its own 'i'.

But what you are doing is this:

int i;
for (int i; ; )
{
}

Here, there are two scopes. An outer which declares an 'i', and an inner which also declares an 'i'. This is legal in C++ - the outer 'i' is hidden - but it's illegal in C#, regardless of whether the inner scope is a for loop, a while loop, or whatever.

Try this:

int i;
while (true)
{
    int i;
}

It's the same problem. C# does not allow variables with the same name in nested scopes.

like image 164
Jeff Dege Avatar answered Oct 03 '22 21:10

Jeff Dege


The incrementor does not exist after the for loop.

for (int i = 0; i < 10; i++) { }
int b = i; // this complains i doesn't exist
int i = 0; // this complains i would change a child scope version because the for's {} is a child scope of current scope

The reason you can't redeclare i after the for loop is because in the IL it would actually declare it before the for loop, because declarations occur at the top of the scope.

like image 42
Jimmy Hoffa Avatar answered Oct 03 '22 22:10

Jimmy Hoffa


Yea. Syntactically: The new scope is inside the block defined by the curly strings. Functionally: There are cases in which you may want to check the final value of the loop variable (for example, if you break).

like image 42
Nicolas78 Avatar answered Oct 03 '22 22:10

Nicolas78


Just some background information: The sequence doesn't come into it. There's just the idea of scopes - the method scope and then the for loop's scope. As such 'once the loop terminates' isn't accurate.

You're posting therefore reads the same as this:

int i = 0; // scope error
string str = ""; // no scope error

for (int i = 0; i < 3; i++)
{
  string str = "";
}

I find that thinking about it like this makes the answers 'fit' in my mental model better..

like image 45
Kieren Johnstone Avatar answered Oct 03 '22 22:10

Kieren Johnstone