0
votes

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".

2
f(x) is just an example, the for 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 the x variable were modified inside of the loop, it wouldn't affect the number of iterations...ewcz
Please note that f(x) can not return two values when being used in expression list of numeric "for"-loop. For example, the following code function f(x) return x,-1 end; for i=10,f(1) do print(i) end will not print anything.Egor Skriptunoff
The Lua Reference Manual can be clearer by being concise and precise rather than expository like PIL. Use both.Tom Blodget
To my misfortune, the Lua Reference Manual is a nerd that enjoys making me feel stupid and my brain hurt: stat ::= for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end o_Oqubodup
You need to review Lua's basic syntax, be ause as if stands, you don't really have a strong enough knowledge of the foundation of the language to move on to control structures such as loops.warspyking

2 Answers

3
votes

This sentence clarifies it: expressions are evaluated once, before the loop starts.

Thus, f(x) is called only once is merely stating that the expressions will not be affected by potential changes in the loop.

For example, the following code (expressions are i=1 and x in the second line):

x=5
for i=1,x do
  x = x - 1
  print(i, x)
end
print(x)

will produce the following output:

1   4
2   3
3   2
4   1
5   0
0

and will not produce the following output:

1   4
2   3
3   2
2
2
votes

f(x) is just a function which takes the argument x and returns a value that is used as the upper bound for the loop.

So for example, if the function f(x) calculates x² and you call it as f(3), it would return the value of 9. The resulting for loop would look like this:

for i=1, f(3) do print(i) end

which is exactly the same as

for i=1, 9 do print(i) end