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

For...Next vs Do...While

P: n/a
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
Share this Question
Share on Google+
8 Replies


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

P: n/a

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

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

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

P: n/a

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

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

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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.