SA-MP Forums Archive
else if or if? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: else if or if? (/showthread.php?tid=333421)



else if or if? - lukas567 - 11.04.2012

Variant 1
Код:
if(VariableSet)
{
   print("OK");
   return 1;
}
if(!VariableSet)
{
   print("Not OK");
   return 1;
}
Variant 2
Код:
if(VariableSet)
{
   print("OK");
   return 1;
}
else if(!VariableSet)
{
   print("Not OK");
   return 1;
}
Which code will cause less lag than other? Or they're same?


Re: else if or if? - Slice - 11.04.2012

Variant 2 is a little bit faster because it doesn't check if(!VariableSet) if the first if(VariableSet) is true.

The difference in speed is completely insignificant, though, we're talking about like ~0.00000000001some ms.


Re: else if or if? - lukas567 - 11.04.2012

But doesn't return 1; stops checking other variables?


Re: else if or if? - Slice - 11.04.2012

Oh, sorry, I didn't notice that. The performance would be the same, though it's usually best practice to use "else if" when you know the above if statement would have to fail in order for the second one not to.

You could also just do this as there are only two possible outcomes:
pawn Код:
if (VariableSet) {
// stuff
} else {
// stuff
}



Re: else if or if? - lukas567 - 11.04.2012

Space after if:
pawn Код:
if (VariableSet)
It's some part of indentation? Will it cause less lag?


Re: else if or if? - lukas567 - 11.04.2012

Oh, so as everyone says "Will be easier to read" means, that it will be easier to read for programmer? Not for server?


AW: else if or if? - Meta - 11.04.2012

I don't know if you know that so I'll explain it.
There is the possibibily to write if and else without brackets.
for example:
pawn Код:
if(statement) return 1;
else return 0;
But only the first code after the if or else will be executed. else if is used like that, it does not really exist. The following codes are exactly the same except the brackets:
pawn Код:
if(statement)
{
    //...
}
else
{
    if(statement2)
    {
        // ...
    }
}

// ========================

if(statement)
{
    //...
}
else if(statement2)
{
    // ...
}