[Ilugc] more lua

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Sat, 5 Jan 2013 15:03:53 +0530

$ cat sqr.lua

-- This is a single line comment
--[[
    This is a block comment
    that can span any
    number of lines
]]--
print("Script for computing square roots")
for i=2,200,3 do
    print ("Number is ", i)
    print(math.sqrt(i))
end


This is a simple for() loop in lua.

And you can see how easily we can do math and looping in lua.

The syntax of for() loop is,

for var=start,end,step do
   (body)
done

So we can write,

for var=2,20 do
     print(var)
end

in which case the step is 1.

You can also write

for b=20,1,-1 do
      print(b)
end

and so on

Print by default prints a newline.

If you do not want it I guess you need io.write().

-Girish
-- 
Gayatri Hitech
http://gayatri-hitech.com

Other related posts:

  • » [Ilugc] more lua - Girish Venkatachalam