Which do I use? if or switch?

You must understand the Game Editor concepts, before post here.

Which do I use? if or switch?

Postby Hblade » Fri Sep 09, 2011 8:10 pm

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 :)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Which do I use? if or switch?

Postby NERDnotGEEK » Sat Sep 10, 2011 3:17 pm

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.
NERDnotGEEK
 
Posts: 75
Joined: Sat Oct 30, 2010 1:48 am
Score: 12 Give a positive score

Re: Which do I use? if or switch?

Postby Game A Gogo » Sat Sep 10, 2011 3:21 pm

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"!
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Which do I use? if or switch?

Postby jimmynewguy » Sat Sep 10, 2011 3:32 pm

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
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Which do I use? if or switch?

Postby NERDnotGEEK » Sat Sep 10, 2011 3:37 pm

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?
NERDnotGEEK
 
Posts: 75
Joined: Sat Oct 30, 2010 1:48 am
Score: 12 Give a positive score

Re: Which do I use? if or switch?

Postby Game A Gogo » Sat Sep 10, 2011 3:40 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Which do I use? if or switch?

Postby NERDnotGEEK » Sat Sep 10, 2011 3:45 pm

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
NERDnotGEEK
 
Posts: 75
Joined: Sat Oct 30, 2010 1:48 am
Score: 12 Give a positive score

Re: Which do I use? if or switch?

Postby Fuzzy » Tue Sep 13, 2011 7:49 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score

Re: Which do I use? if or switch?

Postby Jagmaster » Tue Sep 13, 2011 10:46 pm

Thanks for the good information Fuzzy!
User avatar
Jagmaster
 
Posts: 875
Joined: Sun May 08, 2011 4:14 pm
Location: Not where you think.
Score: 82 Give a positive score

Re: Which do I use? if or switch?

Postby Jagmaster » Thu Sep 15, 2011 2:57 am

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
User avatar
Jagmaster
 
Posts: 875
Joined: Sun May 08, 2011 4:14 pm
Location: Not where you think.
Score: 82 Give a positive score

Re: Which do I use? if or switch?

Postby Fuzzy » Mon Sep 26, 2011 8:48 am

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.
Mortal Enemy of IF....THEN(and Inspector Gadget)

Still ThreeFingerPete to tekdino
User avatar
Fuzzy
 
Posts: 1068
Joined: Thu Mar 03, 2005 9:32 am
Location: Plymostic Programmer
Score: 95 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest

cron