English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C# for Loop

Here, you will learn how to use the for loop, the structure of the for loop, nested for loops that execute statements or code blocks multiple times, and how to exit the for loop.

The for keyword represents a loop in C#. The for loop repeatedly executes a statement block until the specified condition returns false.

Syntax:

for (initializer; condition; iterator)
{    
   //Code block 
}

The for loop contains the following three optional parts, separated by semicolons:

Initializer: is used to initialize a variable that will be a local variable of the for loop and cannot be accessed outside the loop. It can also be zero or more assignment statements, method calls, increment or decrement expressions, such as++i or i++, and await expressions.

Condition: The condition is a boolean expression that returns true or false. If the result of the expression is true, it will execute the loop again; otherwise, it will exit the loop.

Iterator: It defines the increment or decrement of the loop variable.

The following for loop executes a code block10times.

for (int i = 0; i < 10; i++)
{
    Console.WriteLine("The value of i: {0}", i);
}

Try to see the output:

The value of i: 0
The value of i: 1
The value of i: 2
The value of i: 3
The value of i: 4
The value of i: 5
The value of i: 6
The value of i: 7
The value of i: 8
The value of i: 9

In the above example, int i = 0 is an initializer, where we define an int variable i and initializes it with 0. The second part is the condition expression i < 10, if the condition returns true, then it will execute a code block. After executing the code block, it will move to the third part, the iterator. i+ + is an increment statement that increments the loop variable i increases1Now, it will check the condition expression again and repeat the same operation until the condition expression returns false. The following figure illustrates the execution steps of the for loop.

The following diagram illustrates the execution steps of the for loop.

for Loop Execution Steps

If a code block contains only one statement, it is not necessary to wrap it in curly braces { }, as shown below.

 Example: for Loop

for (int i = 0; i < 10; i++{
  Console.WriteLine("Value of i: {0}", i);
}

Initializer, condition, and iterator parts are optional. You can initialize variables before the for loop and define conditions and iterators within the code block, as shown below.

 Example: for Loop in C#

int i = 0;
for (;;)
{
    if (i < 10)
    {
      Console.WriteLine("Value of i: {0}", i);
      i++;
    } else {
       break;
    }
}
Output:
Value of i: 0
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4
Value of i: 5
Value of i: 6
Value of i: 7
Value of i: 8
Value of i: 9

 Since all three parts are optional in the for loop, be careful when defining conditions and iterators. Otherwise, it will be an infinite loop that never ends.

for (; ; )
{
    Console.Write(1);
}
Output:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ....

The control variable of the for loop can be any numeric data type, such as double, decimal, etc.

for (double d = 1.01D; d < 1.10; d+= 0.01D)
{
    Console.WriteLine("Value of i: {0}", d);
}
Output:
Value of i: 1.01
Value of i: 1.02
Value of i: 1.03
Value of i: 1.04
Value of i: 1.05
Value of i: 1.06
Value of i: 1.07
Value of i: 1.08
Value of i: 1.09

The step part in the for loop can increase or decrease the value of the variable.

for (int i = 10; i > 0; i--)
{
    Console.WriteLine("Value of i: {0}", i);
}
Output:
Value of i: 10
Value of i: 9
Value of i: 8
Value of i: 7
Value of i: 6
Value of i: 5
Value of i: 4
Value of i: 3
Value of i: 2
Value of i: 1

Exit for Loop

You can also use the break keyword to exit a for loop.

for (int i = 0; i < 10; i++)
{
    if (i == 5 )
        break;
    Console.WriteLine("Value of i: {0}", i);
}
Output:
Value of i: 0
Value of i: 1
Value of i: 2
Value of i: 3
Value of i: 4

Multiple Expressions

A for loop can also include multiple comma-separated initializer and iterator statements, as shown below.

for (int i = 0, j = 0; i+j < 5; i++, j++)
{
    Console.WriteLine("Value of i: {0}, J: {1}
}
Output:
Value of i: 0, J: 0 
Value of i: 1, J: 1 
Value of i: 2, J: 2

A for loop can also include statements as initializers and iterators.

int i = 0, j = 5;
for (Console.WriteLine($"Initializer: i={i}, j={j}"); 
    i++ < j--; 
    Console.WriteLine($"Iterator: i={i}, j={j}"))
    {
    }

Output:

Initializer: i = 0,j = 5
Iterator: i = 1, j = 4  
Iterator: i = 2, j = 3  
Iterator: i = 3, j = 2

Nested Loops

C# allows a for loop within another for loop.

for (int i = 0; i < 2; i++)
{
    for(int j =i; j < 4; j++{
       Console.WriteLine("i's value: {0}, J: {1}
    }
        
}
Output:
i's value:0,J:0 
i's value:0,J:1  
i's value:0,J:2  
i's value:0,J:3  
i's value:1,J:1  
i's value:1 ,J:2  
i's value:1,J:3