For...Next vs Do...While

J

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.
 
T

Tim Anderson

jvb said:
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
 
M

m.posseth

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]
 
H

Herfried K. Wagner [MVP]

jvb said:
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

m.posseth

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 said:
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 said:
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.
 
L

Larry Lard

jvb said:
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>
 
P

Patrice

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
 
C

Cor Ligthert [MVP]

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
 
H

Homer J Simpson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top