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:
int sum = 0;
for (int i = 1; i <= 100; i++)
{
sum += i;
}
Console.WriteLine(sum); // 5050The three header parts
The parentheses after for hold three parts separated by semicolons:
int i = 1runs once, before the loop starts. The runner steps up to the start line.- The condition
i <= 100is checked before every iteration. If it is true, the body in braces runs. This is the check under the arch before a lap. - The step
i++runs after the body. This is the mark in the notebook.
Now let's go through the program:
int sum = 0;sets up the sum.for (int i = 1; i <= 100; i++)repeats the body forifrom1to100.sum += i;adds the currentito the sum.Console.WriteLine(sum)stands after the loop's closing brace. It runs once, when the loop has already finished, and prints5050.
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.
1int sum = 0;2 3for (int i = 1; i <= 3; i++)4{5 sum += i;6}7 8Console.WriteLine(sum);
int sum = 0 sets up the sum.
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:
int sum = 0;
int i = 1;
while (i <= 100)
{
sum += i;
i++;
}
Console.WriteLine(sum); // 5050The parts of the for header have moved to their own places:
- the starting value
int i = 1stands before the loop; - the condition
i <= 100stands in thewhileparentheses; - the step
i++stands at the end of the body, aftersum += i, and so runs after the body, as infor.
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:
for (int i = 10; i > 0; i -= 3)
{
Console.WriteLine(i);
}
// 10
// 7
// 4
// 1Let's go step by step:
int i = 10sets the starting value10.- The condition
i > 0is checked before every iteration. - The step
i -= 3lowersiby3after the body, and the program prints10,7,4,1. - After
1, the step gives-2, the condition-2 > 0is false, and the loop ends.
Common mistake
The loop variable is read after the loop:
for (int i = 0; i < 3; i++)
{
Console.WriteLine(i);
}
Console.WriteLine(i); // ✗error CS0103: The name 'i' does not exist in the current contextThe 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
forheader 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
fornever runs. - Any
forcan be rewritten as awhile: the starting value before the loop, the step at the end of the body. - A variable from a
forheader is visible only inside the loop.