Skip to content
Theory · Conditions and loops · lesson 7 of 13

The for loop

In the previous lesson, the while loop repeated its body while a condition was true. The counter lived on its own: it was declared before the loop and changed in the body. When the number of repetitions is known in advance, all of that is gathered into one line—the header of a for loop.

Back to the runner. This time the coach is precise and asks for a hundred laps. The runner steps up to the start line, checks the count under the arch before each lap, and makes a mark in a notebook after the lap. A for loop works the same way. Let's add up the numbers from 1 to 100:

C#
int sum = 0;

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

Console.WriteLine(sum); // 5050

The three header parts

The parentheses after for hold three parts separated by semicolons:

  1. int i = 1 runs once, before the loop starts. The runner steps up to the start line.
  2. The condition i <= 100 is checked before every iteration. If it is true, the body in braces runs. This is the check under the arch before a lap.
  3. The step i++ runs after the body. This is the mark in the notebook.

Now let's go through the program:

  1. int sum = 0; sets up the sum.
  2. for (int i = 1; i <= 100; i++) repeats the body for i from 1 to 100.
  3. sum += i; adds the current i to the sum.
  4. Console.WriteLine(sum) stands after the loop's closing brace. It runs once, when the loop has already finished, and prints 5050.

The order of the header parts is where beginners get confused most often. The widget runs the same loop, but up to 3: press "Next step" and watch when each part of the header runs.

LiveThe order of the for partsstep 1 of 13 · frames: 1

int sum = 0 sets up the sum.

The Main frame shows the program's variables after each step

The step i++ runs after the body, and the condition before it. That is why the last check, 4 <= 3, is false, and the body never runs with i equal to 4. The condition is also checked before the first iteration: if it is false from the start, the body never runs at all.

Interviewers often give you a short loop and ask what it prints. Such tasks test exactly this order of the header parts.

This is where the stadium picture is off. The notebook with its marks stays with the runner after the race, but the variable i disappears along with the loop: on the widget's last step it is gone.

for and while

Any for can be rewritten as a while. Here is the same sum:

C#
int sum = 0;
int i = 1;

while (i <= 100)
{
    sum += i;
    i++;
}

Console.WriteLine(sum); // 5050

The parts of the for header have moved to their own places:

  • the starting value int i = 1 stands before the loop;
  • the condition i <= 100 stands in the while parentheses;
  • the step i++ stands at the end of the body, after sum += i, and so runs after the body, as in for.

There is one difference: here i is declared before the loop and lives on after it, while the i from a for header is visible only inside the loop.

for is the choice when you know how many times to repeat: for each number from 1 to 100. while is the choice when you need to repeat until something happens.

The step can be anything

The step does not have to be i++. The counter can go down or grow by any number:

C#
for (int i = 10; i > 0; i -= 3)
{
    Console.WriteLine(i);
}
// 10
// 7
// 4
// 1

Let's go step by step:

  1. int i = 10 sets the starting value 10.
  2. The condition i > 0 is checked before every iteration.
  3. The step i -= 3 lowers i by 3 after the body, and the program prints 10, 7, 4, 1.
  4. After 1, the step gives -2, the condition -2 > 0 is false, and the loop ends.

Common mistake

The loop variable is read after the loop:

C#
for (int i = 0; i < 3; i++)
{
    Console.WriteLine(i);
}

Console.WriteLine(i); // ✗
TEXT
error CS0103: The name 'i' does not exist in the current context

The program does not compile. A variable declared in a for header is visible only inside the loop: in the header and in the body. Past the loop's closing brace, the name i no longer exists, and neither does the variable. If you need the value after the loop, declare the variable before it, as in the while version.

Key points

  • A for header has three parts: the starting value is set once, the condition is checked before every iteration, and the step runs after the body.
  • If the condition is false from the start, the body of a for never runs.
  • Any for can be rewritten as a while: the starting value before the loop, the step at the end of the body.
  • A variable from a for header is visible only inside the loop.