C# Loop Control Structure

Loop Control Structure

Many times there is a requirement to execute some statements repeatedly many times. Programming languages provide looping structures to accomplish this kind of task. C# provides following looping structures & case control structure.

  • For Loop
  • While Loop
  • Do While Loop
  1. For Loop:

The most common used looping structure is ‘for’. In for loop, there is one loop control variable which acts as a counter that controls the number of iterations. The general structure of for loop is as below:

for (initialization ; condition; increment/decrement) 
{ 
     //Statements to be executed repeatedly 
}

The curly braces are optional; if there is only one statement to be executed then you can write for loop as below:

for (initialization ; condition; increment/decrement) 
//Statement to be executed repeatedly

In above syntax, there are three parts

Initialization part initializes the loop control structure to some value. The second part defines the expression is a boolean expression, the loop is repeated as long as the value of a boolean expression is true. The third part is used to increment or decrement the value of loop control variable. All above three parts are optional in for loop.

Let’s see following example:

using System; 
namespace ForLoop 
{
  class Program
  {
    static void Main(string[] args)
    { 
      for (int counter = 0; counter < 5;counter++) 
      { 
        Console.WriteLine("The value of variable counter is : "+counter); 
      }
      Console.ReadLine(); 
    } 
  } 
}

The output of program is:

The value of variable counter is : 0 
The value of variable counter is : 1 
The value of variable counter is : 2 
The value of variable counter is : 3 
The value of variable counter is : 4
  1. While Loop: 
Recommended Post   C# Language Fundamentals

The structure of while loop is as below

while (condition)
{
    //Statements to be executed repeatedly
}

Like for loop you omit curly braces in while loop also if there is only one statement to be executed like below

while(condition)
//Statement to be executed

The condition in while loop is a boolean expression which returns to true or false. This is an entry controlled loop, this means the condition is checked at the entry point of a loop and if condition returns to true then loop statements are executed. If the condition returns to false then loop terminated.

Let’s see following example

using System;
namespace WhileLoop
{
  class Program
  {
    static void Main(string[] args)
    {
      int counter = 1;
      while (counter <= 5)
      {
        Console.WriteLine("The value of counter is : "+counter );
        counter++;
      }
      Console.ReadLine();
    }
  }
}

The output of the program is:

The value of variable counter is : 1
The value of variable counter is : 2
The value of variable counter is : 3
The value of variable counter is : 4
The value of variable counter is : 5
  1. Do While Loop: 

The third loop control structure in C# is a do-while loop. This is an exit controlled loop; this means the condition is checked after the execution of statements in the loop. If the condition expression returns to true then next iteration is performed otherwise loop is terminated. The syntax of do-while loop is as below

do
{
    //Statements to be executed repeatedly
 } while (condition);

Here also curly braces or not mandatory and you can write loop for single statement as:

do
//Statment to be executed repeatedly
while(condition);

The condition must be a valid boolean expression. As a condition is checked at the end of a loop,     statements in this loop get executed at least on time.

Recommended Post   C# Tutorial

Let’s see following example

using System;
namespace DoWhileLoop
{
  class Program
  {
    static void Main(string[] args)
    {
      int counter = 1;
      do{
          Console.WriteLine("The value of counter is : " + counter);
          counter++;
        } while (counter<= 5);
        Console.ReadLine();
     }
   }
}

The output of program is

The value of variable counter is : 1
The value of variable counter is : 2
The value of variable counter is : 3
The value of variable counter is : 4
The value of variable counter is : 5

Following are some other statements which are used with looping structures

  • Break Statement: 

The break statement is used in a loop to terminate the execution of a loop. When program encounters break statement in any loop, the loop is terminated and program control goes to next line after the loop. In the case of nested loops if the break is present in the inner loop then it terminates inner loop and control goes to next line after the inner loop.

Let’s see following example:

using System;
namespace BreakStatement
{
  class Program
  {
    static void Main(string[] args)
    {
      for (int counter = 0; counter< 5; counter++)
      {
        if (counter== 3)
        {
          break;
         }
         Console.WriteLine("The value of variable counter is : " + counter);
       }
       Console.WriteLine("The for loop is terminated.");
       Console.ReadLine();
      }
   }
}

The output of program is

The value of variable counter is : 0
The value of variable counter is : 1
The value of variable counter is : 2
The for loop is terminated.

Continue Statement: 

Recommended Post   C# Classes & Objects 

The continue statement in loop forces the control to go to next iteration. When a continue statement encountered in a loop, all statements below the continue statement are skipped and controls go at the entry of loop with a new iteration.

Let’s see following example

using System;
namespace ContinueStatement
{
  class Program
  {
    static void Main(string[] args)
    {
      for (int counter = 0; counter < 5; counter++)
      {
        if (counter == 3)
        {
          continue;
        }
        Console.WriteLine("The value of variable counter is : " + counter);
      }
      Console.ReadLine();
    }
  }
}

The output of program is

The value of variable counter is : 0
The value of variable counter is : 1
The value of variable counter is : 2
The value of variable counter is : 4
Content Protection by DMCA.com