Category: 

What Is a Nested Loop?

Nested loops are logical structures that are used in a wide array of computer programs.
Article Details
  • Written By: C. Martin
  • Edited By: C. Wilborn
  • Last Modified Date: 24 March 2014
  • Copyright Protected:
    2003-2014
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
Artists tend to grow up in wealthier households than doctors.  more...

April 24 ,  2005 :  The world's first cloned dog was born.  more...

A nested loop is a logical structure used in computer programming where two repeating statements are placed in a "nested" form, i.e., one loop is situated within the body of the other. In this structure, the first iteration of the outer loop causes the inner loop to execute. The inner loop then repeats for as many times as is specified. When the inner loop completes, the outer loop is executed for its second iteration, triggering the inner loop again, and so on until the requirements for the outer loop are complete.

Looping, and by extension nested looping, is a powerful construct in programming as it allows rapid sorting or insertion of large amounts of data in an efficient way. Solving problems in the business world often involves repeating an action over and over with hundreds, thousands, or even millions of items of data. Loops are therefore frequently used constructs in all kinds of computer programs in all types of industry.

More than two loops can be nested, and they can go as deep as required. To take an example from the real world that explains the idea, people can consider the car odometer. It is easy to imagine the leftmost digit of the odometer as the outermost loop, and each successive digit as another loop, each within the other, until one reaches the rightmost digit, which represents the innermost nested loop.

Ad

For those programmers working in Structured Query Language (SQL), the nested loop join construct is of particular relevance. This is a means by which one can quickly and efficiently search data in two linked tables. An outer loop is used to read the first table, one row of data at a time. Then the data required to search the second table is passed to the inner loop, which reads the second table. Use this way, it can process two linked tables in an efficient manner.

The method used to set up nested loops varies between programming languages. In SQL, for example, a programmer may script these structures and execute them either as a server nested loop that is called remotely or as one on a client machine that is executed locally. In order to use these powerful structures effectively, it is necessary to take into consideration both the language used by the program and the database or other backend that is being queried for the data.

Ad

Discuss this Article

anon353411
Post 8

Pattern

1

22

333

4444

55555

anon348311
Post 7

1

23

456

78910

anon291711
Post 6

for (int i=6;i>0;i--)

{

for (int j=1;j<=i;j++)

{

Console.Write(j);

}

Console.Write("

");

}

anon271498
Post 4

Use nested loops that print the following patterns:

Pattern I

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

Pattern II

1 2 3 4 5 6

1 2 3 4 5

1 2 3 4

1 2 3

1 2

1

everetra
Post 3

@nony - I think the loop that @Mammmood originally described in his example used a loop array, like a for-next loop, not do-while or anything like that.

The advantage of the for-next loop is that you don’t have to worry about a break point. Once the loop is done iterating through its elements, whether they be 1 to 100 or what have you, you’re done.

nony
Post 2

@Mammmood - Yes, nested loops are used in every language. A C loop would probably use the do while loop construct, where the loop will continue while a certain condition continues to remain true.

A loop VB developers would use to accomplish the same task would be a Do While or Do Until construct, allowing the programmer to specify a condition that continues to be true or one which will define the loop break point, which is where the loop would end.

Mammmood
Post 1

I use nested loops a lot in my programming, and as this article explains, it comes in real handy with SQL. In one loop example I’ve done recently, I had to create a master loop of customer accounts in code, and then inside this loop I created another loop to iterate through orders or some other data that I was trying to retrieve.

The nested loop construct is probably one of the most common algorithms I use in my everyday coding, and I’ve used it in just about every programming language imaginable.

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email