Loops

Loops in C# used for executing a piece of code repeatedly or Loops are usually used for eliminating the repeated code. C# supports following loops.
  • for
  • foreach
  • while

For loop

using a for loop we can perform a repeating activity with less code. for(initilaization;condition;incrementation/decrementation) { } Following for loop in main method displays all data present in dNames array.
public class Test
{
    public static void Main(string[] args)
    {
        string[] dNames = new string[6] {"Dr.Yuvi","Dr.Kate","Dr.Dappu","Dr.Vani","Dr.Bill","Dr.Sara"};
        for(int i=0;i < dNames.Length;i++)
        {
          Console.WriteLine(dNames[i]);
        }
    }
}

For each loop

For each loop is almost similar to a for loop. For each loop will be usually used for reading everything present in a collection.Using for loop we can modify data present in a collection but by using foreach we can't modify data present in a collection.See the following code for sample.
public class Test
{
    public static void Main(string[] args)
    {
        string[] dNames = new string[6] { "Dr.Yuvi", "Dr.Kate", "Dr.Dappu", "Dr.Vani", "Dr.Bill", "Dr.Sara" };
        foreach (string item in dNames)
        {
            Console.WriteLine(item);
        }
    }
}

While loop

While loop in C# is used for executing a piece of code repeatedly based on a boolean expression. The while loop is technically equalent to a repeating if condition.See the following sample code for more details.
public class Test
{
    public static void Main(string[] args)
    {
        string[] dNames = new string[6] { "Dr.Yuvi", "Dr.Kate", "Dr.Dappu", "Dr.Vani", "Dr.Bill", "Dr.Sara" };
        int count=dNames.Length;
        while(count>0)
        {
          Console.WriteLine(dNames[count-1]);
          count--;
        }
    }
}

Assignment

write a for loop, for each loop and a while loop seperately for displaying data present in the following array. int[] firstSemisterMarks=new int[]{90,80,50,70};