- Lua Quick Start Guide
- Gabor Szauer
- 229字
- 2021-08-05 10:30:42
elseif
You might want to make a more complicated decision than a simple if statement allows. For example, you may want to do one thing if the length of a string is less than three, but another thing if the length of a string is greater than three AND less than six! This can be achieved with the elseif statement.
Unlike other languages, there is no space between else and if; it's one keyword: elseif.
An elseif must always follow an if. Syntactically, an elseif statement is followed by a Boolean condition which is then followed by a then/end block. The elseif statement follows the body of the then block of an if statement, but goes before the end statement. The following code demonstrates the syntax of an elseif statement:
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")
end
You can add as many elseif statements to one if statement as you want. The following code example demonstrates this:
print ("Enter a number")
x = io.read()
if x == "0" then
print ("input is 0!")
elseif x == "1" then
print ("input is 1!")
elseif x == "2" then
print ("input is 2!")
elseif x == "3" then
print ("input is 3!")
end
推薦閱讀
- DevOps:軟件架構(gòu)師行動指南
- .NET之美:.NET關(guān)鍵技術(shù)深入解析
- LabVIEW程序設(shè)計基礎(chǔ)與應(yīng)用
- Building a Home Security System with Raspberry Pi
- 無代碼編程:用云表搭建企業(yè)數(shù)字化管理平臺
- Visual Basic程序設(shè)計實驗指導(dǎo)(第4版)
- 可解釋機器學(xué)習(xí):模型、方法與實踐
- Terraform:多云、混合云環(huán)境下實現(xiàn)基礎(chǔ)設(shè)施即代碼(第2版)
- OpenGL Data Visualization Cookbook
- iOS自動化測試實戰(zhàn):基于Appium、Python與Pytest
- ElasticSearch Cookbook(Second Edition)
- LabVIEW數(shù)據(jù)采集
- 循序漸進Vue.js 3前端開發(fā)實戰(zhàn)
- KnockoutJS Blueprints
- Node.js Web Development