SA-MP Forums Archive
Difference between else and else 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: Difference between else and else if (/showthread.php?tid=607812)



Difference between else and else if - Nin9r - 24.05.2016

Hello. What's the difference between else and else if ?

How to use?


Re: Difference between else and else if - ratxrat - 24.05.2016

else : if 1 Condition not match than goto else

example
Код:
 if (goto school)
{
get knowledge
}
else
{
not get any
}
else if more than 1 condition

example
Код:
 if (goto school)
{
get knowledge
}
else if (goto mall)
{
shoping
}
else if(goto movies)
{
get a girlfriend :D
}
else
{
not get any
}



Re: Difference between else and else if - Dayrion - 24.05.2016

Let's take an example :
There is an admin command making you in GODMODE.
PHP код:
CMD:god(playerid,params[])
{
     if(
PlayerInfo[playerid][pAdminlvl] >= && PlayerGodMode[playerid] == 1// If the player has AT LEAST a admin lvl > 4 AND he is NOT in godmode
    
{
        
PlayerGodMode[playerid] = 2;
        
SendClientMessage(playerid, -1"Godmode on");
        
GOD SetTimerEx("GodMode"100true"i"playerid);
        return 
1;
    }
     else if(
PlayerInfo[playerid][pAdminlvl] >= && PlayerGodMode[playerid] == 2// ELSE IF the player has AT LEAST a admin lvl > 4 AND he IS in godmode.
    
{
        
SendClientMessage(playerid, -1"Godmode off");
        
KillTimer(GOD);
        
PlayerGodMode[playerid] = 1;
        
SetPlayerHealth(playerid100.0);
        
SetPlayerArmour(playerid100.0);
        return 
1;
    }
      else return 
SendClientMessage(playeridred"You are not allowed to use this command."); // ELSE : the player isn't admin

I hope you understand the difference. By the way check THIS.


Re: Difference between else and else if - Nin9r - 19.06.2016

I know how to use it but i don't know the difference between that.

For example, i can use:

if(var == 1)
{
code
}
if(var == 2)
{
}

or

if(var == 1)
{
code
}
else if(var == 2)
{
}
.
What's the difference?


Re: Difference between else and else if - Konstantinos - 19.06.2016

For the first: both if statements are checked.
For the second: the else if statement will be checked only if the if statement is false. That means that if the if statement is true, the else if will never be checked.


Re: Difference between else and else if - Vince - 19.06.2016

I was always taught to avoid "else if" wherever possible. And as it happens, there are very few cases where else-if is the appropriate thing to use. It can usually be replaced with a switch, one way or another.


Re: Difference between else and else if - Nin9r - 19.06.2016

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
For the first: both if statements are checked.
For the second: the else if statement will be checked only if the if statement is false. That means that if the if statement is true, the else if will never be checked.
So, the script will return when the if value is true?
new var = 2;
if(var == 1)
{
}
else if(var == 2)
{
}
else if(var == 3)
{
}
. The script will be returned at if var == 2 no ?

if
{
}
if
{
}
will be used just if you want to check different values in any script.

I understand now. Thank you.


Re: Difference between else and else if - Luicy. - 19.06.2016

Just a tip,

Bad:
PHP код:
if(var == 1)
{
}
else if(var == 
2)
{
}
else if(var == 
3)
{

good:
PHP код:
switch(var)
{
    case 
1:
    {
       
// This is if(var == 1)
    
}
    case 
2:
    {
       
// This is if(var == 2)
    
}
    case 
3:
    {
       
// This is if(var == 3)
    
}
    default:
    {
       
// This is else
    
}




Re: Difference between else and else if - FuNkYTheGreat - 19.06.2016

Quote:
Originally Posted by Nin9r
Посмотреть сообщение
So, the script will return when the if value is true?
new var = 2;
if(var == 1)
{
}
else if(var == 2)
{
}
else if(var == 3)
{
}
. The script will be returned at if var == 2 no ?

if
{
}
if
{
}
will be used just if you want to check different values in any script.

I understand now. Thank you.
FOR EXAMPLE:
Код:
if(GetPlayerWeapon(playerid) == 4) // if player has a Knife it will Send player a message " This is a Knife"
{
 SendClientMessage(playerid, -1, "This Is A Knife.");
}
else if(GetPlayerWeapon(playerid) == 5) // but if player has a Baseball Bat it will Send player a message " This is a BaseBall bat."
{
 SendClientMessage(playerid, -1, "This is A BaseBall Bat.");
}