- Lua Quick Start Guide
- Gabor Szauer
- 190字
- 2021-08-05 10:30:42
else
What happens when none of the if or elseif statements evaluate to true? No chunk of code is executed. But, you might want some chunk of code to execute when none of the if/elseif arguments are true. This is what the else statement does. It executes a chunk of code when none of the statements tested by the preceding if/elseif tests were true.
Syntactically, the else statement is just an else/end chunk. The else statement always comes last, as demonstrated by the following code:
print ("Enter your name")
name = io.read()
if #name <= 3 then
print ("that's a short name, " .. name)
elseif #name <= 6 then
print (name .. " is an average length name")
else
print ("that's a long name, " .. name)
end
There can be only one else statement, and it must be at the end of your if/elseif logic. An else does not have to follow an elseif; it could simply follow an if:
print ("Enter a number")
x = io.read()
if x % 2 == 0 then
print (x .. " is even")
else
print (x .. " is odd")
end
推薦閱讀
- Git Version Control Cookbook
- Java 9 Concurrency Cookbook(Second Edition)
- Vue.js 2 and Bootstrap 4 Web Development
- 樂高機器人設計技巧:EV3結構設計與編程指導
- 蘋果的產品設計之道:創建優秀產品、服務和用戶體驗的七個原則
- Swift 4從零到精通iOS開發
- Java語言程序設計教程
- 深度學習原理與PyTorch實戰(第2版)
- 計算機應用技能實訓教程
- Web編程基礎:HTML5、CSS3、JavaScript(第2版)
- 征服C指針(第2版)
- MongoDB Cookbook
- Unreal Engine Game Development Cookbook
- 陪孩子像搭積木一樣學編程:Python真好玩+Scratch趣味編程(全2冊)
- 羅布樂思開發官方指南:Lua 語言編程