Nested loops in C# programming

Loop can be used inside loop in any programming language including C#. Such loops are known as nested loops.

Nested for Loop in C#

Syntax for Nested for Loop in C#:

for (initialization; condition; increment/decrement)
{
    for (initialization; condition; increment/decrement)
    {
        statements;
    }
    statements;
}

Example 1: C# program of Nested for Loop

C# program to print triangle pattern of first five natural numbers using nested for loop.

using System;
namespace loop
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 5; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

In this program, the outer loop run 5 times. In each iteration of outer loop, the inner loop runs i times. Notice that, the value of i is different for every iteration of outer loop. In above program, the inner loop runs only once in first iteration of outer loop, twice in second iteration of outer loop and so on until i is incremented to 6. When i is 6, the loop terminates.

Output

1
12
123
1234
12345

Nested while Loop in C#

Syntax for Nested while Loop in C#:

while (condition)
{
    while (condition)
    {
        statements;
    }
    statements;
}

Example 2: C# program of Nested while Loop

C# program to print triangle pattern of first five natural numbers using nested while loop.

using System;
namespace loop
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 5;
            while (i >= 1)
            {
                int j = 5;
                while (j >= i)
                {
                    Console.Write(j);
                    j--;
                }
                i--;
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}

Similarly, in this program, the outer loop runs for a total of 5 times. In each iteration of the outer loop, the inner loop runs 5-i+1 times. Here too, the value of is different for every iteration of outer loop. The inner loop runs once in first iteration of outer loop, twice in second iteration of outer loop and so on until i is decremented to 0. When i is 0, the loop terminates.


Output

5
54
543
5432
54321

Nested Do While Loop in C#

Syntax for Nested Do While Loop in C#:

​do
{
    do
    {
        statements;
    }while (condition);
    statements;
}while(condition);

Example 3: C# program for Nested do...while Loop

C# program to print triangle pattern of character '*' using nested do while loop.

using System;
namespace loop
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = 5;
            do
            {
                int space = i;
                do 
                {
                    Console.Write(' ');
                    space--;
                }while (space >= 1);
                int j = 5;
                do
                {
                    Console.Write("* ");
                    j--;
                }while(j >= i);
                Console.WriteLine();
                i--;
            }while(i >= 1);
            Console.Read();
        }
    }
}

Again, In this program, the outer loop run 5 times. In each iteration of outer loop, the first inner loop runs times and second inner loop runs 5-i+1 times. The value of is different for every iteration of outer loop. The first inner loops and second inner loop run 5 times and one time in first iteration, 4 times and 2 times in second iteration of the outer loop respectively and so on until i is decremented to 0. When i is 0, the loop terminates.

Output

    *
   * *
  * * *
 * * * * 
* * * * *