Nesting if statements

if/elseif/else statements control the execution of chunks of code. Like chunks, if statements can be nested. The same rule for scope applies to nested if statements as it does to nested chunks. The following code demonstrates the use of nested if statements:

print ("Enter a number")
x = io.read()

if x == "6" then
print ("x is six!")

print ("Enter another number")
local y = io.read()
-- Nested if statement begins here
if y == "6" then
print ("y is also six!")
elseif y == "5" then
print ("y is one less than x")
else
print ("x is 6, but y is not!")
end
-- Nested if statement ends here
else
print ("x is not 6!" .. x)
end
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.205.99