473,783 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2841
"jvb" <go*****@gmail. com> wrote in message
news:11******** **************@ e56g2000cwe.goo glegroups.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.goo glegroups.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
counterproducti ve.

--
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******** *****@tk2msftng p13.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.goo glegroups.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.WriteLi ne(i)
Next

End Sub

Sub Bar()
Dim i As Integer
i = 1
Do
Console.WriteLi ne(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(int3 2)
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(int3 2)
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.goo glegroups.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.goo glegroups.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.goo glegroups.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
10988
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 on having a "previous" record and a "next" record link on the detail page. This code below works, for the "next" record, by searching the values in the array $myarray for the variable $ref. It then returns the key value and the key value, as a...
19
10645
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 do readline(). Is there a way to do this? for example: while 1: line=stdin.peek_nextline()
2
1512
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: ... i = it.next() ... print i ... if i&3==0: ... print 'skipping next'
7
2500
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 <method-wrapper object at 0x009D1BB0> >>> gen.next is gen.next
3
2638
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) #include <stdlib.h> #include <stdio.h>
5
4074
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 has a field called id_pasivo, which is foreign key to another table called pasivo, a consecutive field called no_cupon and a third field called date. So when i make a query (i.e select * from amortizaciones where id_pasivo = 522 order by...
13
14733
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
1827
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
6547
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 /new_file/prob1.rec started. Please wait.... In while loop of request searching *** glibc detected *** ./a.out: free(): invalid next size (normal): 0x099da890 *** ======= Backtrace: ========= /lib/libc.so.6
13
4875
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 user is redirected to 'hardware_view.php' page and can view the hardware.. my question is.. while the hardware_view.php is loaded, it has to retrieve the first record from the db and has to display the values.. this page contains previous, next...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10315
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10083
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.