[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
#2

Good tutorial.
Reply
#3

It is simple, but it uses as much .
+Rep.

Ah, more simplified.


Quote:
It is easier to look from the index (switch) that to look for page for page (if).

Reply
#4

This should be common knowledge, but I'll be sure to link people here whenever I see this kind of structure. Nevertheless, the if-else structure isn't inherently "wrong" since it will still work. Perhaps "bad" or "not recommended" would be a better wording.
Reply
#5

Agree with Vince. I won't be minding to use if-else statement for any amount of cases. Just that it is slow as compared to switch statement. NOT bad or wrong or etcetera. Also, this isn't called a tutorial, you have just gave a hint/suggestion in improving it. Just provide some something like information about if-else statement and switch statement.
Reply
#6

Quote:
Originally Posted by Vince
View Post
This should be common knowledge, but I'll be sure to link people here whenever I see this kind of structure. Nevertheless, the if-else structure isn't inherently "wrong" since it will still work. Perhaps "bad" or "not recommended" would be a better wording.
It's not really common knowledge as I've seen many people making this mistake.
Quote:
Originally Posted by iZN
View Post
Agree with Vince. I won't be minding to use if-else statement for any amount of cases. Just that it is slow as compared to switch statement. NOT bad or wrong or etcetera. Also, this isn't called a tutorial, you have just gave a hint/suggestion in improving it. Just provide some something like information about if-else statement and switch statement.
Yeah it's more a tip, although it's a bit of a tutorial how to prevent wasting data at this point.
Quote:
Originally Posted by EnzoMetlc
View Post
It is simple, but it uses as much .
+Rep.

Ah, more simplified.
Thanks
Quote:
Originally Posted by Diesel5
View Post
Good tutorial.
Thank you.
Reply
#7

May also be worth noting that in some cases the if statement is actually better. For example:

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

switch(Cash)
{
    case 1500..8000:
    {
        // do whatever
    }
}
In this case because of the huge range, the if statement is better than the switch.
Reply
#8

Simple but effective, good job!
Reply
#9

Nice one. Criticism is welcome so here it comes: Could you add what switch actually also does? I bet there are tons of (damage) players that don't know why they are even using case's instead of if-statements (dialogid == ..) on OnPlayerDialogResponse. Just a tip though, good tutorial, understood it. :P
Reply
#10

Never knew about this, thanks for the tutorial, have changed my commands to this as it seems to be faster.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)