SA-MP Forums Archive
Does This Need 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: Does This Need Else If? (/showthread.php?tid=280157)



Does This Need Else If? - Shockey HD - 30.08.2011

pawn Код:
stock UpdateRank(playerid)
{
    new string[128];
    if(PInfo[playerid][Owner] == 1) return format(pRank[playerid],128,"Server Owner");
    if(PInfo[playerid][Level] < 500) return format(pRank[playerid],128,"Administrator");
    if(PInfo[playerid][Missionspassed] >= 0 && PInfo[playerid][Missionspassed] < 10) return format(pRank[playerid],128,"New Trucker");
    if(PInfo[playerid][Missionspassed] >= 10 && PInfo[playerid][Missionspassed] < 25) return format(pRank[playerid],128,"Trucker");
    if(PInfo[playerid][Missionspassed] >= 25 && PInfo[playerid][Missionspassed] < 45) return format(pRank[playerid],128,"Advancing Trucker");
    if(PInfo[playerid][Missionspassed] >= 45 && PInfo[playerid][Missionspassed] < 65) return format(pRank[playerid],128,"Advanced Trucker");
    if(PInfo[playerid][Missionspassed] >= 65 && PInfo[playerid][Missionspassed] < 90) return format(pRank[playerid],128, "Respected Trucker");
    if(PInfo[playerid][Missionspassed] >= 90 && PInfo[playerid][Missionspassed] < 115) return format(pRank[playerid],128,"Trucking Maniac");
    if(PInfo[playerid][Missionspassed] >= 115 && PInfo[playerid][Missionspassed] < 145) return format(pRank[playerid],128,"Trucking is my life!");
    if(PInfo[playerid][Missionspassed] >= 145 && PInfo[playerid][Missionspassed] < 170) return format(pRank[playerid],128,"Trucking Addicted");
    if(PInfo[playerid][Missionspassed] >= 170) return pRank[playerid] = "Trucking God";
    format(string,sizeof(string),"%s",pRank[playerid]);
    Update3DTextLabelText(Ranklbl[playerid],green2,string);
    return 1;
}



Re: Does This Need Else If? - FireCat - 30.08.2011

In short, no.


Re: Does This Need Else If? - Kingunit - 30.08.2011

Not needed - But I read on the internet that it's better to do

pawn Код:
if
else if
else if
else if
else if



Re : Does This Need Else If? - Naruto_Emilio - 30.08.2011

I think it will be much better to use
pawn Код:
else if



Re: Does This Need Else If? - AndreT - 30.08.2011

I have to inform you your function will stop executing mid-way, because you're using return where it is not needed at all. So only the variable will be updated, not the text label.


Re: Does This Need Else If? - Kar - 30.08.2011

couldn't be bothered adding brackets for you..

pawn Код:
stock UpdateRank(playerid)
{
    new string[40];
    if(PInfo[playerid][Missionspassed] >= 0 && PInfo[playerid][Missionspassed] < 10) format(pRank[playerid], 15, "New Trucker");
    else if(PInfo[playerid][Missionspassed] >= 10 && PInfo[playerid][Missionspassed] < 25) format(pRank[playerid],10,"Trucker");
    else if(PInfo[playerid][Missionspassed] >= 25 && PInfo[playerid][Missionspassed] < 45) format(pRank[playerid],20,"Advancing Trucker");
    else if(PInfo[playerid][Missionspassed] >= 45 && PInfo[playerid][Missionspassed] < 65) format(pRank[playerid],20,"Advanced Trucker");
    else if(PInfo[playerid][Missionspassed] >= 65 && PInfo[playerid][Missionspassed] < 90) format(pRank[playerid],20, "Respected Trucker");
    else if(PInfo[playerid][Missionspassed] >= 90 && PInfo[playerid][Missionspassed] < 115) format(pRank[playerid],20,"Trucking Maniac");
    else if(PInfo[playerid][Missionspassed] >= 115 && PInfo[playerid][Missionspassed] < 145) format(pRank[playerid],21,"Trucking is my life!");
    else if(PInfo[playerid][Missionspassed] >= 145 && PInfo[playerid][Missionspassed] < 170) format(pRank[playerid],20,"Trucking Addicted");
    else if(PInfo[playerid][Missionspassed] >= 170) pRank[playerid] = "Trucking God";
    else format(string, sizeof(string), "%s", pRank[playerid]);
    if(PInfo[playerid][Owner] == 1) strcat(pRank[playerid], "- Server Owner", 40);
    if(PInfo[playerid][Level] < 500) strcat(pRank[playerid], "- Administrator", 40);
    Update3DTextLabelText(Ranklbl[playerid], green2, string);
    return 1;
}



Re: Does This Need Else If? - Vince - 30.08.2011

Better to use switch, actually.

pawn Код:
switch(PInfo[playerid][Missionspassed])
{
    case 0..9: {}
    case 10..24: {}
    case 25..44: {}
    case 45..64: {}
    case 65..89: {}
    case 90..114: {}
    case 115..144: {}
    case 145..169: {}
    default: {}
}



Re: Does This Need Else If? - [L3th4l] - 30.08.2011

Also, don't use format to copy strings, use this instead:
pawn Код:
#define strcpy(%0,%1,%2) \
    strcat((%0[0] = '\0', %0), %1, %2)

//format(pRank[playerid], 15, "New Trucker");
strcpy(pRank[playerid], "New Trucker", 15);
Macro by ******


Re: Does This Need Else If? - Mauzen - 30.08.2011

Using switch is indeed the best thing here. But to answer the else-question: Else is a good thing to use here, as it can safe you 50% of the conditions and so optimizes the code a lot:

pawn Код:
stock UpdateRank(playerid)
{
    new string[128];
    if(PInfo[playerid][Owner] == 1) return format(pRank[playerid],128,"Server Owner");
    if(PInfo[playerid][Level] < 500) return format(pRank[playerid],128,"Administrator");
    if(PInfo[playerid][Missionspassed] >= 0 && PInfo[playerid][Missionspassed] < 10) return format(pRank[playerid],128,"New Trucker");
    else if(/*PInfo[playerid][Missionspassed] >= 10 && */PInfo[playerid][Missionspassed] < 25) return format(pRank[playerid],128,"Trucker");
    else if(/*PInfo[playerid][Missionspassed] >= 25 && */PInfo[playerid][Missionspassed] < 45) return format(pRank[playerid],128,"Advancing Trucker");
    // first condition is already checked, so not needed
    // ...
}



Re: Does This Need Else If? - AndreT - 30.08.2011

pawn Код:
format(pRank[playerid],128,"New Trucker");
Guys, what's with this?!

Did the more efficient version stop working all of the sudden?
pawn Код:
pRank[playerid] = "New Trucker";