第一版是針對 Lua 5.0 所撰寫,雖然對於後續版本仍然相當適用,但仍有一些差異。
第四版針對 Lua 5.3,可在 Amazon 及其他書店購買。
購買書籍也能 贊助 Lua 專案


4.3.1 – if then else

if 陳述式會測試其條件,並據此執行其 then-partelse-part。else-part 是可選的。

    if a<0 then a = 0 end
    
    if a<b then return a else return b end
    
    if line > MAXLINES then
      showpage()
      line = 0
    end
撰寫巢狀 if 時,可以使用 elseif。它類似於 else 後接 if,但可以避免需要多個 end
    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