Looping Techniques in C#


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
1while 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.
2for loop
It executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
3do...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);
   }
}

Updated on: 21-Jun-2020

122 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements