官术网_书友最值得收藏!

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
主站蜘蛛池模板: 广水市| 乐平市| 杭州市| 靖远县| 景洪市| 韩城市| 广宗县| 宁波市| 远安县| 中山市| 乳山市| 和林格尔县| 龙胜| 浪卡子县| 韶山市| 道孚县| 怀柔区| 壤塘县| 桐乡市| 安岳县| 灌南县| 科技| 长葛市| 嘉峪关市| 曲麻莱县| 毕节市| 新干县| 福泉市| 海安县| 大竹县| 双鸭山市| 德昌县| 彭阳县| 上高县| 墨竹工卡县| 邵武市| 泸定县| 香港| 淮滨县| 青田县| 徐闻县|