Home All Groups Group Topic Archive Search About

For...Next vs Do...While

Author
1 Mar 2006 3:40 PM
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.

Author
1 Mar 2006 3:51 PM
Tim Anderson
"jvb" <gome***@gmail.com> wrote in message
news:1141227640.497650.110320@e56g2000cwe.googlegroups.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
Author
1 Mar 2006 3:54 PM
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]

Show quoteHide quote
"jvb" <gome***@gmail.com> schreef in bericht
news:1141227640.497650.110320@e56g2000cwe.googlegroups.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.
>
Author
1 Mar 2006 4:00 PM
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]



Show quoteHide quote
"m.posseth" <poss***@planet.nl> schreef in bericht
news:uiNA1hUPGHA.428@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" <gome***@gmail.com> schreef in bericht
> news:1141227640.497650.110320@e56g2000cwe.googlegroups.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.
>>
>
>
Author
1 Mar 2006 3:56 PM
Herfried K. Wagner [MVP]
"jvb" <gome***@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/>
Author
1 Mar 2006 4:07 PM
Larry Lard
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
Author
1 Mar 2006 4:16 PM
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
--

Show quoteHide quote
"jvb" <gome***@gmail.com> a écrit dans le message de
news:1141227640.497650.110320@e56g2000cwe.googlegroups.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.
>
Author
1 Mar 2006 7:03 PM
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

Show quoteHide quote
"jvb" <gome***@gmail.com> schreef in bericht
news:1141227640.497650.110320@e56g2000cwe.googlegroups.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.
>
Author
3 Mar 2006 5:13 AM
Homer J Simpson
"jvb" <gome***@gmail.com> wrote in message
news:1141227640.497650.110320@e56g2000cwe.googlegroups.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.