1. Introduction
Imagine you have a task: print "Hello!" to the screen ten times. Sure, you could write ten lines with Console.WriteLine("Hello!"); — but now you know about the while loop, so you can totally handle this.
But there's another loop — for, which repeats the needed action the exact number of times you want. And it can count by itself!
Here's a typical example:
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Hello!");
}
What does this code do? It prints "Hello!" to the screen exactly 10 times. No copy-paste, no wrist pain.
2. for Loop Syntax
The for loop has three main parts, written in parentheses after the for keyword:
for (initialization; condition; change)
{
// Loop body
}
Let's break down each part in detail:
- Initialization — usually creating a counter variable to keep track of the repeats (int i = 0).
- Condition — checked before each iteration. If it's true, the loop body runs. If it's false, the loop ends. For example, i < 10.
- Change — this runs after each iteration. Usually increases or decreases the counter (i++).
You can draw a diagram:
3. Comparing for and while
Maybe it'll make more sense if I tell you that for is just another way to write a while loop.
We had a while loop:
int i = 0; // Initialization of the loop variable
while(i < 10) // Checking the loop continuation condition
{
Console.WriteLine(i); // Useful action (loop body)
i++ // Increasing the counter
}
We can write it as a for loop by just moving all the stuff that controls the loop logic:
for (int i = 0; i < 10; i++) // Initialization; Condition check; Counter increase
{
Console.WriteLine(i); // Useful action (loop body)
}
Steps for both loops:
- Initialization int i = 0; runs once before the loop.
- Then the condition i <= 10 is checked
- Then the loop's useful action Console.WriteLine(i);
- Then the counter increases i++
- Then it goes back to step 2.
4. for Loop Usage Examples
The simplest example
Let's print numbers from 1 to 5:
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
Output:
1
2
3
4
5
Note: if you wrote i < 5, the last output would be 4.
Countdown
Want to feel like an astronaut? Let's do a countdown!
for (int i = 5; i > 0; i--)
{
Console.WriteLine(i);
}
Console.WriteLine("Let's go!");
Output:
5
4
3
2
1
Let's go!
Loop step not equal to 1
You can change the counter by any number, not just 1:
for (int i = 0; i <= 10; i += 2)
{
Console.WriteLine(i);
}
// Will print: 0 2 4 6 8 10
5. for Loop Variations
Multiple variables in the loop
Sometimes it's handy to track two variables at once. For example, let's print numbers from left to right and right to left:
for (int left = 1, right = 10; left <= 10; left++, right--)
{
Console.WriteLine($"{left} {right}");
}
Output:
1 10
2 9
3 8
...
10 1
Infinite for loop
If things get really wild (like your project is a perpetual motion machine in IT), you can make a "forever" for loop:
for (;;) // no initialization, no condition, no change
{
Console.WriteLine("Work, work, and work some more!");
}
Warning: this loop will only end if there's a break, return, or throw inside. Don't use it without a good reason, or you might wake up an ancient demon — "Process not responding".
GO TO FULL VERSION