Lua Programming/How to Lua/whitespace

The lua programming language uses a freeform syntax. This means that whitespace characters, such as space, tab characters and newline characters can be inserted into the program code for the purpose of spacing, alignment or indentation in either the horizontal or vertical directions.

Line Breaks and IndentationEdit

The use of indentation and linebreaks can greatly improve the readability of the code, without affecting the way that the code performs. Using whitespace appropriately, makes the code easier to read and follow because at a glance it is very apparent where functions and procedures end, and which lines are part of which loops and procedures:

!!Example need here

Whitespace characters are ignored by the interpreterEdit

The whitespace characters do not change the functionality of the code, and are ignored by the lua interpreter. The following lines of code execute as a single statement:

print
"hello"

StringsEdit

As with other programming languages, space, tab, and newline characters within a quoted literal string do not form whitespace, and are treated as part of the string:

print "hello    bob"

Line breaks are not permitted within a literal stringEdit

In lua, a line break is not permitted within a literal string:

-- This will produce an unfinished string error
print "hello
bob"

Whitespace is not permitted within an identifier nameEdit

It is not permissible to use whitespace in identifer names used for variables or functions:

-- function names cannot contain whitespace
function my function(myparameter)
end

It is permissible to use underscore symbols in identifier names:

function my_function(myparameter)
end
Last modified on 8 March 2011, at 17:36