While trying to completely understand the solution to Lua - generate sequence of numbers, the section 4.3.4 of Programming in Lua is unclear:
for i=1,f(x) do print(i) end for i=10,1,-1 do print(i) end
The for loop has some subtleties that you should learn in order to make good use of it. First, all three expressions are evaluated once, before the loop starts. For instance, in the first example, f(x) is called only once. Second, the control variable is a local variable automatically declared by the for statement and is visible only inside the loop. [...]
The first line of code doesn't work of course.
What is f(x)
and where is it defined?
Unfortunately the documentation isn't available as a single page, making it a huge effort to search for the first occurrence. Searching for "lua f(x)" doesn't bear fruit either.
Explanation: now that I have received answers, I realize the problem was a misunderstanding. I incorrectly interpreted "f(x) is called only once" as "the line containing f(x) - for i=1,f(x) do print(i) end
- will only return one value" and didn't pay enough attention to "all three expressions are evaluated once, before the loop starts".
f(x)
is just an example, thefor
statement illustrates: (i) that the upper bound of a for loop can be a "complex" expression (ii) that this upper bound is evaluated before the loop starts which means that even if thex
variable were modified inside of the loop, it wouldn't affect the number of iterations... – ewczf(x)
can not return two values when being used in expression list of numeric "for"-loop. For example, the following codefunction f(x) return x,-1 end; for i=10,f(1) do print(i) end
will not print anything. – Egor Skriptunoffstat ::= for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end
o_O – qubodup