[ACCEPTED]-How do you use an existing variable within one or more for loops?-.net-3.5

Accepted answer
Score: 10

The issue is one of scoping. Read here for some details 4 on how variable scoping works in C#.

If a 3 variable is declared outside a loop, you can't 2 re-declare it inside:

BAD:

int c = 0;
for(int c = 0; c < list.Count; c++) // Error!
{

}

OK:

Declared outside, used inside:

int c = 0;
for(c = 0; c < list1.Count; c++)
{
}

for(c = 0; c < list2.Count; c++)
{
}

Declared inside two 1 loops:

for(int c = 0; c < list1.Count; c++)
{
}

for(int c = 0; c < list2.Count; c++)
{
}
Score: 3

You can either do

  int i;
  for (i = 0; i < 3; i++)
    foo(i);
  for (i = 0; i < 5; i++)
    bar(i);

or

 for (int i = 0; i < 3; i++)
    foo(i);
 for (int i = 0; i < 5; i++)
    bar(i);

but not

int i;
for (int i = 0; i < 3; i++) //error
  foo(i);
for (int i = 0; i < 5; i++)
  bar(i);

0

Score: 1

I think they mean the following.

You can 8 do this. What happens hear is that you've 7 declared a variable outside of the loop 6 and are using it. The problem though is 5 that you may overwrite an existing value 4 which you need to use somewhere else.

int i = 0;

for (i = 0; i < 100; i++)
{
    // Do something
}

What 3 you really can't do is this. Here you are 2 re-using the variable from the outer for in 1 the inner for.

for (int i = 0; i < 100; i++)
{
    for (i = 0; i < 100; i++)
    {
        // Do something
    }
}
Score: 1

The statement is indeed confusing, if I 6 understand it correctly, according to the 5 text I should not be able to do this:

int i;
for (i = 1; i < 10; i++) { }

for (i = 0; i < 20; i++) { }

But 4 I can, and this is clearly valid. I think 3 what the text means is "you can't re-declare it in 2 either one" instead of "you can't use it 1 in either one".

Score: 1

The concept here is Scope. A variable is declared 10 within some scope and cannot be accessed 9 outside of it. This helps define the lifetime 8 of a variable as well as control access 7 to it. A variable can be declared within 6 a class, a method, or a conditional scope 5 within a method such as within an if statement 4 or a for loop.

One easy way to think of scope 3 is that you can access a variable
within 2 the pair of curly braces { ... } it lives under.

Here's 1 an example:

    public class TestClass
    {
        int p;  // p's is in the 'TestClass' scope

        public void TestFunction1()
        {

            Console.WriteLine(p);   // OK, p in class scope

            //  a lives in the 'TestFunction' scope
            int a = 1; // Declared outside of any loop.

            for (int i = 0; i < 10; i++)
            {
                //  i lives in the scope of this for loop
                Console.WriteLine(i);
                //  a is still accessible since this for loop is inside TestFunction1
                Console.WriteLine(a);
            }
            Console.WriteLine(a); // OK, in scope
            //Console.WriteLine(i); // Error, i out of scope

            //  j also lives in the TestFunction1 scope
            int j = 0;
            for (j = 0; j < 20; j++)
            {
                //  j still accessible within the loop since the loop is within TestFunction1
                Console.WriteLine(j);
            }

            Console.WriteLine(j); // Ok, j still in scope (outside of loop)
        }

        public void TestFunction2()
        {
            Console.WriteLine(p);   // Ok, TestFunction2 is in the TestClass scope like TestFunction1
            //Console.WriteLine(a);   // Error, a lives in TestFunction1
            //Console.WriteLine(i);   // Error, allright now we're just getting silly ; )
        }
    }
Score: 1

You can use an existing variable as the 5 starting point of a for loop.

I just started 4 learning C# 4 weeks ago, so be weary.. but 3 the syntax is:

int x = 10;

for (x = x ; x < 15; x++) // x starts off as whatever defined above (15)
    {
        Console.WriteLine("x is: {0}", x);
    }
    // After for is done executing, x will = 15

Then for whatever reason, you 2 can continue doing some other logic (When 1 X < 30, something else happens) ex)

for (x = x ; x < 30; x++)
    {
        // Do stuff here
    }
Score: 1

x=x works to re-use the variable keeping 3 its value from one loop to another but it 2 gives you a warning. you could prefer this 1 syntax

for (x+=0; x>10; x++) ;
Score: 0

Another syntax to consider, if you want 3 to use a variable from the outside, without 2 having to re-assign or re-initialize the 1 variable, you can omit the clause before the first semi-colon.

int c = 0;
for(; c < list1.Count; c++)
{
}

// c will start at the ending value list1.Count from the loop above
for(; c > 0; c--)
{
}

More Related questions