C#- Nested loops

Nested loops are those loops that are present inside another loop. In C#, nesting of for, while, and do-while loops are allowed and you can also put any nested loop inside any other type of loop like in a for loop you are allowed to put nested if loop.

for Loop: The functionality of for loop is quite similar to while loop. It is basically used when the number of times loop statements are to be executed is known beforehand. Nesting of for loop is allowed, which means you can use for loop inside another for loop.

Syntax:

for(variable initialization; testing condition; increment / decrement)
{
    for(variable initialization; testing condition; 
                                 increment / decrement)
    {
        // Statements 
    }
}

Example: 

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate nested for loop 
using System; 
  
class GFG{
      
public static void Main() 
      
    // for loop within another for loop 
    // printing GeeksforGeeks 
    for(int i = 0; i < 4; i++) 
        for(int j = 1; j < i; j++) 
            Console.WriteLine("GeeksforGeeks!!"); 
}

chevron_right


Output:



GeeksforGeeks!!
GeeksforGeeks!!
GeeksforGeeks!!

while Loop: In a while loop, the test condition is given at the beginning of the loop, and all statements are executed till the given boolean condition satisfies and when the condition becomes false, the control will be out from the while loop. Nesting of a while loop is allowed, which means you can use while loop inside another while loop. However, it is not recommended to use nested while loop because it is difficult to maintain and debug.

Syntax:

while(condition) 
{
   while(condition)
   {
      // Statements 
   }

  // Statements 
}

Example:

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate nested while loop 
using System; 
  
class GFG{
      
public static void Main() 
    int x = 1, y = 2;
      
    while (x < 4)
    {
        Console.WriteLine("Outer loop = {0}", x);
        x++;
      
        while (y < 4)
        {
            Console.WriteLine("Inner loop = {0}", y);
            y++;
        }
    }
}

chevron_right


Output:

Outer loop = 1
Inner loop = 2
Inner loop = 3
Outer loop = 2
Outer loop = 3

do-while Loop: In C#, the do-while loop is similar to the while loop with the only difference that is, it checks the condition after executing the statements. Nesting of a do-while loop is allowed, which means you can use a do-while loop inside another do-while loop.

Syntax:

do
{
   // Statements
   do 
   {
      // Statements
   }
   while(condition);
}
while(condition);

Example:

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate nested do-while loop 
using System; 
  
class GFG{
      
public static void Main() 
    int x = 1;
    do
    {
        Console.WriteLine("Outer loop = {0}", x);
        int y = x;
      
        x++;
                  
        do
        {
            Console.WriteLine("Inner loop: {0}", y);
            y++;
        } while (y < 4);
  
    } while (x < 4);
}

chevron_right


Output:

Outer loop = 1
Inner loop: 1
Inner loop: 2
Inner loop: 3
Outer loop = 2
Inner loop: 2
Inner loop: 3
Outer loop = 3
Inner loop: 3



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 :

Be the First to upvote.


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