if and else if
#1

I really did't understand the difference between if and else if
Can anyone explain and should I put inside else if code return 1;
I mean
Код:
else if(PlayerData[playerid][Member] == 1)
{
 code
 return 1;
}
or
else if(PlayerData[playerid][Member] == 1)
{
 code
 without retrun 1;
}
Reply
#2

The first check is always if. In case you want to check for the same variable if the value is something else and do something else, you'll need to use else if.

A simple example:
pawn Код:
// a is 10.
// b is 0 but it will change in the statements below.
new
    a = 10,
    b
;

if( a == 0 ) // if a is 0...
{
    b = 1; // ... set b to 1.
}
else if( a = 5 ) // if the above "if( a == 0 )" return false, that a is not 0, the next "else if" will be called. If a is 5...
{
    b = 2; // ... set b to 2.
}
else if( a = 10 ) // if the above is false, a is not 5, then this "else if" will be called. Is a equal to 10? Yes, it is!
{
    b = 3; // ... so set b to 3 and don't execute the rest of "else if" and/or "else" statements.
}
else
{
    b = 4;
}
Reply
#3

return 1; means stop running the code in that function
https://sampwiki.blast.hk/wiki/Control_Structures#return

this site has some good info
https://sampwiki.blast.hk/wiki/Control_Structures#else_if
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)