【Lua 筆記】條件控制 - part 3 === 目錄(Table of Contents): [TOC] --- 由於有款遊戲叫做 CSO(Counter-Strike Online),內建模式創世者模式(Studio)新增使用 Lua 及其遊戲的 API,所以突發奇想製作這個筆記。 這個筆記會在一開始先著重純粹的程式設計自學,在最後的章節才會與 CSO 遊戲 API 進行應用。 條件控制(Condition) --- 條件控制,大多程式語言所用到的關鍵字為「if」。 if 就是如果的意思,為什麼要用到條件控制呢?舉個例子:"如果"今天下雨,那麼我就帶雨傘。 那"如果"今天不下雨,"否則"我就不帶雨傘。 如果是關鍵字,否則同屬之。-> if、else 關鍵字 從上面例子可以看出來,"如果"今天下雨這個條件不成立,也就是今天不下雨,則會觸發"否則"這個條件。 在進入正式的語法之前,先來個重要的觀念: > 控制結構的條件式結果可以是任何值,Lua 認為 false 和 nil 為假,true 和非 nil 為真(非常重要!!!!)。要注意的是 Lua 中 0 為 true: ```lua= --[ 0 為 true ] if (0) then print("0 為 true") end ``` 輸出結果: ``` 0 為 true ``` lua 當中最特別的就是 0,是 true,***因為不是 nil 或 false 的都是表示 true。*** ### 回到正題,以下是 if 語句的結構定義(基本格式): ```lua= if (Boolean_Expression) then --[ If Boolean_Expression is true, then execute here. --] end ``` 意思是說,如果 Boolean_Expression == true 這個條件成立(表示 Boolean_Expression 是 true)的話,則執行 then 以下,end 以上的程式碼區塊。 註:Boolean_Expression 稱為布林運算式,也就是經過運算過後的值會是布林值(true、false),這邊小補充一下,通常我們會說運算過後「回傳」的值。 ```lua= a = 10 if (a == 10) then print("a = 10.") end ``` 輸出結果: ``` a = 10. ``` 以上程式碼是說,如果確實 a == 10,則執行 then ~ end 的內容。 可以看到我們為什麼要學習邏輯運算子,原因出在此,日後當我們在寫程式時,會有更多的條件判斷需要用到邏輯運算。 另外各位也可以驗證 (0) 是否真的就是 true: ```lua= if (0) then print("Condition is true") end ``` 輸出結果: ``` Condition is true ``` ### if... else 語句 --- ```lua= if (Boolean_Expression) then --[ If Boolean_Expression is true, then execute here. --] else --[ If Boolean_Expression is false, then execute here. --] end ``` 多了一個 else,什麼意思呢?就是上面的 if 裡面的 Boolean_Expression 條件不成立,也就是 false 的時候,才會執行 else 下面的程式碼區塊。 ```lua= a = 100 if ( a < 20 ) then print("a < 20" ) else print("a > 20" ) end print("a is : ", a) ``` 輸出結果: ``` a > 20 a is : 100 ``` 在這邊插入一則提醒:Lua 跟 C / C++ 一樣,是不講究縮排的程式語言,也與 Python 不同。所以可以這樣寫: ``` a = 100 if( a < 20 ) then print("a < 20" ) else print("a > 20" ) end print("a is : ", a) ``` 只不過為了可讀性,還是建議各位縮排(就是按下 tab)啦~~ ### if... else if... else 語句 --- ```lua= if (Boolean_Expression_1) then --[ If Boolean_Expression_1 is true, then execute here. --] elseif (Boolean_Expression_2) then --[ If Boolean_Expression_2 is true, then execute here. --] elseif (Boolean_Expression_3) then --[ If Boolean_Expression_3 is true, then execute here. --] else --[ If Boolean_Expression_1~3 is not true, then execute here. --] end ``` 多了個 elseif,也就是否則如果的意思,要執行到 elseif 的語句,則需要 elseif 前面的條件語句不成立,也就是前面那個條件要 false。 ```lua= a = 100 if (a == 10) then print("a is 10") elseif (a == 20) then print("a is 20") elseif (a == 30) then print("a is 30") else print("Not matching value") end print("a real value is : ", a) ``` 輸出結果: ``` Not matching value a real value is : 100 ``` 可以從上面範例程式碼看到: * 第一個條件語句(if),判斷 a 是否等於 10,發現不是,則走下一個 elseif 語句。 * 第二個條件語句(elseif),判斷 a 是否等於 20,發現不是,則走下一個 elseif 語句。 ... 就這樣直到 else,因為上面條件都一一不成立,也就是 false,印出 Not matching value。 ### 巢狀 if --- ```lua= if (Boolean_Expression_1) then --[ If Boolean_Expression_1 is true, then execute here. --] if (Boolean_Expression_2) then --[ If Boolean_Expression_2 is true, then execute here. --] end end ``` ```lua= a = 100 b = 200 if (a == 100) then if (b == 200) then print("a is 100;b is 200") end end print("a is :", a) print("b is :", b) ``` 輸出結果: ``` a is 100;b is 200 a is : 100 b is : 200 ``` 以上程式碼也可以寫成這樣(邏輯運算子與關係運算子的應用): ``` a = 100 b = 200 if (a == 100 and b==200) then print("a is 100;b is 200") end print("a is :", a) print("b is :", b) ``` 總結 --- 條件語句共有以下三種: * if * if... else * if... elseif... elseif... else 每個條件語句的基本格式都需要在 boolean_expression 的右邊放上 then,且在結尾的時候放上 end。 像這樣: ```lua= if (boolean_expression) then -- [ 放點東西來執行 --] end ``` boolean_expression 即布林運算式,又可稱為布林表達式,因為 expression 有表達的意思。 運算式複習:運算元+運算子。 e.g. a == b 回傳的值如果是以布林值表示(true 或 false 就是布林值),假設 a = 1、b = 1,而經過 a == b 的運算過後,回傳的值會是 true。 參考資料 --- * [Lua if..else 語法 · Lua 基礎使用教程](https://j13tw.gitbooks.io/lua/content/lua-ifelse-yu-fa.html) * [Lua 流程控制 | 菜鸟教程](https://www.runoob.com/lua/lua-decision-making.html)