Difference between else and else if
#1

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

How to use?
Reply
#2

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

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

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

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

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

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

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
    
}

Reply
#9

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.");
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)