C# for Loop

The C# for loop is an iteration statement which executes a code block until the condition remains true. The C# for code block must be enclosed in two curly braces {} if loop body has more than one line of code.

Let's understand the for loop flow with an image.

Following is the syntax of a for statement.

for (INITIALIZATION; CONDITION; ITERATOR) 
{
    // Code for the for-loop's body goes here.
}

A C# for loop expression must have two semicolons. These semicolons divide the initialization, condition and iterator parts. However, it's not mandatory to have these parts written in a for loop expression, but the semicolons are still required.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("The value of i is : {0}", i);
            }
        }
    }
}

The above for loop works in following way.

  • First, if there is an initialization part defined, it gets evaluated.
  • Then as long as the condition remains true, the loop body gets executed.
  • When the condition part returns false the control gets transferred outside the loop.

The above loop prints the value of i till the condition remains true, ie i remains less than 10.

Output

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

A for loop is generally used in those conditions when we know the exact number of times a loop has to execute. Iterating over arrays are an example of such conditions. In the following example, a for loop is being used to print the values of an integer array.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            int[] arr = new int[5] { 10, 20, 30, 40, 50 };
 
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine("Value at position [{0}] in array is : {1}", i, arr[i]);
            } 
        }
    }
}
//Output
//Value at position [0] in array is : 10
//Value at position [1] in array is : 20
//Value at position [2] in array is : 30
//Value at position [3] in array is : 40
//Value at position [4] in array is : 50

Any valid data type can be used in a for loop expression. In the following example, a float DataType is being used to print the values.

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            for (float i = 1.0F; i < 2; i = i + .1F)
            {
                Console.WriteLine("Value at i is : {0}", i);
            }
        }
    }
}

Nested for loop

It's perfectly valid to create nested for loops in C#. C# allows creating a for loop inside another for loop. In the following example nested for loops are being used to print a series of '*'

Example

Try It
using System;
 
namespace TutorialsLeader
{
    public class MyFirstClass
    {
        static void Main()
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j <= i; j++)
                {
                    Console.Write("*");
                }
                Console.Write("\n");
            }
        }
    }
}
//Output
*
**
***
****
*****
******
*******
********
*********
**********

Infinite Loop

A for loop can be defined in the following way also. But as there no condition is being checked inside the loop declaration, this loop will become an infinite loop

for (; ;)
{
    //loop body goes here
}

In such kind of situations, control statements are used to control the execution of a for loop. Let's understand about the break statement in the next section.