Loading...

Nested Loops in C#

Nested Loops in C#:

Nested Loops has always been a massive problem for students of Computer Programming to learn it. Some of our followers also complain about the lesson of Nested Loop so here is the very easy tutorial of Nested Loop and I am sure that you will definitely understand the concept of nested loops after reading this. 

To understand Nested Loop we have to understand a loop first. A loop is used for iteration or repetition. It means that we can execute a piece of code for as many time as we want without writing it again and again. For Example:


           for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Hello World");  // this code will repeat for 5 times

            }


Now we are able to understand Nested Loop so lets understand that. Nested Loops are just Loop in another Loop, It mean that we place a loop (called inner loop) in another loop body(called outer loop) so that the inner loop Complete its iteration every time time outer loop repeats. Let us help by an example:

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Outer Loop {0}",i);  // this code will repeat for 5 times
                for (int j = 0; j < 2; j++)
                {
                    Console.WriteLine("Inner Loop {0}.{1}",i,j); // this loop will repeat 2 times every time when the outer loop is repeated
                }
            }

Output:



Thank you for Reading, Like this it you Understood. It helps a lot to keep us motivated

1 comments:

Post a Comment

Google+

 
TOP