TExcelTAD
Home › C# › C# For Loop

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

csharp
for (initializer; condition; iterator)
{
    // body
}
  1. The initializer runs once, before the loop starts (usually int i = 0).
  2. The condition is checked before each iteration; the loop stops when it is false.
  3. The iterator runs after each iteration (usually i++).
csharp · try it yourself
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.

csharp · try it yourself
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:

csharp · try it yourself
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.

csharp · try it yourself
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 to Length - 1, and array[array.Length] throws an IndexOutOfRangeException.

Nested loops

A loop inside another loop runs completely for every iteration of the outer loop. Nested loops are ideal for grids and tables:

csharp · try it yourself
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:

csharp
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?

  1. 3
  2. 4
  3. 10
  4. 9

Which part of a for loop runs only once?

  1. The condition
  2. The iterator
  3. The initializer
  4. The body

With int[] a = new int[5];, which loop visits every element exactly once?

  1. for (int i = 1; i <= 5; i++)
  2. for (int i = 0; i <= 5; i++)
  3. for (int i = 0; i < 5; i++)
  4. for (int i = 1; i < 5; i++)