Codefixer - ASP tutorials, resources and software
ASP.NET Hosting with US & UK Data Centers!
Home   Articles   Resources   ASP directory   ASP Tutorials   Code Snippets   ASP Applications   Forum
Monday 6 October
MembersPro PayPal - ASP Membership software

USA & Europe Data Centers – DiscountASP.NET


ASP Partner ASP Partner ASP Partner more partners...

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





If you have any code snippets to share with full credit given then send an email to Codesnippets - You'll receive full credit and a link back to your site.

ASP.NET 3.5/2.0 Web Hosting: 3 Months FREE – Click Here!




About | Contact | Advertise | Feedback | Hire Us | Link

Site developed by Michael Wall - Web Design Belfast N.Ireland.
Copyright © 2000-2008. All rights reserved.

Do you intend to move to ASP.NET or have you already?
Yes will do
Have done
ASP does fine
Not a priority


Directory Software