473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Nested Loops

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 2015
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
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
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
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
* "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
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
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
Great!!! Thank a lot, Kevin. This is just what I needed.

Roshawn
Nov 21 '05 #9
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
4
by: toddlahman | last post by:
I am using two while loops that are nested. The first loop (post name) returns the full column of results, but the second (post modified) only returns the first row of the column. Is there another...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.