Open In App

C#- Nested loops

Improve
Improve
Like Article
Like
Save
Share
Report

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#




// 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!!"); 
}


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#




// 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++;
        }
    }
}


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#




// 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);
}


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


Last Updated : 14 Oct, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads