On Sunday, December 30, 2001, at 02:01 PM, Kevin Ballard wrote:
On 12/30/01 1:25 PM, "Charles Yeomans" <yeomans at desuetude dot com> wrote:
In fact there is. Using a For...Next loop for such situations is a
well-known source of programming errors; I think this is mentioned in
"Code Complete", and there have been numerous "help me" messages on
this
list in which the problem is caused by using a For..Next loop like
this.
Consider the following code.
For i = 0 to WindowCount - 1
Window(i).Close
Next
Will this close all windows?
I still think it's easier to say:
For i = WindowCount - 1 DownTo 0
Window(i).Close
Next
This code is correct, but I had to look at it and think.
instead of:
Do
Window(0).Close
Loop until WindowCount = 0
This snippet isn't so good; what if there are no windows open?
In this case, best is
While WindowCount > 0
Window(0).Close
Wend
It handles extreme cases correctly and it's immediately obvious that
it's correct.
Of course, we could dispense with all three control structures and just
use If..Then and GOTO. The point of having the various loop control
structures is to make it easier to write correct code. The experience
of many developers over time is that using For...Next with endpoints
that change during the loop is prone to error.
Charles Yeomans
|