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

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
主站蜘蛛池模板: 册亨县| 泸西县| 水城县| 辽宁省| 赣州市| 多伦县| 涞源县| 右玉县| 顺平县| 长宁县| 沂源县| 绥棱县| 广河县| 迭部县| 闸北区| 伊宁县| 贵阳市| 长白| 华宁县| 博爱县| 松溪县| 吉木萨尔县| 读书| 翁牛特旗| 高陵县| 涡阳县| 博白县| 玛曲县| 博乐市| 石楼县| 那曲县| 皮山县| 巴楚县| 仁怀市| 上蔡县| 蒙城县| 轮台县| 罗平县| 兴海县| 仁化县| 闽侯县|