Looping

Could someone please explain to me in simple terms the difference between a For loop and a Do until loop

I have looked in the help but it is not clear to me.

Many thanks

Subject: Looping

Jeff,

My understanding is that you use For when you usually know how many times you are going to loop (ie: For x=1 to 100 or For x=1 to collection.count) whereas Do Until is useful when you have no idea how many loops it might be and you just keep going until you run out of the item (ie: Do Until document is Nothing)

Just my understanding. Would love other people to chip in!!

Mike

Subject: RE: Looping

Something like that. To say the same thing in a slightly different and more long-winded way, For loops repeat an operation a set number of times, although that number might be set within the script. For example, you might get an array, count the members of an array, and go through the loop that many times, the point being that the number of times something is going to happen is set before the loop starts.

Do, on the other hand, runs until some condition is reached, making the number of times it runs unpredictable. To invent a very bad example, consider an agent which checks a web service providing information on weather conditions. The Until condition might be the temperature hitting a certain point. If you start your agent running in late autumn and set it up to inform you when it’s warm enough to go swimming again, the loop could run countless billions of times before it’s done.

Subject: RE: Looping

Also, programming 101 point:

For loops have the execution check at the start, so many never run the block.

Do/Until blocks execute the code once, and then check afterwards whether to do it again.

That’s a real important difference …

HTH

Kiers

Subject: RE: Looping

Do/Until blocks execute the code once, and > then check afterwards whether to do it again.

Depends on where you put the While/Until. This executes at least once:

Do

’ stuff here

Loop Until foo = bar

This, however, may not:

Do Until foo = bar

’ stuff here

Loop

Subject: Looping

Me thinks this is a joke…

Subject: Looping

Isn’t “do until document is nothing” just the same as using the UnprocessedDocuments property?

Subject: RE: Looping

No – Do Until doc Is Nothing just repeats the loop until the variable doc has the value Nothing. It doesn’t know or care where you are getting the value that you put into that variable. It will not automatically fetch the next document out of UnprocessedDocuments – you have to explicitly do something inside the loop to make doc change. Of course you don’t have to use UnprocessedDocuments – you might read the docs from a view instead, or any other place where some docs can be found.