Nested Loop, a Loop within a Loop

A nested loop is a loop within a loop.

The first run through of the outer loop triggers the inner loop which loops through until finished. Then the second run through of the outer loop triggers the inner loop again which loops through until finished again. This process will repeat until the outer loop finishes its loop sequence.

The code below has two For Next Loops. Our loop counter variables OuterLoop and InnerLoop both begin at 1 and end at 3 incrementing by 1 each time they pass through their respective Next Statement.
The first run through of the outer loop triggers the inner loop. The inner loop runs through from 1 to 3, executing 3 times. The second run through of the outer loop triggers the inner loop again. This repeats until the outer loop has passed through 3 times.

<%
'Declare variables
Dim OuterLoop, InnerLoop

For OuterLoop= 1 to 3 ' Our For loop will loop through from 1 to 3
For InnerLoop = 1 to 3 ' Our For loop will loop through from 1 to 3
%>
<div><p>
OuterLoop= <%= OuterLoop %> InnerLoop = <%= InnerLoop %>
</p></div>

<%
Next ' increment InnerLoop variable by 1 and repeat the inner loop
Next ' increment OuterLoop variable by 1 and repeat the outer loop
%>

Below is the above code in action:

OuterLoop= 1 InnerLoop = 1

OuterLoop= 1 InnerLoop = 2

OuterLoop= 1 InnerLoop = 3

OuterLoop= 2 InnerLoop = 1

OuterLoop= 2 InnerLoop = 2

OuterLoop= 2 InnerLoop = 3

OuterLoop= 3 InnerLoop = 1

OuterLoop= 3 InnerLoop = 2

OuterLoop= 3 InnerLoop = 3

Get the best asp web hosting provider now and save 30%

Advertisements



MembersPro

MembersPro PayPal - ASP Membership software

Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.