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

Quick Question about Elseif?

Asked by 5 years ago

I am fairly new to scripting, and wondering, what would the function elseif do? and how would I properly execute it? I know what else does, and can't figure out how to use elseif.

0
http://robloxdev.com/articles/Conditional-Statements-in-Lua (Check section named "elseif") RayCurse 1518 — 5y

1 answer

Log in to vote
4
Answered by
Link150 1355 Badge of Merit Moderation Voter
5 years ago
Edited 5 years ago

First of all, elseif is not a function. It is a keyword.

The difference between elseif and else is that the former lets you specify a new condition which must be true for the block to be entered, while the latter executes if and only if all previous if and elseifs failed.

It is really just a combination of else and if:

if A then
    -- do something if `A` is true
elseif B then
    -- do something else if `A` is false but `B` is true
end

is equivalent to

if A then
    -- do something if `A` is true
else
    if B then
        -- do something else if `A` is false but `B` is true
    end
end

The former is clearer and should be preferred.

0
I'd like to add that an `if` statement can have as many `elseif` clauses as you want, but can only have a *single* `else` clause located at the very end. Link150 1355 — 5y
Ad

Answer this question