In real life, we need to do some repetitive tasks, and that too multiple times, and doing the same task, again and again, is quite a hectic one. In the case of programming, there are situations where you have to write a program statement or execute a set of logic multiple times or display a message with a slight variation in numbers. So, doing this form of work manually will be time-consuming and less effective. In this chapter, you will know about the effective way or concept through which you can iterate multiple lines in a C# program.



What Are Loops in C#?

Loop is a concept used in almost all Programming languages for executing a single statement or collection of statements several times, depending on the condition.

The loops in C# are basically of two types depending on their behavior. These are:
  • Entry controlled loop
  • Exit controlled loop

Entry Controlled Loops

When the looping condition is checked at the very beginning of the loop body and before executing the loop block, those looping statements are termed as entry controlled loops. There are two types of looping statements provided by C# that is entry controlled. These are:

while loop in C#

In this looping statement, the test condition is given at the very beginning before the execution gets inside the loop block. The iteration will continue until the condition becomes false.

Syntax:

while (boolean conditional expression)
{
        // block of loop statements
}

Example C# Program to Demonstrate while Loop

Example:

using System;   
class IterateWithWhile
{ 
    public static void Main() 
    { 
        int cntr = 1; 
        while (cntr <= 5) 
        { 
            Console.WriteLine(" Hello World "+cntr); 
           cntr++; 
        } 
    } 
}

Output:

 Hello World 1
 Hello World 2
 Hello World 3
 Hello World 4
 Hello World 5

for loop in C#

"For loop" is another entry controlled looping statement provided by C#, which also iterates in a program but has a different syntax. Here in the same line, the counter variable is initialized, then the looping state is checked, and increment/subtraction is performed, and a semi-colon separates each of these expressions.

Syntax:

for (initialization of counter variable; test the condition of looping; increment / decrement)
{    
        // block of loop statements
}

Example C# Program to Demonstrate for Loop

Example:

using System;   
class IterationWithFor 
{ 
    public static void Main() 
    { 
       for (int cntr = 1; cntr <= 5; cntr++) 
            Console.WriteLine(" Hello world "+cntr); 
    } 
}

Output:

 Hello World 1
 Hello World 2
 Hello World 3
 Hello World 4
 Hello World 5

Exit Controlled Loops in C#

When the looping condition is checked at the end of the loop body and right after executing the loop block (at least once), those types of looping statements are termed as exit controlled loops. C# provides only one exit controlled looping statement, and that is the do-while loop.

do-while loop in C#

This loop is similar to the while loop, except the looping condition is checked at the very end of the loop block, which means at least once the looping block will be executed and evaluated irrespective of the condition.

Syntax:

do
{
        // block of loop statements
} while (condition);

Example C# Program to Demonstrate do-while Loop

Example:

using System; 
class IterateWithDoWhile 
{ 
    public static void Main() 
    { 
        int cntr = 11; 
        do
        { 
            Console.WriteLine(" Hello world "+cntr); 
            cntr++; 
        } 
        while (cntr < 10); 
    } 
}

Output:

Hello world 11

Infinite Loops

There are other types of a loop where the condition will not evaluate to false. Hence, the iteration goes on and on forever until an external agent or an external potential is used to stop this endless iteration forcefully. These types of loops are called infinite loops. Here is a simple example of an infinite loop in C#.

Example:

using System;   
class GoWithInfiniteLoop 
{ 
    public static void Main() 
    {  
       for( ; ; ) 
        Console.WriteLine(" Hello world"); 
    } 
}


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram