By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

Nested Loops

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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)
"Roshawn" <ud****@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #2

P: n/a
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.
"Roshawn" <ud****@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #3

P: n/a
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

Nov 21 '05 #4

P: n/a
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?

"Roshawn" <ud****@bellsouth.net> wrote in message
news:eN*************@TK2MSFTNGP12.phx.gbl...
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?

Nov 21 '05 #5

P: n/a
* "Roshawn" <ud****@bellsouth.net> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #6

P: n/a
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
Nov 21 '05 #7

P: n/a
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.
"Roshawn" <ud****@bellsouth.net> wrote in message
news:uH*************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #8

P: n/a
Great!!! Thank a lot, Kevin. This is just what I needed.

Roshawn
Nov 21 '05 #9

P: n/a
you should read up on scoping (scope of code blocks) rules, it will help you
a lot
"Roshawn" <ud****@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #10

P: n/a
The inner loop will run 100 times not 10 times.

"Kevin Hodgson" <ke***@caseware.com> wrote in message
news:uo**************@TK2MSFTNGP12.phx.gbl...
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)
"Roshawn" <ud****@bellsouth.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.