Subscribe by Email


Thursday, May 17, 2012

Explain nested loops in detail?



Why Loops are Important?


- Loops are an extremely important entity when it comes to the programming in languages like C and C++ i.e., to say the object orient languages. 
Loops are used to handle many other program constructs and execute the same statement or a block of statement as many times as required by the program or as specified by the user.
- Loops are either manually included in to the source code of the program or are generated in to the code by the state machine code generators or by an optimizing compiler. 

There are various types of loops but this article is limited only to the discussion regarding the nested loops. Let us see what the nested loops in detail are.
What the phrase “to nest” means?  
It means to keep one object inside the other object.  This holds good when it comes to the context of the loops also. All the types of loops can be nested in to the similar types of loops irrespective of their type.

What are Nested Loops?


- Nested loops are the loops that have been declared inside another loop which may or may not be of the same type as that of the nested loop.
- The loop which is placed inside the another loop is called the nested loop and the loop which holds this nested loop is called the parent loop.
- It is not necessary that the nested loop and the parent loop should be of similar types.
- Nesting the loops falls under the context of combining the looping procedures.
- Whenever a loop is nested, its parent loop is said to take the control of it that is the parent loop decides how many times the inner loop is to be executed. 
- Although the nesting holds good for all the types of loop, it is the for loop that dominates the line. - Once the parent loop has been iterated once, the control is transferred to the inner loop.
- After this the parent loop iterates for a second time only when the execution and iteration of the inner loop is wholly complete. 
- To say it other way round, the inner or the nested loop is triggered by the first pass of the outer or parent loop which is then executed to completion. 
- Then, the inner loop is again triggered by the second pass of the parent loop which is then executed till its complete iteration.
- This process continues till the test condition for the outer parent loop is evaluated to a false value. - If a break statement has been incorporated inside the inner loop then it may come out of the loop before all the iterations of the outer loop are complete. 
- The continue and the break commands are the two commands affecting the behavior of the both nested loops and the outer parent loops. 

Basically the nested loops find their use in the problems that involve working with matrices. Let us illustrate the working of nested with the help of the code written below:

// this code has been written to accept elements in to a 3 x 3 matrix
For ( int r = 0; r < 3; r++)
{
For (int c = 0; c < 3; c++)
{
Scanf(“%d”, &A [r] [c]);
}
}
In the above written c code two loops have been used to accept the elements for a matrix among which one is nested in to the another. The variables r and c denote the rows and columns respectively. Nested loops can be thought of as a logical structures which consist of two repeating statements are placed in the nested that is one inside the other. 


No comments:

Facebook activity