C# For Loop
When you know how many times a loop should run — ten times, once per array element, every number from 1 to 100 — the for loop keeps all the loop control in one line.
Syntax
for (initializer; condition; iterator)
{
// body
}- The initializer runs once, before the loop starts (usually
int i = 0). - The condition is checked before each iteration; the loop stops when it is false.
- The iterator runs after each iteration (usually
i++).
using System;
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(quot;Iteration {i}");
}Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
The variable i exists only inside the loop. After the closing brace, it is out of scope.
Counting in different steps
The iterator can be any expression: count backwards, skip numbers, or multiply.
using System;
for (int i = 10; i >= 0; i -= 2)
Console.Write(i + " ");
Console.WriteLine();
for (int i = 1; i <= 1000; i *= 10)
Console.Write(i + " ");
Console.WriteLine();10 8 6 4 2 0
1 10 100 1000
Accumulating results
Loops are often used to build up a total. Here we sum the numbers 1 to 100 and compute a factorial:
using System;
int sum = 0;
for (int i = 1; i <= 100; i++)
sum += i;
Console.WriteLine(quot;Sum 1..100 = {sum}");
long factorial = 1;
for (int i = 2; i <= 15; i++)
factorial *= i;
Console.WriteLine(quot;15! = {factorial}");Sum 1..100 = 5050
15! = 1307674368000
We used long for the factorial because 15! is far larger than int.MaxValue.
Looping over an array by index
A for loop is the right choice when you need the index of each element — to number items, compare neighbours or modify the array.
using System;
string[] planets = { "Mercury", "Venus", "Earth", "Mars" };
for (int i = 0; i < planets.Length; i++)
{
Console.WriteLine(quot;{i + 1}. {planets[i]}");
}
int[] values = { 3, 8, 2, 9, 4 };
for (int i = 0; i < values.Length; i++)
values[i] *= 10;
Console.WriteLine(string.Join(", ", values));1. Mercury
2. Venus
3. Earth
4. Mars
30, 80, 20, 90, 40
Note: Use
i < array.Length, not<=. Valid indexes go from 0 toLength - 1, andarray[array.Length]throws anIndexOutOfRangeException.
Nested loops
A loop inside another loop runs completely for every iteration of the outer loop. Nested loops are ideal for grids and tables:
using System;
for (int row = 1; row <= 4; row++)
{
for (int col = 1; col <= 4; col++)
{
Console.Write(quot;{row * col,4}");
}
Console.WriteLine();
} 1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
Multiple variables
The initializer and iterator can contain several comma-separated parts. This loop walks two indexes toward each other to reverse an array:
for (int left = 0, right = arr.Length - 1; left < right; left++, right--)
{
(arr[left], arr[right]) = (arr[right], arr[left]); // swap
}Test yourself
How many times does for (int i = 0; i < 10; i += 3) run?
- 3
- 4
- 10
- 9
Which part of a for loop runs only once?
- The condition
- The iterator
- The initializer
- The body
With int[] a = new int[5];, which loop visits every element exactly once?
- for (int i = 1; i <= 5; i++)
- for (int i = 0; i <= 5; i++)
- for (int i = 0; i < 5; i++)
- for (int i = 1; i < 5; i++)