Looping Statements In C# - Part Two

Synopsis

  1. Introduction
  2. Nested Looping Definition
  3. Nested For looping
  4. Nested While Looping
  5. Nested Do While Looping
  6. Conclusion

Introduction

My previous article explains single looping statement in C#. This article explains nested looping statements. Nested looping is very important for manipulation sets of statements or data in any programming language.

Definition

One looping is insight; another looping is called nested looping. We could call inner looping the ones that are insights of looping and another one called outer looping.

Nested Looping Statement
                                 Figure 1: Nested Looping Statement

Nested For Looping

Nested looping is when one for loop contains another for loop. When outer for loop condition is true then enter inner for loop, otherwise do not enter inner loop. If once we enter inner loop it will come out false of inner loop condition.

Syntax

    for (initialization; condition; increment or decrement) // Outer loop
    {
       for (initialization; condition; increment or decrement) // Inner Loop
       {
          //Statements
       }
    }

Program 1

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         int i, j;  
  6.         for (i = 1; i <= 3; i++)  
  7.         {  
  8.             Console.WriteLine();  
  9.             for (j = 1; j <= 2; j++)  
  10.             {  
  11.                 Console.WriteLine(i+" "+j);  
  12.             }  
  13.         }  
  14.         Console.Read();  
  15.     }  
  16. }  
Output

1 1
1 2

2 1
2 2
3 1
3 2


Explanation

Program 1 executes 6 times because inner loop executes each 2 times based on outer loop so executes six times in total. The first time I value 1 so condition true inner loop execute two times. When inner loo is false then it comes out to outer loop now increment outer loop. I value 2 so condition true again enters inner loop and it will execute two times. Terminate the loop statement when two conditions are false.

Nested While Looping

Nested looping iswhen one while loop contains another while loop. When outer while loop condition is true then enter inner while loop, otherwise do not enter inner loop. If once enter inner loop it will come out after false of inner loop condition.

Syntax

 

    //Initialization;
    while (condition)
    {
       //Initialization;
       while (condition)
       {
          //Statements;
          //Increment or decrement;
       }
       //Increment or decrement;
    }
Program 2
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         int i, j;  
  6.         i = 1;  
  7.         while(i<=3)  
  8.         {  
  9.             Console.WriteLine();  
  10.             j = 1;  
  11.             while(j<=2)  
  12.             {  
  13.                 Console.WriteLine(i+" "+j);  
  14.                 j++;  
  15.             }  
  16.             i++;  
  17.         }  
  18.         Console.Read();  
  19.     }  
  20. }  
Output

1 1
1 2

2 1
2 2
3 1
3 2

Explanation

Program 2 is same as program 1 but syntax wise only it's different. In nested while first is initializating the value, second check the condition and increment value.

Nested Do... While Looping

Nested looping is one Do… while loop contains another do… while loop. When outer do…while loop condition is false or true enter inner do…while loop. If once it enters inner loop it will come out after false of inner loop condition. If outer do… while is false inner do… while execute one set of time.

Syntax
    //Initialization;
    do
    {
       //Initialization;
       do
       {
          //Statements;
          //Increment or decrement;
       }

       while (condition);
       //Increment or decrement;
       } while(condition);

Program 3

  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         int i, j;  
  6.         i = 1;  
  7.         do  
  8.         {  
  9.             Console.WriteLine();  
  10.             j = 1;  
  11.             do  
  12.             {  
  13.                 Console.WriteLine(i+" "+j);  
  14.                 j++;  
  15.             }while(j<=2);  
  16.             i++;  
  17.         }while(i <=3);  
  18.         Console.Read();  
  19.     }  
  20. }  
Output

1 1
1 2

2 1
2 2
3 1
3 2

Explanation

Program 3is the same as program 2 but syntax wise only it's different. I the first time condition is false in do while it executes 2 times then will be terminated.

Program 4
  1. class Program  
  2. {  
  3.     static void Main(string[] args)  
  4.     {  
  5.         int i, j;  
  6.         i = 4;  
  7.         do  
  8.         {  
  9.             Console.WriteLine();  
  10.             j = 1;  
  11.             do  
  12.             {  
  13.                 Console.WriteLine(i+" "+j);  
  14.                 j++;  
  15.             }while(j<=2);  
  16.             i++;  
  17.         }while(i <=3);  
  18.         Console.Read();  
  19.     }  
  20. }  
Output

4 1
4 2

Explanation

IN program  I initialized value 4 so here condition is false but also runs 2 times like above output. Do while first enter and execute inner loop then check the conditions so that it executes 2 times.

Conclusion

This and previous article helps those who are newly learning programming language. This and my previous articles explain how to handle looping statements in different scenarios.

Read more articles on C# Programming:

Up Next
    Ebook Download
    View all
    Learn
    View all