473,320 Members | 1,713 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,320 software developers and data experts.

For...Next vs Do...While

jvb
Hey all,

I figure it's Wednesday, why not put a question up for debate. Beyond
personal preference, is there any benefit (performance or otherwise) to
using one loop over the other? For example, I remember in hearing in
class (many years ago...) that VB compiles For...Next loops as
Do...While loops.

Mar 1 '06 #1
8 2813
"jvb" <go*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Hey all,

I figure it's Wednesday, why not put a question up for debate. Beyond
personal preference, is there any benefit (performance or otherwise) to
using one loop over the other?


I would go for the one that most naturally expresses what the loop is for.

Tim
Which dynamic language will win the Enterprise?
http://www.itwriting.com/blog/?postid=354
Mar 1 '06 #2

well i believe that it depends on the situation wich one to choose

however as a general rule i avoid While .... End While Loops as the
plague :-)
as it`s modern brother the Do .. Loop as it is much more flexible
regards
Michel Posseth [MCP]

"jvb" <go*****@gmail.com> schreef in bericht
news:11**********************@e56g2000cwe.googlegr oups.com...
Hey all,

I figure it's Wednesday, why not put a question up for debate. Beyond
personal preference, is there any benefit (performance or otherwise) to
using one loop over the other? For example, I remember in hearing in
class (many years ago...) that VB compiles For...Next loops as
Do...While loops.

Mar 1 '06 #3
"jvb" <go*****@gmail.com> schrieb:
Beyond personal preference, is there any benefit (performance or
otherwise) to
using one loop over the other? For example, I remember in hearing in
class (many years ago...) that VB compiles For...Next loops as
Do...While loops.


I suggest to set up some test code and use "ILDASM.EXE" to check the IL code
emitted by the compiler for different loop constructs. However, the chosen
loop type should fit semantically. Micro-optimizations like choosing one
loop type over another because of an unnoticeable performance gain will
reduce maintenability of the source code and thus is often
counterproductive.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Mar 1 '06 #4
correction :

as his brother the Do .. Loop is much more flexible
:-)
sorry me no native englis speaking :-)
okay you got the point i hope ....

regards

Michel Posseth [MCP]

"m.posseth" <po*****@planet.nl> schreef in bericht
news:ui*************@tk2msftngp13.phx.gbl...

well i believe that it depends on the situation wich one to choose

however as a general rule i avoid While .... End While Loops as the
plague :-)
as it`s modern brother the Do .. Loop as it is much more flexible
regards
Michel Posseth [MCP]

"jvb" <go*****@gmail.com> schreef in bericht
news:11**********************@e56g2000cwe.googlegr oups.com...
Hey all,

I figure it's Wednesday, why not put a question up for debate. Beyond
personal preference, is there any benefit (performance or otherwise) to
using one loop over the other? For example, I remember in hearing in
class (many years ago...) that VB compiles For...Next loops as
Do...While loops.


Mar 1 '06 #5

jvb wrote:
Hey all,

I figure it's Wednesday, why not put a question up for debate. Beyond
personal preference, is there any benefit (performance or otherwise) to
using one loop over the other? For example, I remember in hearing in
class (many years ago...) that VB compiles For...Next loops as
Do...While loops.


Depending on how many years ago it was, the subject in question may
have been VB6 or earlier, about which compiler questions are too much
work (for me) to answer. However, through the wonders of ILDASM, this
question is easy for VB.NET

VB:
Sub Foo()
Dim i As Integer
For i = 1 To 3
Console.WriteLine(i)
Next

End Sub

Sub Bar()
Dim i As Integer
i = 1
Do
Console.WriteLine(i)
i = i + 1
Loop While i <= 3

End Sub

IL:
..method public static void Foo() cil managed
{
// Code size 21 (0x15)
.maxstack 2
.locals init ([0] int32 i)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0
IL_0003: ldloc.0
IL_0004: call void [mscorlib]System.Console::WriteLine(int32)
IL_0009: nop
IL_000a: nop
IL_000b: ldloc.0
IL_000c: ldc.i4.1
IL_000d: add.ovf
IL_000e: stloc.0
IL_000f: ldloc.0
IL_0010: ldc.i4.3
IL_0011: ble.s IL_0003
IL_0013: nop
IL_0014: ret
} // end of method Module1::Foo

..method public static void Bar() cil managed
{
// Code size 21 (0x15)
.maxstack 2
.locals init ([0] int32 i)
IL_0000: nop
IL_0001: ldc.i4.1
IL_0002: stloc.0
IL_0003: nop
IL_0004: ldloc.0
IL_0005: call void [mscorlib]System.Console::WriteLine(int32)
IL_000a: nop
IL_000b: ldloc.0
IL_000c: ldc.i4.1
IL_000d: add.ovf
IL_000e: stloc.0
IL_000f: ldloc.0
IL_0010: ldc.i4.3
IL_0011: ble.s IL_0004
IL_0013: nop
IL_0014: ret
} // end of method Module1::Bar

As you can see, Foo has a nop *before* the WriteLine call, whereas Bar
has a nop *after* the WriteLine call. So... they're different, right?
:)

As someone else said, use the one that makes the most sense. All loops
are really just:

<loop init>
<start>
<loop start action>
....
<loop end action>
<conditional jump to start>

--
Larry Lard
Replies to group please

Mar 1 '06 #6
For..Next is when you know how many iterations you'll need.

Do..While is when you want to iterate while (or until) a condition is met.

It's unlikely you'll see any significant difference here. Just pick the
appropriate one.

If you have some kind of performance problem, start by measuring how much is
spent in each part (for example avoid repeating something in the loop that
you could do once for all before entering the loop).

Patrice
--

"jvb" <go*****@gmail.com> a écrit dans le message de
news:11**********************@e56g2000cwe.googlegr oups.com...
Hey all,

I figure it's Wednesday, why not put a question up for debate. Beyond
personal preference, is there any benefit (performance or otherwise) to
using one loop over the other? For example, I remember in hearing in
class (many years ago...) that VB compiles For...Next loops as
Do...While loops.

Mar 1 '06 #7
jvb,

I look forever which one needs the less characters to type.

What important is it how it is compiled, every solution is more than fast
enough.

:-)

Cor

"jvb" <go*****@gmail.com> schreef in bericht
news:11**********************@e56g2000cwe.googlegr oups.com...
Hey all,

I figure it's Wednesday, why not put a question up for debate. Beyond
personal preference, is there any benefit (performance or otherwise) to
using one loop over the other? For example, I remember in hearing in
class (many years ago...) that VB compiles For...Next loops as
Do...While loops.

Mar 1 '06 #8

"jvb" <go*****@gmail.com> wrote in message
news:11**********************@e56g2000cwe.googlegr oups.com...
Hey all,

I figure it's Wednesday, why not put a question up for debate. Beyond
personal preference, is there any benefit (performance or otherwise) to
using one loop over the other? For example, I remember in hearing in
class (many years ago...) that VB compiles For...Next loops as
Do...While loops.


Actually they all eventually wind up as goto's. Look at an assembly dump
sometime.


Mar 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: george | last post by:
Can anyone help? I query a database and return a result on the column "reference". There might be 7 listings. Each row is displayed in a table, with links through to a detail page. I am working...
19
by: les_ander | last post by:
Hi, suppose I am reading lines from a file or stdin. I want to just "peek" in to the next line, and if it starts with a special character I want to break out of a for loop, other wise I want to...
2
by: Bengt Richter | last post by:
Is this a well known bug that's been fixed? I couldn't find any discussion of it, but maybe my googling's off today ;-/ >>> def foo(): ... it = iter(range(10)) ... while True: ... ...
7
by: simonwittber | last post by:
>>> gen = iterator() >>> gen.next <method-wrapper object at 0x009D1B70> >>> gen.next <method-wrapper object at 0x009D1BB0> >>> gen.next <method-wrapper object at 0x009D1B70> >>> gen.next...
3
by: Francois Grieu | last post by:
Given that next is the first field in struct node, and head is a pointer to node, does assigning ((node*)&head)->next safely assign head ? Illustration (this code works on many platforms)...
5
by: robecflo | last post by:
Hi Forum, i have a problem, hope somebody can give me ideas. I'm developing with windows forms and vb.net, and oracle as a database. At this moment i have a table called amortizaciones, this table...
13
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
3
by: PointMan | last post by:
what i know is... while ((currentStr = sr.ReadLine()) != null) { in this area, can i get next ReadLine Value of currentStr } best regard,,
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
13
by: sweetline priya | last post by:
in my project, a table called 'hardware' contains merely 5000 records..its a mysql database.. the user can add, modify or can view this hardware details one by one.. while clicking 'view' button, the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.