Learn Iteration Statements In C#

Introduction

In C#, iteration statements are used to repeat a block of code several times or until a specific condition is met. The process of repeating a task repeatedly is called looping or iteration. There are four types of iteration statements in C#: For, Foreach, Do, and While. This article teaches how to use these iteration statements in C#.

C# for statement

The for statement in C# is used to execute a block of code a specific number of times. It consists of three parts: the initialization statement, the condition expression, and the iterator statement.

The general syntax of a for the statement in C# is.

for (initialization statement; condition expression; iterator statement)
{
    // code to be executed
}

The initialization statement is executed once before the loop starts and is used to declare and initialize any variables used in the loop.

The condition expression is evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. If the condition is false, the loop terminates.

The iterator statement is executed after each iteration of the loop and is used to modify any variables used in the condition expression.

The following flowchart diagram represents a for loop.

represents forloop
C# for statement example

Here is an example of a for statement that loops through a block of code five times, and each time, it prints a string "The iteration number is" with the count value to the console. In this code, int i=1; is a declaration of a local variable that will participate throughout the loop.The i<=5; is the condition that decides how often iteration will continue.

public class Program {  
    public static void Main() {  
        for (int i = 1; i <= 5; i++) {  
            Console.WriteLine("The iteration number is " + i);  
        }  
  
        Console.ReadLine();  
    }  
}

Output. The output of the above program looks like this

C# foreach statement

The foreach statement in C# is used to iterate over a collection of objects. It provides a simpler and more readable way to loop through the elements of a collection than using a for or while loop.

The general syntax of a foreach statement in C# is.

foreach (type variable in collection)
{
    // code to be executed
}

In this syntax, type is the type of the elements in the collection, the variable is a variable that refers to each element in turn, and collection is the collection of objects being iterated over.

The following flowchart diagram represents a foreach loop.

C# foreach statement example

Here's an example of a foreach statement in C# that iterates over an array of strings and prints each element of the array. The string array daysofWeek has five elements.

public class Program {  
    public static void Main() {  
        string[] daysOfWeek = new string[] {  
            "Monday",  
                "Tuesday",  
                "Wednesday",  
                "Thursday",  
                "Friday"  
        };  
        foreach(string Day in daysOfWeek) {  
            Console.WriteLine("The Day is : {0}", Day);  
        }  
  
  
        Console.ReadLine();  
    }  
}

Output. The output of the above program looks like this.

The foreach statement is recommended on a collection of objects.

C# while statement

The while statement in C# is used to execute a block of code as long as a specific condition is true. It evaluates the condition before each iteration of the loop.

The general syntax of a while statement in C# is:

while (condition)
{
    // code to be executed
}

In this syntax, the condition is the expression evaluated before each iteration of the loop. If the condition is true, the code inside the loop is executed. If the condition is false, the loop terminates, and the program continues with the next statement after the loop.

The following flowchart diagram represents a while loop.

C# while statement example

In the following program, a while statement loop runs until the value of num is less
public class Program {  
    public static void Main() {  
        int num = 1;  
        while (num <= 10) {  
            Console.WriteLine("The number is {0}", num);  
            num++;  
        }  
        Console.ReadLine();  
    }  
}

Output​​​​​​. The output of the above program looks like this

C# do-while statement

The do-while statement in C# is used to execute a block of code at least once and as long as a specific condition is true. It evaluates the condition after each iteration of the loop.

The general syntax of a do-while statement in C# is:

do
{
    // code to be executed
} while (condition);

In this syntax, the block of code inside the do statement is executed at least once, regardless of the value of the condition. After the block of code is executed, the while statement checks if the condition is true. If the condition is true, the loop repeats, and the block of code is executed again. If the condition is false, the loop terminates, and the program continues with the next statement after the loop.

The following flowchart diagram represents a do-while loop:

C# do-while statement example

The following example executes the block of code with the do scope until the value ofnum is less than or equal to 10.

public class Program {  
    public static void Main() {  
        int num = 1;  
        do {  
            Console.WriteLine("The number is {0}", num);  
            num++;  
        } while (num <= 10);  
  
        Console.ReadLine();  
    }  
}

Output. The output of the above program looks like this:

Example. Here is another example of a do-while loop. Here the value of the num variable is 1. The loop will execute until the value of num is less than or equal to 0.

public class Program {  
    public static void Main() {  
        int num = 1;  
        do {  
            Console.WriteLine("The number is {0}", num);  
            num++;  
        } while (num <= 0);  
  
        Console.ReadLine();  
    }  
}  

Output. The output of the above program looks like this.

Summary

There are four iteration statements in C#. This article taught you C# iteration statements, for, foreach, while, and do-while, and how to use these iteration statements in your C# code.


Similar Articles