top

Search

C# Tutorial

Loops are used to execute one or more statements multiple times until a specified condition is fulfilled. There are many loops in C# such as for loop, while loop, do while loop etc. Details about these are given as follows:for loop in C#The for loop executes one or more statements multiple times as long as the loop condition is satisfied. If the loop condition is true, the body of the for loop is executed. Otherwise, the control flow jumps to the next statement after the for loop.A diagram that demonstrates the flow in the for loop is given as follows:As seen from the above diagram, first the initialization is done. This declares and initializes the loop variables, if there are any. Then the condition is evaluated. If it is true, loop body is executed. If it is false, the control passes to the next statement after the for loop body. After the loop body is executed, the loop variables are updated. Then the condition is checked again and the cycle continues.The syntax of the for loop is given below:for ( initialization, condition, increment ) {    // These statements will be executed if the condition evaluates to true } A program that demonstrates the for loop is as follows: Source Code: Program that demonstrates for loop in C# using System; namespace LoopDemo {   class Example {      static void Main(string[] args) {         Console.WriteLine("First 10 Natural Numbers are: ");         for (int i = 1; i <= 10; i++)           Console.WriteLine(i);      }   } }The output of the above program is as follows:First 10 Natural Numbers are: 1 2 3 4 5 6 7 8 9 10while loop in C#The while loop continuously executes one or more statements as long as the loop condition is satisfied. If the loop condition is true, the body of the for loop is executed. Otherwise, the control flow jumps to the next statement after the while loop.A diagram that demonstrates the flow in the while loop is given as follows:As seen from the above diagram, first the condition is checked. If the condition is true, the loop body is executed. If the condition is false, control passes to the next statement after the loop body. The while loop may never run if the condition is false the first time it is tested. The control skips the loop and goes directly to the next statement.The syntax of the while loop is given as follows:while (condition) {   // These statements will be executed if the condition evaluates to true }A program that demonstrates the while loop is given as follows:Source Code: Program that demonstrates while loop in C#using System; namespace LoopDemo {   class Example {      static void Main(string[] args) {         int i = 1;         Console.WriteLine("First 10 Natural Numbers are: ");         while (i <= 10)         {             Console.WriteLine( i );             i++;         }      }   } }The output of the above program is as follows: First 10 Natural Numbers are: 1 2 3 4 5 6 7 8 9 10do-while loop in C#The do while loop executes one or more statements multiple times as long as the loop condition is satisfied. It is similar to the while loop but the while loop has the test condition at the start of the loop and the do while loop has the test condition at the end of the loop. So it executes at least once always.A diagram that demonstrates the flow in the do while loop is given as follows:As seen from the above diagram, the first time loop body is directly executed as the test condition is at the bottom. The condition is checked. If it is true, the loop body is executed again. If the condition is false, control passes to the next statement after the loop body.The syntax of the do while loop is given as follows:do {   // These statements will be executed if the condition evaluates to true }while (condition); A program that demonstrates the do while loop is given as follows: Source Code: Program that demonstrates do while loop in C# using System; namespace LoopDemo {   class Example {      static void Main(string[] args) {         int i = 1;  Console.WriteLine("First 10 Natural Numbers are: ");         do         {             Console.WriteLine( i );             i++;         } while (i <= 10);      }   } }The output of the above program is as follows:First 10 Natural Numbers are: 1 2 3 4 5 6 7 8 9 10Nested loops in C#Nested loops are loop where one loop is situated inside another. Nested loops can be created from for loops, while loops and do while loops.The syntax of nested loops is given as follows:Outer loop {       // Statements in the outer loop    Inner Loop    {        // Statements in the inner loop    }        // Statements in the outer loop } A program that demonstrates nested loops is given as follows: Source Code: Program that demonstrates nested loops in C# using System; namespace LoopDemo {   class Example {      static void Main(string[] args) {         Console.WriteLine("Nested for loop");         for( int i = 0; i < 5; i++)         {             for(int j=0; j<=i; j++)               Console.Write("*");             Console.WriteLine();         }      }   } }The output of the above program is as follows:Nested for loop * ** *** **** *****foreach loop in C#The foreach loop executes one or more statements for all the elements in an instance of the type Systems.Collection.Generic.IEnumerable<T> or Systems.Collection.IEnumerable interface.A program that demonstrates the foreach loop is given as follows:Source Code: Program that demonstrates foreach loop in C#using System; namespace LoopDemo {   class Example {      static void Main(string[] args) {        Console.WriteLine("First 10 Natural Numbers are: ");        int[] naturalNos = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };          foreach (int i in naturalNos)          {            Console.WriteLine(i);          }      }   } }The output of the above program is as follows:First 10 Natural Numbers are: 1 2 3 4 5 6 7 8 9 10
logo

C# Tutorial

Loops in C#

Loops are used to execute one or more statements multiple times until a specified condition is fulfilled. There are many loops in C# such as for loop, while loop, do while loop etc. Details about these are given as follows:

for loop in C#

The for loop executes one or more statements multiple times as long as the loop condition is satisfied. If the loop condition is true, the body of the for loop is executed. Otherwise, the control flow jumps to the next statement after the for loop.

A diagram that demonstrates the flow in the for loop is given as follows:

for loop in C#

As seen from the above diagram, first the initialization is done. This declares and initializes the loop variables, if there are any. Then the condition is evaluated. If it is true, loop body is executed. If it is false, the control passes to the next statement after the for loop body. After the loop body is executed, the loop variables are updated. Then the condition is checked again and the cycle continues.

The syntax of the for loop is given below:

for ( initialization, condition, increment )
{
   // These statements will be executed if the condition evaluates to true
}
A program that demonstrates the for loop is as follows:
Source Code: Program that demonstrates for loop in C#
using System;
namespace LoopDemo
{
  class Example {
     static void Main(string[] args) {
        Console.WriteLine("First 10 Natural Numbers are: ");
        for (int i = 1; i <= 10; i++)
          Console.WriteLine(i);
     }
  }
}

The output of the above program is as follows:

First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10

while loop in C#

The while loop continuously executes one or more statements as long as the loop condition is satisfied. If the loop condition is true, the body of the for loop is executed. Otherwise, the control flow jumps to the next statement after the while loop.

A diagram that demonstrates the flow in the while loop is given as follows:

while loop in C#

As seen from the above diagram, first the condition is checked. If the condition is true, the loop body is executed. If the condition is false, control passes to the next statement after the loop body. The while loop may never run if the condition is false the first time it is tested. The control skips the loop and goes directly to the next statement.

The syntax of the while loop is given as follows:

while (condition)
{
  // These statements will be executed if the condition evaluates to true
}

A program that demonstrates the while loop is given as follows:

Source Code: Program that demonstrates while loop in C#

using System;
namespace LoopDemo
{
  class Example {
     static void Main(string[] args) {
        int i = 1;
        Console.WriteLine("First 10 Natural Numbers are: ");
        while (i <= 10)
        {
            Console.WriteLine( i );
            i++;
        }
     }
  }
}

The output of the above program is as follows: 

First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10

do-while loop in C#

The do while loop executes one or more statements multiple times as long as the loop condition is satisfied. It is similar to the while loop but the while loop has the test condition at the start of the loop and the do while loop has the test condition at the end of the loop. So it executes at least once always.

A diagram that demonstrates the flow in the do while loop is given as follows:

do-while loop in C#

As seen from the above diagram, the first time loop body is directly executed as the test condition is at the bottom. The condition is checked. If it is true, the loop body is executed again. If the condition is false, control passes to the next statement after the loop body.

The syntax of the do while loop is given as follows:

do
{
  // These statements will be executed if the condition evaluates to true
}while (condition);
A program that demonstrates the do while loop is given as follows:
Source Code: Program that demonstrates do while loop in C#
using System;
namespace LoopDemo
{
  class Example {
     static void Main(string[] args) {
        int i = 1;
 Console.WriteLine("First 10 Natural Numbers are: ");
        do
        {
            Console.WriteLine( i );
            i++;
        } while (i <= 10);
     }
  }
}

The output of the above program is as follows:

First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10

Nested loops in C#

Nested loops are loop where one loop is situated inside another. Nested loops can be created from for loops, while loops and do while loops.

The syntax of nested loops is given as follows:

Outer loop
{
      // Statements in the outer loop
   Inner Loop
   {
       // Statements in the inner loop
   }
       // Statements in the outer loop
}
A program that demonstrates nested loops is given as follows:
Source Code: Program that demonstrates nested loops in C#
using System;
namespace LoopDemo
{
  class Example {
     static void Main(string[] args) {
        Console.WriteLine("Nested for loop");
        for( int i = 0; i < 5; i++)
        {
            for(int j=0; j<=i; j++)
              Console.Write("*");
            Console.WriteLine();
        }
     }
  }
}

The output of the above program is as follows:

Nested for loop
*
**
***
****
*****

foreach loop in C#

The foreach loop executes one or more statements for all the elements in an instance of the type Systems.Collection.Generic.IEnumerable<T> or Systems.Collection.IEnumerable interface.

A program that demonstrates the foreach loop is given as follows:

Source Code: Program that demonstrates foreach loop in C#

using System;
namespace LoopDemo
{
  class Example {
     static void Main(string[] args) {
       Console.WriteLine("First 10 Natural Numbers are: ");
       int[] naturalNos = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };  
       foreach (int i in naturalNos)  
       {
           Console.WriteLine(i);  
       }
     }
  }
}

The output of the above program is as follows:

First 10 Natural Numbers are:
1
2
3
4
5
6
7
8
9
10

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial