Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

What is the difference between else and elseif?

Asked by 4 years ago

I'm watching some tutorials and they use elseif, and I don't know what it is, because they don't explain it, and I can't find tutorials that explain what is elseif used for.

1 answer

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

The simple answer is that the else block will run if no other blocks ran and a elseif block will be evaluated if the previous did not run.

Now some examples ........

using else

local run = true

if run then
    print('runs only if  truthy')
else
    print('runs if the above did not run')
end

using elseif

local run = true
local run2 = true
local run3 = true

if run then
    print('run first')
elseif run2 then
    print('the above did not run. run must be falsy')
elseif run3 then
    print('you can chain as many as you want but only the first one that evaluate to true will run')
else
    print('none of the above ran')
end

The above are just some testing setups you can run and turn the variable from true to false.

Also take a look at how Lua treats type as either true or false her

I hope this helps. Please comment if you have any other questions about this code.

0
You made a spelling mistake^•^. I accepted this answer because this give’s an explanation of the question asked with a following example Ziffixture 6913 — 4y
Ad

Answer this question