【轉載】Lua腳本語法說明(修訂)

原文:http://www.cnblogs.com/ly4cn/archive/2006/08/04/467550.htmlhtml

 

挑出來幾個spa

.邏輯運算
    and, or, not
    其中,andor 與C語言區別特別大。
    在這裏,請先記住,在Lua中,只有false和nil才計算爲false,其它任何數據都計算爲true,0也是true!
    and 和 or的運算結果不是true和false,而是和它的兩個操做數相關。
    a and b:若是a爲false,則返回a;不然返回b
    a or b:若是 a 爲true,則返回a;不然返回b

    舉幾個例子:
     print(4 and 5) --輸出 5
     print(nil and 13) --輸出 nil
     print(false and 13) --輸出 false
     print(4 or 5) --輸出 4
     print(false or 5) --輸出 5


    在Lua中這是頗有用的特性,也是比較使人混洧的特性。
    咱們能夠模擬C語言中的語句:x == a? b : c,在Lua中,能夠寫成:x == a and b or c。
    最有用的語句是: x = x or v,它至關於:if not x then x = v end
相關文章
相關標籤/搜索