What's the Point of "elseif"?

What’s the point of the elseif statement? For example:



Health = 100;
Alive = true;

if Health == 0 then
	Alive = false;
elseif Health > 0 then
	Alive = true;
end


Why would I not just have two if statements instead of adding elseif in there?

[editline]25th July 2015[/editline]

Also - Another question, what’s the difference between a method and a function?

the chunk nested in the elseif statement is called if the previous ifs and elseifs were false, and the condition of the current statement is met. In the example you posted, it would make more sense to use an else statement, but oh well.

a function is a callable piece of code, that is, code that is only ran when the “function is called”. You can pass arguments to the function, and these “arguments” (variables) are available in the code being called. A method is a special kind of function: It’s a member of the object it’s being called on, and the first argument is the object itself. For example:

[lua]
MyObject:Method()
– Is the same as
MyObject.Method( MyObject )
[/lua]

The alternative would become
[lua]Health = 100;
Alive = true;

if Health == 0 then
Alive = false;
else
if Health > 0 then
Alive = true;
end
end[/lua]

elseif is just shorter and makes more sense to use.

A method is a function which is called on an object.
In the following examples this is demonstrated:
[lua]function GM:TestFunction()
print(self)
end

– Now call it like so:
GM:TestFunction()

– Or like this:
GM.TestFunction(GM)
[/lua]

As you can see, “self” is the object the method is called on

[editline]25th July 2015[/editline]

damn late :goodjob: me

But whenever something does more damage than the person’s health (100) then won’t it be a negative? If I were to put an else statement wouldn’t it mean that even if the person’s health is less than 0 then they will be alive?

[editline]25th July 2015[/editline]

I think I’m getting a better understanding thanks! :slight_smile:

I meant that you are doing 2 checks when you only need to do one: Is the player health great than 0, hence:

[lua]
if Health > 0 then
Alive = true;
else
Alive = false
end
[/lua]

I see now, I was thinking way to hard. I tend to do that a lot lol.

I was making it too complicated. I see what you mean now.

[editline]25th July 2015[/editline]

Also, are parameters and and arguments the same thing?

You don’t even need all that…

[lua]
Alive = Health > 0
[/lua]

In the following:

function func(a, b)
end

“a” and “b” are parameters.

The arguments are the values that get assigned automatically to the parameters “a” and “b” when you call the function.

func(x == z, 4)

in this case, “x == z” is an expression (which is one kind of argument), 4 is a literal value (another kind of argument) - and you can pass other variables (as arguments too).

  • parameters are sometimes called “formal parameters”
  • arguments are sometimes called “actual parameters”, although the actual parameters would probably refer to the result of the expression “x == z” rather than the expression itself.

–snip–

This one right here! Thanks so much mate :slight_smile: It helped a lot!

The point of the elseif keyword is to create multi-endpoint branches where, after a condition is met, subsequent condition checks are not performed.

Consider the following:


if Health >= 50 then
    something
end

if Health > 0 and Health < 50 then
    something else
end

if Health == 0 then
   well you died
end


If we execute this code, the computer WILL ALWAYS run 3 condition checks even if the user’s health is 100. It will see that it’s >=50, run something, then make its decision about 100 not being == 0 nor (>0 and <50).
While if we use the elseif, the computer will only run as much conditions checks as needed to get a satisfied condition.



if Health >= 50 then
   something
elseif Health > 0 and Health < 50 then
   something else
elseif Health == 0
   well you died
end


100 Health? 1 condition check. 45 health? 2. 0 health? 3. -1 health… well, you also need to evaluate all three conditions, but in this code, 50% of the time evaluating one condition is enough to decide what to do.
And conditions aren’t just “this variable equals/greater/smaller than this” or “this value exists in this array”, sometimes conditions can be REALLY long and thus, wasting time and memory to evaluate them is bad practice.

Another great thing about elseif is when you branch based on the result of a bool expression.
Let A and B some booleans, totally arbitrary. Let’s do something when both are true and do something else if only one of them are true and do something else else if both are false
Consider the following code:



if A and B then
   something
end

if A or B then
   something else
end

if not A and not B then
   something else else
end


Right now, if A and B are both true, something and something else are both executed. Not what we want.
To do what we want, you would need to use a more complex structure, use XOR, etc. But why would we? If we changed the second condition from “A or B” to “A xor B”, yet again, we’d have three conditions.

But we can abuse the power of elseif!



if A and B then
   something
elseif A or B then
   something else
else
   something else else
end


Why does this work? Becuase the computer runs the code from the top to the bottom. If both A and B are true, something runs and the evaluation of this code is over.
But if they aren’t both true, that branch will not run, the computer will, instead, evaluate “A or B”. If so, something else. If both are false, the else statement will be executed. Just what we wanted.

It is simply a great tool, makes writing and reading code easier. Of course, to properly read the last code, you need to know that the second branch is not simply “if A or B” (because that would run when both are true!), but “if (all above is false) and (A or B)”. But instead of evaluating the expressions by running A, B (they could be REALLY long functions that return a boolean value) and calculating the result with the given logical operator, the way the whole code executed makes us sure that “A or B” right now is really an “A xor B”, but no xor function is ran.

And if you write neat-looking code (which you SHOULD!), you know that after every flow control, there comes an indentation. Who the hell would want to read code which looks like a descending stairwell?