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

  • Lua Quick Start Guide
  • Gabor Szauer
  • 190字
  • 2021-08-05 10:30:42

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
主站蜘蛛池模板: 隆子县| 凤台县| 前郭尔| 九江市| 海门市| 平遥县| 雅安市| 永兴县| 嘉善县| 离岛区| 白玉县| 顺昌县| 阿尔山市| 手游| 新邵县| 巨鹿县| 永州市| 刚察县| 泰宁县| 邯郸市| 长宁县| 隆化县| 环江| 寿阳县| 漳州市| 长汀县| 新安县| 天峨县| 庆阳市| 杭锦旗| 桦川县| 武宣县| 宁化县| 乌拉特后旗| 嘉禾县| 峨眉山市| 甘谷县| 安阳市| 乌兰察布市| 巨鹿县| 措美县|