[LUA] Chap 4. sentence (문장)

[예제]
--#assignment, multiple assignment
a, b, c = 0, 1
print(a, b, c)
a, b = a+1, b+1, b+2
print(a, b)
a, b, c = 0
print(a, b, c)
--#local, global variable. global is default.
--chunk means block.
--local is valid only one chunk.
a=2
b=4
c=6
d=8
do
  local a2 = 2*a
  local d = (b^2-4*a*c)^(1/2)
  x1 = (-b + d)/a2
  x2 = (-b - d)/a2
 end

 print(x1, x2)

 --Lua -> everything is true except false, nil.(0 is true, ""(empty string) is true 
 
 
 
[결과]
0 1 nil
1 2
0 nil nil
1.#QNAN 1.#QNAN








[예제]
 a=2
b=4
c=6
d=8
 --#if then else
 if a < 0 then a = 0 end

 if a < b then
 -- return a
 else
 -- return b
 end
   
  op = "+"     
  if op == "+" then
    r = a+b   
  elseif op == "-" then
    r = a-b   
  elseif op == "*" then
    r = a*b
  elseif op == "/" then
    r = a  / b
  else
    error("invalid operation")
  end
  print(r)
 
  kbs={}
 
  --#while
  local k = 1
  while kbs[k] do
    print(kbs[k])
    k = k + 1
  end
   
  --#repeat
  x = 100
  local sqr = x/2
  repeat
    sqr = (sqr + x/sqr)/2
    local error = math.abs(sqr^2 - x)
   until error < x/10000 
 
  --#numeric for
  for i=1, math.huge do
    if (0.3*i^3 - 20*i^2 - 500 >= 0) then
      print(i)
      break
    end
   end
  
   --#general for  key -> index(1, 2, 3, ... n)  
   --for key, value in paris(table) do print(key, value) end
   days = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"}
   revDays = {["sunday"] = 1, ["monday"] = 2, ["tuesday"] = 3, ["wednesday"] = 4, ["thursday"] = 5, ["friday"] = 6, ["saturday"] = 7}
   x = "tuesday"
   print(revDays[x])
  
   revDays100 = {}
   for k, v in pairs(days) do
    revDays100[v] = k
    print(revDays100[v])
   end
  
   --do return end  -> 특정 부분이 실행되지 않ㄷ록 함.
  
  

!!CAUTION : goto is valid after LUA 5.2
    --goto state machine
   
    ::s1:: do
      local c = io.read(1)
      if c == '0' then goto s2
      elseif c == nil then print 'ok'; return
      else
        goto s1
      end
    end
     
    ::s2:: do
      local c = io.read(1)
      if c == '0' then goto s1
      elseif c == nil then print 'not ok'; return
      else goto s2
      end
    end
   
    goto s1
   
   
 
 
[결과]
6
68
3
1
2
3
4
5
6
7
1
2
2
3
4
5
[LUA] Chap 4. sentence (문장) [LUA] Chap 4. sentence (문장) Reviewed by kukanuc on 3월 07, 2019 Rating: 5

댓글 없음:

Powered by Blogger.