Lua : getting started

At very basic Lua syntax is very flexible. Lua can interpret the line ending!! I am serious. For example the code below

local a=6 local b=5 print (a+b)

is exactly same as the code below

local a=6 
local b=5 
print (a+b)

That means we can go ahead writing like the code below too.

local 
a=6 local b
=5 print 
(a+b)

But then comes readability. Its generally better to write code, which we, ourselves can read later too !! So generally one statement per line is a convention.
Now for the declaration of variables, by default any variable declaration is a global variable, unless we define it as a local variable with the ‘local’ keyword as follows.

local a=1
local b='string'

If we remove ‘local’ keyword, the variables are global.
Any variable declaration can contain any kind of values, i mean its not mandatory for a variable to store one kind of values, Lua interpreter can take care of that.
For people who are used to semicolons at the end of the statement, can write that way too and Lua interpreter can take care of that too.

local a=6;
local b=5;
print (a+b);

Writing a for loop in Lua is as below

for i=0,10 do
print (i)
end

Writing a conditional statement is as below

if(a>b) then
print ('a>b')
end

Similarly the if-else would be as below

if(a>b) then
print ('a>b')
end if(a

Finally the way we load different .lua files is through 'require' as follows

require "newFile.lua"

Finally there is a way to start experimenting with Lua without even installing it. This is through the Web Lua project.

With that I think we can get started with this small but powerful language, Lua.

3 thoughts on “Lua : getting started

  1. Pingback: Lua : some basics | saumya

Comments are closed.