Iterative Programming

By   Tewodros   Date Posted: Oct. 14, 2021  Hits: 774   Category:  ASP.NET C#   Total Comment: 0             A+ A-


side

Iterative Programming is executing a specific code block several time until an exit condition is met.

There are four ways to do Iterative Programming in C#

 

  1. for Loops
  2. foreach loop
  3. while loop
  4. do while loop

 

For Loop

Syntax

for(variable initialization; condition ; variable increment) {

   //code block goes here

}

Example:

for( int i=0;i<10;i++) {

    Console.Write(i);

}

Output: 12345678910

Explanation:

We want to execute the body of the loop as long as an integer i < 10 starting from i=0 and incrementing it by 1 in each iteration. 

Example:

 

           for (char c = 'a'; c < ='z'; c++)  {

               Console.Write(c);

           }

Output: abcdefghijklmnopqrstuvwxyz

Explanation:

We want to execute the body of the loop as long as a character c < ā€˜zā€™ starting from a and incrementing it by one letter in each iteration. 

 

Loops are very effective in working hand to hand with arrays specially when we want to navigate through array items.

Example: The follwoing program displays only even number from the given array list of integers. 

int [] nums = {1,2,3,4,5,6,7,8,9,10};

for( int i=0;i<10;i++) {

if(nums[i] %2 == 0)

    Console.Write(nums[i]);

}

Output: 1235678910

Foreach loop

Foreach loop is similar to for loop but it is a has a relatively shorter syntax. Basically it iterates over a collection of list items and do something with the items while a for loop uses index based access system which works based with index based collections like arrays.

Example:

   int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

   foreach (int digit in nums)  {

               if (digit % 2 == 0)

                   Console.Write(digit);

      }

Example:

List<int> digits = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9,10 }; 

foreach(int digit in digits ) {

    if(digit %2 == 0) 

        Console.Write(digit);

}

Output: 2 4 6 8 10

While loop

while loop is a litter different than for loop, we do the initialization part outside of the while loop and the increment part in side the code block

Syntax:

variable initialization

while(condition  ) {

   //code block goes here

  variable increment

}

Example:

int i=0;

while( i<10)  {

    Console.Write(i);

    i++;

}

char c = 'a';

for ( c < ='z';)  {

     Console.Write(c);

     c++;

  }

While loop are used when you don't know how many times you want to loop through the code block and you just want to execute it until some condition is met which is triggered by what is going on in side the code block.

do while loop

do while loop is a kind of the same as while loop, we do the initialization part outside of the while loop and the increment part in side the code block but it is is different due to small syntax difference. 

Syntax:

variable initialization

do{

   //code block goes here

  variable increment

} while(condition) ;

Example:

int i=0;

do {

    Console.Write(i);

    i++;

} while( i<10)  ;

Output 0123456789

As you can see, the code block always is executed at least once regardless of the condition set in the while statement.

 

Nested Loops

Nested loops are loops that are inside other loop. We call the outside loop outer loop and the inside one inner loop.

We can use any of the above loop type (for, while, do.. while) to create a nested loop. 

The time complexity of nested loops(how many times the body of the inner loop shall be executed) can be calculated using N time M, where N is the size of the outer loop and M is the size of the inner loop.

Nester loop are perfect fit for navigating a multi-dimensional arrays.

Let's see an example:

We want to represent the following table in a two dimensional array and retrieve it back. 

11059
5234
6855

 

            int[,] array = new int[3, 4];

           array[0, 0] = 1;

           array[0, 1] = 10;

           array[0, 2] = 5;

           array[0, 3] = 9;

           array[1, 0] = 5;

           array[1, 1] = 2;

           array[1, 2] = 3;

           array[1, 3] = 4;

           array[2, 0] = 6;

           array[2, 1] = 8;

           array[2, 2] = 5;

           array[2, 3] = 5;

 

           for (int i=0; i<array.GetLength(0); i++)   {

               for(int j=0; j< array.GetLength(1); j++)   {

                   Console.Write(array[i, j] + " " );

               }

               Console.WriteLine();

           }

 

 

 


Tags



Back to Top



Related Blogs






Please fill all fields that are required and click Add Comment button.

Name:*
Email:*
Comment:*
(Only 2000 char allowed)


Security Code:* aeuaul

Back to Top