[Tutorial] Switch & If/else
#1

Introduction

As I have seen many people are using way too much if statements, while they could have used the switch.

The tutorial


A part of my own gamemode, which is using a switch statement:

WRONG:
pawn Code:
if (pStats[playerid][vip] == 1)  SetPlayerColor(playerid, COLOR_VIP_BRONZE);
    if (pStats[playerid][vip] == 2) SetPlayerColor(playerid, COLOR_VIP_SILVER);
    if (pStats[playerid][vip] == 3) SetPlayerColor(playerid, COLOR_VIP_GOLD);
    if (pStats[playerid][vip] == 4) SetPlayerColor(playerid, COLOR_VIP_DIAMOND);
    else SetPlayerColor(playerid, COLOR_VIP_PLAYER);
Correct:
pawn Code:
switch (pStats[playerid][vip]) // A way it can be used. An integer.
    {
            default: SetPlayerColor(playerid, COLOR_VIP_PLAYER); // If a player's level is 0 (normal player)
        case 1: SetPlayerColor(playerid, COLOR_VIP_BRONZE); // If a player's level is 1 (Bronze VIP)
        case 2: SetPlayerColor(playerid, COLOR_VIP_SILVER); // If a player's level is 2 (silver VIP)
        case 3: SetPlayerColor(playerid, COLOR_VIP_GOLD); // If a player's level is 3 (gold VIP)
        case 4: SetPlayerColor(playerid, COLOR_VIP_DIAMOND); // If a player's level is 4 (Diamond VIP)
    }
You might wonder now why the "default" is there, the default is in this fact 0, so the player's vip level is 0. It is better to use than case 0.

Wrong: 5 Lines
Correct: 6 lines

As you can see the if statements are 5 lines, which is shorter than the switch. Although you should still use switch, read below why.

Quote:
Why?
At "Wrong" the server keeps calling the player's stats over and over, which takes much data.
While at "Correct" the server calls the player's stats only once, taking less data than the "wrong" code.

At some situations, using the if/else statement, it's faster, this is with larger case's.

pawn Code:
if(Cash >= 1500 && Cash <= 8000)
{
    // do whatever
}

switch(Cash)
{
    case 1500..8000:
    {
        // do whatever
    }
}
(thank you 2KY for the small tip.)
This is a preview of that, if your if/else or case is over lets say 500, you should use the if & else statement as in that case the 'case' statement is slower.

Offcourse there is an easier way, saving you time although it's a bit harder to read than case and if/else statements.
(Thank you bigETI)

pawn Code:
static const vip_rank[] = {COLOR_VIP_PLAYER, COLOR_VIP_BRONZE, COLOR_VIP_SILVER, COLOR_VIP_GOLD, COLOR_VIP_DIAMOND};
SetPlayerColor(playerid, vip_rank[pStats[playerid][vip]]);
This saves you a lot of space and only takes 2 lines, although it's harder to read.

Additional pages:
https://sampwiki.blast.hk/wiki/Control_Structures#switch
https://sampwiki.blast.hk/wiki/Control_Structures#if

(Critism is allowed, some things might not be right. This is my first tutorial since age's and idea's are welcome.)
Reply


Messages In This Thread
Switch, If/else & Static - by [WA]iRonan - 15.01.2014, 16:52
Re: Switch & If/else - by Diesel5 - 15.01.2014, 16:57
Respuesta: Switch & If/else - by Swedky - 15.01.2014, 16:59
Re: Switch & If/else - by Vince - 15.01.2014, 17:00
Re: Switch & If/else - by iZN - 15.01.2014, 17:09
Re: Switch & If/else - by [WA]iRonan - 15.01.2014, 17:50
Re: Switch & If/else - by 2KY - 15.01.2014, 20:03
Re: Switch & If/else - by Zjiht - 15.01.2014, 20:09
Re: Switch & If/else - by Hansrutger - 15.01.2014, 21:16
Re: Switch & If/else - by Jimmy0wns - 15.01.2014, 21:20

Forum Jump:


Users browsing this thread: 1 Guest(s)