C# For Loop

C# for loop is one of the most used loop in C# programming language and it is same as we have seen in C/ C++ or Java ( if you have learned coding in any of the languages). For loop has a initialized variable, condition until when to loop, and third is incremement/decrement of value.

Syntax of for loop

for(initialization;condition; increment/decrement)
{
   //code to execute until condition in the for loop = true
}

Where

  • initialization = variable which we initialize, it can be int, char etc type, example : int i =0;
  • condition = some condition which will make loop run until this condition is true, example : i < 10;
  • increment/decrement = variable is incremented and decremented accordingly, example: i++
  • Inisde braces we write code which should execute until the above condition is true, example : Console.WriteLine("Value of I ="+i);

Here is the complete example, considering above points

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

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

As the value of i =0; in first loop, it prints 0, then 1 because it was incremented by 1, then 2 and so on.

It will print all the values until the value i < 10, so last value is 9.

Nested For loops in C#

We can have nested for loops, means a for loop inside another for loop, which is useful when coding for Pyramid pattern in C#, let's take a look on how it works.

using System;
					
public class ForLoopProgram
{
	public static void Main()
	{
		//this for loop is checked
		for(int i=1;i<=3;i++)
		{    
			    //then this for loop is executed until condition is true
			   //on each outer for loop run, this is executed completed again
                for(int j=1;j<=3;j++)
				{    
                    Console.WriteLine(i+" "+j);    
                }   
			 Console.WriteLine("End of round "+i +" for outer loop");
        }    
	}
}

Output:

1 1
1 2
1 3
End of round 1 for outer loop
2 1
2 2
2 3
End of round 2 for outer loop
3 1
3 2
3 3
End of round 3 for outer loop

Inifinite For loop

If we will not define any condition inside for loop and leave it as it is, it will be executed infinitely until we stop the program manually by clicking Ctrl+C. Example of infinite for loop is

using System;
					
public class InifiniteForProgram
{
	public static void Main()
	{
		for(;;)
		{
		  Console.WriteLine("Hello World");
		}
	}
}

Incrementing value by 2 in for loop

You can increment, decrement value by 2 also inside for loop, using operators +=., example  i+=2

for (int i = 0; i < 20; i+=2) 
{
 Console.WriteLine(i);
}

If you don't like the above method, you can also leave the increment/decrement part blank and do this inside the {} braces, after main code is executed, example

using System;
					
public class Program
{
	public static void Main()
	{		
		for (int i = 0; i < 20; ) 
		{
		  Console.WriteLine(i );
	      //increment value here
		  i= i+2;

		}
	}
}

Output:

0
2
4
6
8
10
12
14
16
18

Share Tweet