The purpose of the variable after "Next"

Stormin

Nawly Ragistarad Usar
Local time
Today, 20:09
Joined
Dec 30, 2016
Messages
76
Hi all,

So I've wondered this for a while but cannot find any answers on Google due to the keywords being too general.

With a For...Next loop I've always specified my variable after the "Next" statement e.g.
Code:
For i = 1 to 5
    Debug.Print i
Next i
or
Code:
For Each i In myArray
    Debug.Print i
Next i
I know that omitting the variable makes VBA automatically assign it (see here), but is there any advantage/disadvantage to specifying it explicitly?
Personally I find it adds an extra layer of reference but I was wondering if there are any programmatic advantages.

E.g. the following works the same as the first example:
Code:
For i = 1 to 5
    Debug.Print i
Next

Cheers
 
but is there any advantage/disadvantage to specifying it explicitly?...I was wondering if there are any programmatic advantages.

Yes, one advantage is it makes it obvious to the programmers (initial coder and those who follow) what the counter variable in that loop is. Always remember, code isn't just written for computers, it is also written for other humans because they will have to make sense of it at some point. So, write clear code and always comment.

Now, if by "programmatic" you mean makes it run faster/more efficient, the answer is no. Both codes will take the same resources and time to execute.
 
I was thinking more along the lines of are there specific circumstances where you must specify the counter in order for the code to work properly? I can't think of any.

If not I guess the only advantage is, as you say, for the reference/readability.
 
Well, even with nested loops, VBA will presume the most recent "for" statement, but there are some cases where loops aren't based on a "For" statement but still end with a Next. You really should supply the variable if you are into structured programming, and if you have nested loops, you REALLY should supply the index variable as a matter of clarity.
 
Including the loop control variable in the Next is a form of Syntactic Sugar. It serves no purpose from the compiler perspective.

Well, even with nested loops, VBA will presume the most recent "for" statement,

Indeed. A compile error is thrown if anything but the current loop control variable is specified.

but there are some cases where loops aren't based on a "For" statement but still end with a Next.

I can't think of any in VBA. "Next without For" is a familiar compile error. If there were any constructs using Next without For, I would expect it to say "Next without For or something".

You really should supply the variable if you are into structured programming, and if you have nested loops, you REALLY should supply the index variable as a matter of clarity.

I don't include the variable but prefer to rely on indentation to clarify the nested loop constructs. I find not including it simplifies editing of nesting because it doesn't matter which Next line is used. Adding the variable is just another thing that has to match up and a source of potential compile errors.
 
Greg, that may be true for VBA, but there are other languages that use a For/Next construct for which that is NOT a case of syntactic sugar. If it doesn't hurt in VBA but DOES hurt in some other language, you might have a bit of a problem every time you switch languages. And that includes some flavors of BASIC that AREN'T VBA.
 

Users who are viewing this thread

Back
Top Bottom