Author |
Topic  |
|
Da_Stimulator
DEV Team Forum Moderator
    
USA
3373 Posts |
Posted - 07 November 2001 : 02:20:13
|
Was wondering what the difference, if any, would be in using one over the other. I know Do has more functionality, but for something like the below, they both produce the same result...
Dim I I = 0 Do until I = 20 'Stuff goes here Loop
Vs
Dim I For I = 0 to 20 'Stuff goes here Next
--------------- -Da_Stimulator Stims Snitz Test area - Running 3.3.03, 4 beta, and Huw's modified code Need a Mod? Check out the Mod Resource |
|
Kat
Advanced Member
    
United Kingdom
3065 Posts |
Posted - 07 November 2001 : 04:45:14
|
Stimmy, interesting question. I always thought for...next loops were more efficient but I can't sya why!! That said however, I always use do...while loops to process recordsets.
Slightly off topic, I did find this interesting information about loops and performance:
quote: IsClientConnected - avoiding stray tasks This is useful to use to ensure that the client is still fetching the page that is currently processing. For example, consider the following classic loop which simply goes through all the records in a recordset:
Do While Not rsTest.EOF
' process and display each record ' etc.... rsTest.MoveNext Loop
Let's say there are 5000 records to process. What if the user stops the page before it is fully finished - the script will continue to run, when the user has long gone! The following rectifies the situation:
Do While (Not rsTest.EOF) And (Response.IsClientConnected)
' process and display each record ' etc.... rsTest.MoveNext Loop
This will loop until end of file or the user selects the stop button. Excellent, just what we want. ;-)
KatsKorner
|
 |
|
HuwR
Forum Admin
    
United Kingdom
20595 Posts |
Posted - 07 November 2001 : 05:02:06
|
The difference between a for loop and a while loop is that a for loop requires you to explicitly define the number of iterations, so in your example there would be 21 iterations of the loop, however your do until I = 20 loop would (as long as you did I = I + 1 in the loop) do 20 iterations, but with a while or repeat loop, you may reset the value of I inside the loop, so say under a certain contition I gets set to 0, so now your do loop will start iterating again from 0.
|
 |
|
paco
Junior Member
 
Spain
187 Posts |
Posted - 07 November 2001 : 06:44:10
|
quote: but with a while or repeat loop, you may reset the value of I inside the loop, so say under a certain contition I gets set to 0, so now your do loop will start iterating again from 0
You can also reset the value of i inside a for loop
for i = 1 to 10
... i = 3 ' infinite loop next
In a for you (can) have all the important loop information on the for statement while in the do loop that information is inside the loop.
I also think that the for loop is faster although with code as simple as Da Stimulator's, compilers will probably end up with the same machine code.
|
 |
|
HuwR
Forum Admin
    
United Kingdom
20595 Posts |
Posted - 07 November 2001 : 06:51:52
|
quote:
You can also reset the value of i inside a for loop
for i = 1 to 10
... i = 3 ' infinite loop next
This is very bad programming practice, and many compiled languages will error if you tried to do this.
|
 |
|
paco
Junior Member
 
Spain
187 Posts |
Posted - 07 November 2001 : 07:12:38
|
Of course it's really bad programming practice. Note I said that you can do it (at least in asp), I didn't say you should do it.
|
 |
|
Doug G
Support Moderator
    
USA
6493 Posts |
Posted - 07 November 2001 : 11:15:18
|
I doubt there is any real difference in performance. Perhaps a very slight loss in the DO loop because of locating a user variable instead of a built-in counter in the FOR loop.
I use whichever makes the code more understandable. Usually a counter index type loop gets a FOR loop, and a testing type loop is a DO loop.
====== Doug G ====== |
 |
|
Da_Stimulator
DEV Team Forum Moderator
    
USA
3373 Posts |
Posted - 07 November 2001 : 14:21:38
|
I tend to use For for arrays and form processing
I tend to use Do for recordsets...
--------------- -Da_Stimulator Stims Snitz Test area - Running 3.3.03, 4 beta, and Huw's modified code Need a Mod? Check out the Mod Resource |
 |
|
Kat
Advanced Member
    
United Kingdom
3065 Posts |
Posted - 08 November 2001 : 04:36:31
|
Me too Stim.
KatsKorner
|
 |
|
wdworld
Starting Member
Canada
24 Posts |
Posted - 09 November 2001 : 03:23:37
|
Basicaly, you use a For Next loop when you know in advance the exact number of passes you need. That's why you use the For next for arrays... because you know hom many items are in it.
When you work with recordsets, the size changes depending of the number of records that match the query, and you don't know this number when you write the code.
Another thing is the 2 variations of the Do loops... If you want to test your condition when you enter the loop, you will use a do while/until... loop. If you want to test it after a 1st pass, you will use a Do... loop while/until. In this case it is sure you will go thru the loop at least one time.
So there are 2 elements to consider...
- Do I know the number of loops i want to do in advance
- Do I want to test when entering the loop or at the end of it
This is the way i understand it 
Charles
|
 |
|
|
Topic  |
|