Lesson 6 – Iteration Statements in C#

If you have a piece of code that you want to execute multiple times, iteration statements are the way to go. Iteration statements help you execute a particular piece of code multiple times. Iteration statements are also known as loops. There are three types of iteration statements in C#:

  1. For loop
  2. Foreach loop
  3. While loop

For Loop

If you know the exact number of times that you want to repeatedly execute a piece of code, you can use the for loop. For instance, the following script executes a for loop 10 times and prints the table of 9 on the console.

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {

            int j;

            for (j = 1; j <= 10; j++)
            {
                Console.WriteLine(j * 9);
            }
            
        }
    }
}

In the script above, we execute a for loop 10 times. The variable “j” is initially assigned a value 1. Each loop cycle increments its value. When the value becomes greater than 10, the loop executes. During each iteration, the value of “j” is multiplied with 9. The output of the above script is as follows:

9
18
27
36
45
54
63
72
81
90

Traversing Arrays with the For Loop

The for loop can also be used to print all the elements in an array as shown below:

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] week = new string[7];
            week[0] = "Monday";
            week[1] = "Tuesday";
            week[2] = "Wednesday";
            week[3] = "Thursday";
            week[4] = "Friday";
            week[5] = "Saturday";
            week[6] = "Sunday";

            int j;

            for (j = 0; j < 7; j++)
            {
                Console.WriteLine(week[j]);
            }
            
        }
    }
}

The output of the above script is as follows:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

Foreach Loop

The foreach loop execute is used to traverse through a collection of items. It executes the same number of time as the number of item in a collection. Let’s see how we can use a foreach loop to print all the elements in an array:

 using System;
 namespace C_Project
 {
     class Program
     {
         static void Main(string[] args)
         {string[] week = new string[7];
        week[0] = "Monday";
        week[1] = "Tuesday";
        week[2] = "Wednesday";
        week[3] = "Thursday";
        week[4] = "Friday";
        week[5] = "Saturday";
        week[6] = "Sunday";

        foreach(string day in week)
        {
            Console.WriteLine(day);
        }

    }
}

}

In the output, again you will see 7 days of the week printed on the console.

While Loop

While loop is also used to repeatedly execute a piece of code, however, unlike the forloop which terminates after executing a specific number of times, the while loop keeps executing until a certain condition returns true. Let’s take a look at the while loop in action.

using System;

namespace C_Project
{
    class Program
    {
        static void Main(string[] args)
        {

            int j = 1;

            while(j <=10)
            {
                Console.WriteLine(j * 9);
                j++;
            }

        }
    }
}

In the above script, we have a while loop that prints the table of digit 9. A variable “j” has been initially assigned a value of 1. The while condition checks if the number is less than or equal to 10, if it is less than or equal to 10, the value of “j” is multiplied with 9 and displayed on the console. When the variable “j” becomes greater than 10, the while loop terminates. The output of the above script is as follows:

9
18
27
36
45
54
63
72
81
90

What’s New?

In this lesson, you saw iteration statements in action, in the next lesson you will see how to create functions in C#. Happy Coding!!!