else if or if?
#1

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?
Reply
#2

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.
Reply
#3

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

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
}
Reply
#5

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

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

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)
{
    // ...
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)