# for Loop Variable Scope

Try to run following program in your IDE.











 



using System;

class MainClass
{
  public static void Main(string[] args)
  {
    for (int i = 0; i < 10; i++)
    {
      Console.WriteLine(i);
    }
    Console.WriteLine(i);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

What is the output of this program?

TIP

You can run the code in your browser here.

This program will not run at all. Do you know why?

Only the for loop header and body know about the variable i. For rest of the program, variable i does not exist at all.

Last Updated: Apr 22, 2020, 1:39 PM