444,234 Members | 1,908 Online
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
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" 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" 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" 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" 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] 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