Nested Loops

R

Roshawn

Hi,

I am experimenting with nested For...Next loops. My code looks like this:

Dim i as Byte
Dim itm as Byte

For i = 0 to 9
For itm = 0 to 9
'code omitted
Next
Next

I'd like to know which loop will complete first in this example.

Thanks
Roshawn
 
K

Kevin Hodgson

Your inner loop(itm) will finish execution first. It will be executed 10
times in total, once through for each iteration of the outer loop (i)
 
S

Scott M.

With nested loops, the inner loop always completes first. In fact, with
nested anything, it is always the inner most item that completes first and
then works its way to the outer most element.
 
R

Roshawn

Thank you to all for you prompt responses. I have another question to ask.
Regarding my example code, would writing my inner loop like this cause it
to run once?

For i = 0 to 9
For itm = 0 to 0
'code omitted
Next
Next

Or perhaps its best to call a procedure or function without the need of the
inner loop?

Thanks,
Roshawn
 
K

Kevin Hodgson

That really depends on WHY you're calling this inner loop?

What do you intend to do with the omitted code in the Inner Loop?
 
R

Roshawn

Hi Kevin,

I have a collection of items and I'm trying to number them (specifying my
own numbers). The collection comes from another function. The code would
look like this (forgive me if the code is incorrect):

Function CreateBooks(Byval info as BookInfo) as Collection
dim books as new Collection()
dim i as Byte
For i = 0 to 9
dim book as New Book()
With book
.ISBN = info.ISBN
.Title = info.Title
End With
books.Add(book)
Next
Return books
End Function

Sub NumberBooks(Byval bk as Collection)
Dim i as Byte
Dim itm as Byte

For i = 0 to 9
For itm = 10 to 19 'for example
'here I access each book in bk to set its ID property to itm
Next
Next
End Sub

I've been struggling with this for a couple of days now. Any help will be
appreciated.

Roshawn
 
H

Herfried K. Wagner [MVP]

* "Roshawn said:
I am experimenting with nested For...Next loops. My code looks like this:

Dim i as Byte
Dim itm as Byte

For i = 0 to 9
For itm = 0 to 9
'code omitted
Next
Next

I'd like to know which loop will complete first in this example.

The inner loop.

The outer loop's code (that is the inner loop) will be executed 10
times. In the last step 1 is added to 'i' so 'i''s value is 10. Then
'i' is compared to 9, the comparison evaluates to 'False' and the outer
loop is not executed again.
 
K

Kevin Hodgson

I'm not sure why you're going from 0 to 9 in the outer loop, and from 10 to
19 in your inner loop. Are you using the value i for anything in your
inner loop? Is this the number of books in your collection?

Your inner loop appears to just be your ID number. The loop here doesn't
seem to be necessary if your outer loop is counting out each book in your
collection. Just initialize a variable to your initial ID Number, and
increment it (In your example code, you could simply use (i+10) as your ID
number.

You should probably look into using an enumerator (a For Each) loop on your
book collection, and then assign it an incrementing ID Number, if that's
what you're trying to do.

Something like
Sub NumberBooks(Byval bk as Collection)
Dim Book as BookInfo
Dim i as Byte = 0 (or 10, or whatever your initial ID should be)
For Each Book in bk
Book.ID = i
i=i+1
Next
End Sub
This will loop through your collection of books, and assign an incrementing
ID to each book in the collection, which I believe is what you want to do.

Of course, you could do this in your CreateBooks function as well.
 
S

Scott M.

The inner loop will run 100 times not 10 times.

Kevin Hodgson said:
Your inner loop(itm) will finish execution first. It will be executed 10
times in total, once through for each iteration of the outer loop (i)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top