C# - Loops


There may be a situation, when you need to execute a block of code several number of times. In general, the statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.



Loop Architecture

C# provides following types of loop to handle looping requirements. Click the following links to check their detail.

Sr.No. Loop Type & Description
1 while loop

It repeats a statement or a group of statements while a given condition is true. It tests the condition before executing the loop body.

2 for loop


3 do...while loop

It is similar to a while statement, except that it tests the condition at the end of the loop body

4 nested loops


Loop Control Statements


C# provides the following control statements. Click the following links to check their details.

Sr.No. Control Statement & Description
1 break statement


2 continue statement


Infinite Loop



using System;

namespace Loops {
   class Program {
      static void Main(string[] args) {
         for (; ; ) {
            Console.WriteLine("Hey! I am Trapped");

When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but programmers more commonly use the for(;;) construct to signify an infinite loop.