Whereas Python code blocks are controlled by the amount of whitespace on the left margin, Lua has an end keyword to encapsulate blocks and allows free-form coding. If you don’t mind using an extra keyword, and you love being able to put some code on one line, Lua is more writable for you. Readability depends on whether you have a lot of nested blocks, in which case you will want a keyword to signify the end of a block, or if you end up cramming a lot into one line, in which case the reader will be overwhelmed with a long string of tokens. Free-form coding is also a piece of the puzzle that makes full anonymous functions possible in Lua.
Lua has two boolean conditional loops:
while exp do block end
repeat block until exp
Repeat basically means “while not” because it ends the loop when the condition is true.
Lua has a counting loop in this form:
for var = firstnum, lastnum [, step] do block end
Because numbers in Lua are floating point, beware of rounding errors:
> for i = 1, 2, .1 do print(i) end 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 > for i = 9, 10, .1 do print(i) end 9 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10 > for i = 1, 2 do print(i) end 1 2
A for-loop that can iterate over a table is described on this site and is quoted:
Lua provides a pairs() function to create the explist information for us to iterate over a table. The pairs() function will allow iteration over key-value pairs. Note that the order that items are returned is not defined, not even for indexed tables.
Lua > for key,value in pairs(t) do print(key,value) end 3 10 1 3 4 17 2 7 pi 3.14159 banana yellow
The ipairs() function will allow iteration over index-value pairs. These are key-value pairs where the keys are indices into an array. The order in which elements are returned is guaranteed to be in the numeric order of the indices, and non-integer keys are simply skipped. Using the same table as in the example above:
Lua > for index,value in ipairs(t) do print(index,value) end 1 3 2 7 3 10 4 17
Notice how only the array part of the table is displayed because only these elements have index keys.
The counting loop in Lua may be more writable than using range() in Python, but not definitely.