Page 1 of 1
Generic For variables
Posted: Sun Jan 24, 2021 4:29 pm
by lazi
From the generic For description:
For <var1> [, <var2>, ...] In <expr> [Do <stat>] or [<loop-block> Next]
The generic version of the For statement is different from the other two
versions through the fact that it calls a user-defined function to retrieve
the values for each iteration. This fact makes the generic For statement
suitable for a wide variety of purposes.
Is the var1, var2... are always local variables inside the loop?
If I understand correctly those variables need no set to local and they are not write in the variables with the same name.
So they are not really variables. I can use the same variable names outside the loop without confusing anything but the one who want to read the source
Is that correct?
Re: Generic For variables
Posted: Thu Jan 28, 2021 10:31 pm
by airsoftsoftwair
lazi wrote: ↑Sun Jan 24, 2021 4:29 pm
Is the var1, var2... are always local variables inside the loop?
Yes.
lazi wrote: ↑Sun Jan 24, 2021 4:29 pm
If I understand correctly those variables need no set to local and they are not write in the variables with the same name.
So they are not really variables. I can use the same variable names outside the loop without confusing anything but the one who want to read the source

Is that correct?
Yes. Those variables are read-only variables. They are local to the respective for loop and you may only read their value. You must not write to them. And they won't clash with variables that have the same name outside the for loop.
Re: Generic For variables
Posted: Sun Feb 07, 2021 11:41 am
by mrupp
I was wondering, too, if for-in-variables need to be declared local or not.
So this is same, correct?
Code: Select all
For key, paramValue In Pairs(parameters)
<do something>
Next
Code: Select all
Local key, paramValue
For key, paramValue In Pairs(parameters)
<do something>
Next
No need to declare the variables local first, did I get this right?
Re: Generic For variables
Posted: Sun Feb 07, 2021 11:53 am
by Bugala
airsoftsoftwair wrote: ↑Thu Jan 28, 2021 10:31 pm
Yes. Those variables are read-only variables. They are local to the respective for loop and you may only read their value. You must not write to them. And they won't clash with variables that have the same name outside the for loop.
Either I don't understand what you say, or they are not behaving accordingly, I have bumped into this problem accidentally before:
Code: Select all
n = 5
For n = 1 To 3
DebugPrint(n)
Next
DebugPrint("after: "..n)
will result in printing:
1
2
3
after: 4
If they are local to for loop only, then last debugprint should to my understanding print
after: 5
Re: Generic For variables
Posted: Sun Feb 07, 2021 2:52 pm
by SamuraiCrow
The iterator form of the For loop has internal variables. The other form only does if you add the "local" keyword.
has "key" and "value" internally only.
has "count" internally.
Re: Generic For variables
Posted: Sun Feb 07, 2021 9:41 pm
by airsoftsoftwair
mrupp wrote: ↑Sun Feb 07, 2021 11:41 am
No need to declare the variables local first, did I get this right?
Yup. They are by definition always local. Only normal for loops have two different modes, local and non-local, and thus need specification what kind is desired. Generic for loops always use local variables.