In this article I am going to discuss about C# For Loop in detail but before that if you had not read my previous article C# Switch Statement then must read it. In C# for loop is one of the iteration statement which is type of C# Control Flow Statements. We had already covered all types of selection statement in detail and now its time to discuss about the iteration statement. Today I am going to discussing one of the important and most used C# For Loop.

Introduction to C# For Loop

In programming, it is very common to print certain statements for specific number of times. So that doesn’t mean we are going to write the same line specific number of times in order to print them. Therefore to solve this problem iteration statement comes into existence. C# For Loop is one of the type of iteration statement that is used to print the statement for specific number of time as per condition. As the condition is true the statement is going to printed. The statement is get printed till the condition is not going to fail.

Syntax of C# For Loop

for(initialization;condition;increment/decrement){
//block of statement to be executed
}

1. Initialization : In For Loop Initialization is only done once which means variable is usually declared and initialized.

2. Condition : Condition is a boolean expression which returns true or false. If the condition is true then statement is printed else it will come out of the loop.

3. Increment/Decrement : If the condition is true then it will go to last stage of for loop which is either increment or decrement the initialized variable. Increment/Decrement stage is also known as an iterator. We can set the iterator to increment or decrement as per our requirement.

C# Program to Explain For Loop

using System;
namespace ForLoop
{
	class Program
	{
		public static void Main(string[] args)
		{
			for (int i=1; i<=5; i++)
			{
				Console.WriteLine("C# For Loop: Iteration {0}", i);
			}
		}
	}	
}
Output:
C# For Loop: Iteration 1
C# For Loop: Iteration 2
C# For Loop: Iteration 3
C# For Loop: Iteration 4
C# For Loop: Iteration 5
  1. In above program initialization is int i=1 which means the initial value of i is 1.
  2. Then we are checking the condition that if i<=5 then it will print the body else come out of loop.
  3. The third and most important point iterator here we have incremented the i till the condition is true. In simple words if the condition is true then it will increase the value of i with 1. we can also increment the value of i with 2 by writing i+=2 in iterator.
  4. Let see one more example to print the value with iterator of i+2.
using System;
namespace ForLoop
{
	class Program
	{
		public static void Main(string[] args)
		{
			for (int i=1; i<=5; i+=2)
			{
				Console.WriteLine("C# For Loop: Iteration {0}", i);
			}
		}
	}	
}
Output:
C# For Loop: Iteration 1
C# For Loop: Iteration 3
C# For Loop: Iteration 5

Let see the execution of above code in the form of table with each and every step for more understanding.

IterationInitializationConditionIncrementValue of i
1i=1i<=5 ( true)i+2 (post increment)1
2i=3i<=5 (true)i+23
3i=5i<=5 (true)i+25
4i=7i<=5 (false)NilLoop terminates

Infinite Loop

Infinite Loop refers to the loop which never terminates which means the condition inside the for loop never fails. If the condition inside our for loop never fails then it will print the loop’s body infinite number of times and this type of program leads to memory leakage in our computer. Therefore we must have to avoid infinite loop in our program. For example:

using System;
namespace InfiniteForLoop
{
	class Program
	{
		public static void Main(string[] args)
		{
			for (int i=1 ; i>0; i++)
			{
				Console.WriteLine("C# For Loop: Iteration {0}", i);
			}
		}
	}
}

If you run the above program then you see the result which prints the for loop body infinte number of times because here we have pass the condition i>0. So that if i>0 then it will increment the initialization and execute the body then again check the condition and increment the initialization and execute the body. Same it will check the condition every time and every time the condition is true therefore it will execute the body infinite number of times.

Can For Loop Run Without Condition?

Yes, you can run c# for loop without condition but it will turn into infinite loop because if you don’t provide the condition for your loop then it will simply increment the initialization and then executes the body again it will increment the initialization and then executes the body again. Therefore it will turn this loop into infinite loop.

Nested For Loop in C#

In C#, like nested if-else statement we also have nested for loop in c# which is very useful and important in programming. Nested for loop is used in designing patterns, when we are dealing with array and matrices and so on. When we create one for loop inside the body of another for loop, then it is said to be nested for loop in C# programming language.

C# Program to Explain Nested For Loop in C#

using System;
namespace NestedForLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 5; i++) //Outer For Loop
            {
                Console.WriteLine("Outer For Loop : {0}",i);
                for (int j = 1; j <= 10; j++) //Inner For Loop
                {
                    Console.Write(" {0}",j);
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}
Output:
Outer For Loop : 1
 1 2 3 4 5 6 7 8 9 10
Outer For Loop : 2
 1 2 3 4 5 6 7 8 9 10
Outer For Loop : 3
 1 2 3 4 5 6 7 8 9 10
Outer For Loop : 4
 1 2 3 4 5 6 7 8 9 10
Outer For Loop : 5
 1 2 3 4 5 6 7 8 9 10

Conclusion

In this C# tutorials article I have discussed the csharp for loop in detail. We have also covered the nested for loops and how the loop becomes infinite loop in csharp. Hope you like this article. In my next article I am going to discuss about while loop in csharp programming language.