Page 1 of 1

Which do I use? if or switch?

PostPosted: Fri Sep 09, 2011 8:10 pm
by Hblade
Are you confused on which one to use? Which looks cleaner, which is easier to correct errors in? lets write an example program using this. Open up global code. This is the if version
Code: Select all
#define PLAYER_X VARIABLE[0]
#define PLAYER_Y VARIABLE[1]
#define RAMMOUNT VARIABLE[2]

int VARIABLE[4]; //Create 3 variabels prettymuch


void MovePlayer() { //If version
    RAMMOUNT=rand(5); // (Player will move to 6 random positions)
    if (RAMMOUNT == 0) {
        PLAYER_X=50;
        PLAYER_Y=50; }
    if (RAMMOUNT == 1) {
        PLAYER_X=55;
        PLAYER_Y=150; }
    if (RAMMOUNT == 2) {
        PLAYER_X=150;
        PLAYER_Y=155; }
    if (RAMMOUNT ==3) {
        PLAYER_X=255;
        PLAYER_Y=150;
    if (RAMMOUNT == 4) {
        PLAYER_X=150;
        PLAYER_Y=255; }
    if RAMMOUNT == 5) {
        PLAYER_X=300;
        PLAYER_Y=300; }
    Player.x=PLAYER_X;
    Player.y=PLAYER_Y;
}

Here is the same thing, only in switch form.
Code: Select all
#define PLAYER_X VARIABLE[0]
#define PLAYER_Y VARIABLE[1]
#define RAMMOUNT VARIABLE[2]

int VARIABLE[4]; //Create 3 variabels prettymuch


void MovePlayer() { //switch version
    RAMMOUNT=rand(5); // (Player will move to 6 random positions)
    Player.x=PLAYER_X;
    Player.y=PLAYER_Y;

    switch(RAMMOUNT) {
        case 0:
            PLAYER_X=50; PLAYER_Y=50;
        break;
        case 1:
            PLAYER_X=55; PLAYER_Y=150;
        break;
        case 2:
            PLAYER_X=150; PLAYER_Y=155;
        break;
        case 3:
            PLAYER_X=255; PLAYER_Y=150;
        break;
        case 4:
            PLAYER_X=150; PLAYER_Y=255;
        break;
        case 5:
            PLAYER_X=300; PLAYER_Y=300;
        break;
                }
}





Now you can see the difference for yourself, you decide which one you think is easier for you.

Hope this helped someone decide which to use :)

Re: Which do I use? if or switch?

PostPosted: Sat Sep 10, 2011 3:17 pm
by NERDnotGEEK
Very good :) At school in computed I was surprised at how people didnt even know there was an alternative to IF statements. Switch is a lot neater in my opinion, and not frowned on upon as much, but I still just use if statements. I find them a lot easier to read through and understand what I'm actually writing.

Re: Which do I use? if or switch?

PostPosted: Sat Sep 10, 2011 3:21 pm
by Game A Gogo
NERDnotGEEK wrote:Very good :) At school in computed I was surprised at how people didnt even know there was an alternative to IF statements. Switch is a lot neater in my opinion, and not frowned on upon as much, but I still just use if statements. I find them a lot easier to read through and understand what I'm actually writing.


avoid IF's at ALL cost! they take too much cpu cycles! even if you're going to use the "value=(condition)?true:false"!

Re: Which do I use? if or switch?

PostPosted: Sat Sep 10, 2011 3:32 pm
by jimmynewguy
For the switch one you might want to move this under the switch statement
Code: Select all
Player.x=PLAYER_X;
Player.y=PLAYER_Y;

You had it that way in the if but changed it for some reason :) This will just make the player go to 0, 0

Re: Which do I use? if or switch?

PostPosted: Sat Sep 10, 2011 3:37 pm
by NERDnotGEEK
Game A Gogo wrote:
NERDnotGEEK wrote:Very good :) At school in computed I was surprised at how people didnt even know there was an alternative to IF statements. Switch is a lot neater in my opinion, and not frowned on upon as much, but I still just use if statements. I find them a lot easier to read through and understand what I'm actually writing.


avoid IF's at ALL cost! they take too much cpu cycles! even if you're going to use the "value=(condition)?true:false"!


I do use switch when I'm programming big things, like my computing project at the moment. and our teacher wont let us use ifs unless its 100% necessary. But yes I do know this. to be honest I think personally I'm quite smart in the way I program, I normally find a nice neat way round a problem where no If statements will even be needed. still on small scale games, there's little need to not use them here and there to save time when making the game :')

Edit

one more thing, shouldn't really an else if be used here?

Re: Which do I use? if or switch?

PostPosted: Sat Sep 10, 2011 3:40 pm
by Game A Gogo
There are thousands of tricks not to use if's, all it requires is imagination!
Personally I still use them because I'm lazy... I know fuzzy is the master of not using if's!

Besides in the programming world, there shouldn't be if's, since everything must simply be! if that makes sense :P

Re: Which do I use? if or switch?

PostPosted: Sat Sep 10, 2011 3:45 pm
by NERDnotGEEK
I quite liked our last computing paper, there was a whole section where we had to rewrite code to make it more efficient, and all but 1 of the questions where hinting at us to remove the if statements :D

Re: Which do I use? if or switch?

PostPosted: Tue Sep 13, 2011 7:49 am
by Fuzzy
Losing the if statements uncomplicates your code. For that very reason, if at all possible, dont even use switch/case. In many cases you can arrange things so that your options happen decisionless. The classic example is

key press -> s
Code: Select all
shields = !shields;


Where shields has a value of 1 to start, meaning that they are active. Every time you press the s key, they will toggle. In real life, you dont think "if the light switch is on, turn it off, but if its off, turn it on". You just act. Code like that.

To address the original post code, I would advise using switch. Perhaps you decide to change rand(5) to 4 or 6, or your finger just slips. It isnt true in this situation, but it could be that the random value is taken from a variable, and something else changes it. You need to cover that possibilty, and you might as well form a good habit and always take that step.

Add a default: case to the switch, handling anything that isnt a happy number for you. Use that block of code to set text on a normally invisible error actor. By this means you can find subtle errors more easily and you have covered 100% of possibilities.

Code: Select all
#define PLAYER_X VARIABLE[0]
#define PLAYER_Y VARIABLE[1]
#define RAMMOUNT VARIABLE[2]

int VARIABLE[4]; //Create 3 variabels prettymuch


void MovePlayer() { //switch version
    RAMMOUNT=rand(5); // (Player will move to 6 random positions)
    Player.x=PLAYER_X;
    Player.y=PLAYER_Y;

    switch(RAMMOUNT) {
        case 0:
            PLAYER_X=50; PLAYER_Y=50;
        break;
        case 1:
            PLAYER_X=55; PLAYER_Y=150;
        break;
        case 2:
            PLAYER_X=150; PLAYER_Y=155;
        break;
        case 3:
            PLAYER_X=255; PLAYER_Y=150;
        break;
        case 4:
            PLAYER_X=150; PLAYER_Y=255;
        break;
        case 5:
            PLAYER_X=300; PLAYER_Y=300;
        break;
        default:
            // something went terribly wrong! Alert the programmer!
                }
}


Note that you dont need a break for the last case, but it doesnt hurt to put it in.

Re: Which do I use? if or switch?

PostPosted: Tue Sep 13, 2011 10:46 pm
by Jagmaster
Thanks for the good information Fuzzy!

Re: Which do I use? if or switch?

PostPosted: Thu Sep 15, 2011 2:57 am
by Jagmaster
I just found this topic which is full of if replacements!
I've only read the first page so far, but I've already learned a lot from it.

http://game-editor.com/forum/viewtopic.php?f=2&t=7462

Re: Which do I use? if or switch?

PostPosted: Mon Sep 26, 2011 8:48 am
by Fuzzy
Jagmaster wrote:I just found this topic which is full of if replacements!
I've only read the first page so far, but I've already learned a lot from it.

http://game-editor.com/forum/viewtopic.php?f=2&t=7462


Glad you did. We had a lot of fun with that page. I'm dreaming up something similar.