Looping Techniques in C#

CsharpProgrammingServer Side Programming

A loop statement allows us to execute a statement or a group of statements multiple times. The following are the loops supported in C# −

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

It executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

3

do...while loop

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

With C#, you can also use foreach loop as shown below −

Example

using System;
using System.Collections;

class Demo {
   static void Main() {
      bool[] arr = new bool[5];
      arr[0] = true;
      arr[1] = true;
      arr[2] = false;
      arr[3] = false;

      BitArray bArr = new BitArray(arr);

      foreach (bool b in bArr) {
         Console.WriteLine(b);
      }
        
      bool str = arr[1];
      Console.WriteLine("Value of 2nd element:"+str);
   }
}
raja
Published on 13-Aug-2018 11:27:07
Advertisements