Loops in C#

Looping in programming language is a way to execute a statement or a set of statements multiple number of times depending on the result of condition to be evaluated to execute statements. The result condition should be true to execute statements within loops.

Loops are mainly divided into two categories:
Entry Controlled Loops: The loops in which condition to be tested is present in beginning of loop body are known as Entry Controlled Loops. while loop and for loop are entry controlled loops.

1. while loop The test condition is given in beginning of loop and all statements are executed till the given boolean condition satisfies, when the condition becomes false, the control will be out from the while loop.



Syntax:

while (boolean condition)
{
   loop statements...
}

Flowchart:
while loop

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate while loop
using System;
  
class whileLoopDemo
{
    public static void Main()
    {
        int x = 1;
   
        // Exit when x becomes greater than 4
        while (x <= 4)
        {
            Console.WriteLine("GeeksforGeeks");
   
            // Increment the value of x for
            // next iteration
            x++;
        }
    }
}

chevron_right


Output:

GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks

2. for loop
for loop has similar functionality as while loop but with different syntax. for loops are preferred when the number of times loop statements are to be executed is known before hand. The loop variable initialization, condition to be tested and increment / decrement of the loop variable is done in one line in for loop thereby providing a shorter, easy to debug structure of looping.

for (loop variable initialization ; testing condition; 
                              increment / decrement)
{    
    // statements to be executed
}

Flowchart:
for-loop-in-java

1. Initialization of loop variable: Th expression / variable controlling the loop is initialized here. It is the starting point of for loop. An already declared variable can be used or a variable can be declared, local to loop only.
2. Testing Condition: The testing condition to execute statements of loop. It is used for testing the exit condition for a loop. It must return a boolean value true or false. When the condition became false the control will be out from the loop and for loop ends.
3. Increment / Decrement: The loop variable is incremented / decremented according to the requirement and the control then shifts to the testing condition again.

Note: Initialization part is evaluated only once when the for loop starts.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate for loop.
using System;
  
class forLoopDemo
{
    public static void Main()
    {
        // for loop begins when x=1
        // and runs till x <=4
        for (int x = 1; x <= 4; x++)
            Console.WriteLine("GeeksforGeeks");
    }
}

chevron_right


Output:


GeeksforGeeks
GeeksforGeeks
GeeksforGeeks
GeeksforGeeks

Exit Controlled Loops: The loops in which the testing condition is present at the end of loop body are termed as Exit Controlled Loops. do-while is an exit controlled loop.
Note: In Exit Controlled Loops, loop body will be evaluated for at-least one time as the testing condition is present at the end of loop body.

1. do-while loop
do while loop is similar to while loop with only difference that it checks the condition after executing the statements, i.e it will execute the loop body one time for sure because it checks the condition after executing the statements.

Syntax :

do
{
    statements..
}while (condition);

Flowchart:
do-while

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate do-while loop
using System;
  
class dowhileloopDemo
{
    public static void Main()
    {
        int x = 21;
        do
        {
            // The line will be printed even
            // if the condition is false
            Console.WriteLine("GeeksforGeeks");
            x++;
        }
        while (x < 20);
    }
}

chevron_right


Output:

GeeksforGeeks

Infinite Loops:
The loops in which the test condition does not evaluate false ever are tend to execute statements forever until an external force is used to end it and thus they are known as infinite loops.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate infinite loop
using System;
  
class infiniteLoop
{
    public static void Main()
    
        // The statement will be printed
        // infinite times
        for(;;)
        Console.WriteLine("This is printed infinite times");
    }
}

chevron_right


Output:

This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
This is printed infinite times
..........

Nested Loops:
When loops are present inside the other loops, it is known as nested loops.


Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate nested loops
using System;
  
class nestedLoops
{
    public static void Main()
    {
        // loop within loop printing GeeksforGeeks
       for(int i = 2; i < 3; i++)
             for(int j = 1; j < i; j++)
                 Console.WriteLine("GeeksforGeeks");
    }
}

chevron_right


Output:

GeeksforGeeks

continue statement:
continue statement is used to skip over the execution part of loop on a certain condition and move the flow to next updation part.
Flowchart:

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate continue statement
using System;
  
class demoContinue
{
    public static void Main()
    {    
        // GeeksforGeeks is printed only 2 times
        // because of continue statement
        for(int i = 1; i < 3; i++)
        {
            if(i == 2)
              continue;
              
            Console.WriteLine("GeeksforGeeks"); 
        }
    }
}  

chevron_right


Output:

GeeksforGeeks


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.