Which do I use? if or switch?
Posted: 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
Here is the same thing, only in switch form.
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
- 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