C# Loops

Loops are used to execute a block of code several number of times as you want. There are following types of loop in C#:

  • for loop
  • while
  • do-while loop
  • nested loops

C# for Loop

Here is the general form to use for loop in C# programming:

for(initialization; condition-checker; incrementation)
{
	statement(s);
}

Here is the explanation of for loop working:

  • initialization - this is executed first and only once. This is for declaring and initializing the variable or only initializing the variable
  • condition-checker - this is the second part, to check the condition, whether the condition is evaluated to be true or not. If the condition evaluated to be true, then the program control goes to the inner part of the for loop, otherwise exit from the for loop. The program control comes to condition-checker after the incrementation part except at the entry of the for loop (since control comes at condition-checker part after the initialization part at the first time)
  • incrementation - this is the third part of the for loop. This part is used to increment/decrement the loop variable(s) every time before checking the condition

Example

Here is an example program, demonstrating the for loop in C#:

/* C# Loops - The for Loop - Example Program */
		
using System;
namespace ForLoop
{
	class ForClass
	{
		static void Main(string[] args)
		{
			for(int i=1; i<10; i++)
			{
				Console.WriteLine("i = {0}", i);
			}
			Console.ReadLine();
		}
	}
}

Here is the sample output of the above C# program:

i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

C# while Loop

Here is the general form to use while loop in C# programming:

while(condition)
{
	statement(s);
}

Here condition is the condition which is to be evaluated. If the condition is evaluated to be true, then the program control goes to the inner part of the while loop to execute the statement(s). Otherwise if the condition is evaluated to be false, then the program control goes after the while loop (doesn't goes to the inner part of the while loop).

Example

Here is an example program demonstrating the while loop in C# programming:

/* C# Loops - The while Loop - Example Program */
		
using System;
namespace WhileLoop
{
	class WhileClass
	{
		static void Main(String[] args)
		{
			int i=1;
			while(i<10)
			{
				Console.WriteLine("i = {0}", i);
				i++;
			}
			Console.ReadLine();
		}
	}
}

Here is the output produced by the above C# program.

i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

C# do-while Loop

Here is the general form to use do-while loop in C# programming:

do
{
	statement(s);
}while(condition);

Note - Since the condition placed at the bottom of the do-while loop. So this loop is used mainly in menu-driven program because the statement(s) is executed once in case the condition evaluates to be false at the first time.

Example

Here is an example program demonstrating the do-while loop in C#:

/* C# Loops - The do-while Loop - Example Program */
		
using System;
namespace DoWhileLoop
{
	class DoWhileClass
	{
		static void Main(string[] args)
		{
			int i=1;
			do
			{
				Console.WriteLine("i = {0}", i);
				i++;
			}while(i<10);
			Console.ReadLine();
		}
	}
}

Here is the sample output of the above C# program:

i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

C# nested Loops

You are free to use one loop inside another loop. Here is the general form to use nested for loop in C#:

for(initialization; condition; increment)
{
	for(initialization; condition; increment)
	{
		statement(s);
	}
	statement(s);
}

Here is the general form to use nested while loop in C#:

while(condition)
{
	while(condition)
	{
		statement(s);
	}
	statement(s);
}

Here is the general form to use nested do-while loop in C#:

do
{
	statement(s);
	do
	{
		statement(s);
	}while(condition-checker);
}while(condition-checker);

C# Online Test


« Previous Tutorial Next Tutorial »



Like/Share Us on Facebook 😋