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?
Generic For variables
- airsoftsoftwair
- Posts: 5446
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Generic For variables
Yes.
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.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?
Re: Generic For variables
I was wondering, too, if for-in-variables need to be declared local or not.
So this is same, correct?
No need to declare the variables local first, did I get this right?
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
Re: Generic For variables
Either I don't understand what you say, or they are not behaving accordingly, I have bumped into this problem accidentally before: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.
Code: Select all
n = 5
For n = 1 To 3
DebugPrint(n)
Next
DebugPrint("after: "..n)
1
2
3
after: 4
If they are local to for loop only, then last debugprint should to my understanding print
after: 5
-
- Posts: 475
- Joined: Fri May 15, 2015 5:15 pm
- Location: Waterville, Minnesota USA
Re: Generic For variables
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.
Code: Select all
For key, value In Pairs(var)
Code: Select all
For Local count=1 to 5
I'm on registered MorphOS using FlowStudio.
- airsoftsoftwair
- Posts: 5446
- Joined: Fri Feb 12, 2010 2:33 pm
- Location: Germany
- Contact:
Re: Generic For variables
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.