Synopsis
- Introduction
- Nested Looping Definition
- Nested For looping
- Nested While Looping
- Nested Do While Looping
- 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.
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
- class Program
- {
- static void Main(string[] args)
- {
- int i, j;
- for (i = 1; i <= 3; i++)
- {
- Console.WriteLine();
- for (j = 1; j <= 2; j++)
- {
- Console.WriteLine(i+" "+j);
- }
- }
- Console.Read();
- }
- }
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 - class Program
- {
- static void Main(string[] args)
- {
- int i, j;
- i = 1;
- while(i<=3)
- {
- Console.WriteLine();
- j = 1;
- while(j<=2)
- {
- Console.WriteLine(i+" "+j);
- j++;
- }
- i++;
- }
- Console.Read();
- }
- }
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 Program 3
- class Program
- {
- static void Main(string[] args)
- {
- int i, j;
- i = 1;
- do
- {
- Console.WriteLine();
- j = 1;
- do
- {
- Console.WriteLine(i+" "+j);
- j++;
- }while(j<=2);
- i++;
- }while(i <=3);
- Console.Read();
- }
- }
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
- class Program
- {
- static void Main(string[] args)
- {
- int i, j;
- i = 4;
- do
- {
- Console.WriteLine();
- j = 1;
- do
- {
- Console.WriteLine(i+" "+j);
- j++;
- }while(j<=2);
- i++;
- }while(i <=3);
- Console.Read();
- }
- }
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: