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

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
主站蜘蛛池模板: 深水埗区| 甘孜| 樟树市| 分宜县| 东辽县| 饶河县| 来安县| 平舆县| 汨罗市| 平阴县| 海宁市| 鄂托克前旗| 长岛县| 余干县| 胶南市| 古蔺县| 罗田县| 河北区| 南木林县| 台安县| 富民县| 延津县| 永登县| 烟台市| 祥云县| 襄垣县| 德安县| 新野县| 汉阴县| 乡宁县| 边坝县| 通江县| 台湾省| 柳河县| 新化县| 白水县| 通辽市| 吴桥县| 台州市| 绍兴市| 广灵县|