Home  >  Article  >  Backend Development  >  Looping techniques in C#

Looping techniques in C#

王林
王林forward
2023-09-23 15:41:06676browse

C# 中的循环技术

Loop statements allow us to execute a statement or a group of statements multiple times. The following are the loops supported by C#-

##Sr.No.Loop types and description123do...while loopIt is similar to the while statement, only But it tests the condition at the end of the loop body

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

for loop p>It executes a series of statements multiple times and abbreviates management Code for loop variables.

Using C#, you can also use a 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);
   }
}

The above is the detailed content of Looping techniques in C#. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete
Previous article:C# OfType() methodNext article:C# OfType() method