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

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
主站蜘蛛池模板: 达州市| 金山区| 延川县| 万州区| 澳门| 牡丹江市| 梅州市| 博乐市| 深泽县| 丹阳市| 长丰县| 高密市| 翼城县| 滦南县| 道孚县| 弥勒县| 寿宁县| 侯马市| 安龙县| 淄博市| 沧州市| 盈江县| 南丰县| 合阳县| 穆棱市| 乌什县| 遂平县| 原阳县| 错那县| 兴和县| 兴宁市| 比如县| 宣武区| 尤溪县| 旺苍县| 九龙城区| 铜陵市| 常宁市| 遵义市| 麦盖提县| 静海县|