inherit
125499
0
Nov 8, 2011 4:03:57 GMT -8
moneyman18
:-
952
June 2008
moneyman18
|
Post by moneyman18 on Nov 12, 2009 22:05:28 GMT -8
Well I am wondering which is better to use in php and JS. Is it better to use a bunch of If statments or a switch? Obviously there are reason to use a switch or else there probably wouldn't be one if there was no reason for it. Is one faster than the other or something? I searched Google but could not find a definate answer to this. Any information is appreciated!
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Nov 12, 2009 22:17:45 GMT -8
I don't know if there is a difference behind the scenes (there may be some, but it depends on the language and compiler/engine), but the reason for the creation of the switch statement is so you have less to type. It also looks prettier and is more organized in my opinion.
You basically just need to figure out when you think it is best to use a switch statement. If you know you are only going to be testing on one variable and you are going to have three or more tests on that variable then it's probably a good idea to use it. I also like it's version of OR ( || ) which you can see below.
switch(x) { case 1: case 2: { // This code will execute when x // is 1 or 2. break; } }
There are times when you have less typing with an if statement, though. Turning the if statement below into a switch would be a lot of typing, especially since you are working with more than one variable.
if(x == 1 && (y == 2 || y == 3) || (x == 2 || x == 3) && y == 1)
|
|
inherit
16846
0
Nov 19, 2012 15:20:20 GMT -8
Chris
3,036
December 2003
cddude
|
Post by Chris on Nov 13, 2009 13:55:47 GMT -8
I did some speed tests a while back and got no difference in JS... so I don't think that's the reason. I'm pretty sure Triad hit it on the nose... it's just an organizational structure to try to make things easier to handle and it's much more effective for a single variable structure. (Also, depending on spacing, it could eventually be smaller in total coding size, I believe.)
|
|
inherit
12045
0
Nov 19, 2012 14:52:05 GMT -8
Renegade
As unique as mice pudding milkshake
40,557
August 2003
renegade
|
Post by Renegade on Nov 14, 2009 5:50:40 GMT -8
I've never used switches before - any chance you could post a few more functional examples so I can get the hang of them?
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Nov 14, 2009 10:48:25 GMT -8
Sure.
They're really handy when you are testing integers or characters.
I know you've made flash games before, and a really good use I've had for my own games is getting user input with them. The keypress_detector is an integer which represents the most recent key pressed that is stored in the queue.
switch(keypress_detector) {
// If the user is pressing enter. case RETURN_VALUE:
// Code
break;
// If the user is pressing either shift button. case LSHIFT_VALUE: case RSHIFT_VALUE:
// Code
break;
// If the user is pressing the up arrow. case UP_VALUE:
// Code
break;
// If the user is pressing the down arrow. case DOWN_VALUE:
// Code
break;
// If the user is trying to quit the game. case ESCAPE_VALUE:
// Code
break;
// If the user is pausing or unpausing the game. case p_VALUE:
// Code }
|
|
inherit
12045
0
Nov 19, 2012 14:52:05 GMT -8
Renegade
As unique as mice pudding milkshake
40,557
August 2003
renegade
|
Post by Renegade on Nov 14, 2009 11:34:31 GMT -8
The syntax in actionscript is pretty similar to JS, so that's exactly why I was asking ;d that does look a lot more useful than billions of if statements, at leat in some cases.
Can you have more than one case, or varuiable per case? such as case 1 || 2 || 3 ? just saw that you included that. thanks ;d
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Nov 14, 2009 11:45:49 GMT -8
What I posted above is actually C++ since all the values are enums which represent integers. It could be written the exact same way in JS, though.
The switch statement is basically same in every language.
|
|
inherit
97216
0
Nov 26, 2024 13:53:14 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on Nov 14, 2009 13:36:27 GMT -8
If you were to use it for different skins, would you just use:
switch(pb_skinid) { case1: break; }
And so on?
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Nov 14, 2009 13:47:50 GMT -8
Yes, but you need a space after the "case" word.
|
|
inherit
97216
0
Nov 26, 2024 13:53:14 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on Nov 14, 2009 16:08:48 GMT -8
That's implied THat was just a quick check.
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Nov 14, 2009 18:01:45 GMT -8
Just making sure. ;P
|
|
inherit
97216
0
Nov 26, 2024 13:53:14 GMT -8
Bennett 🚀
Formerly iPokemon.
3,622
January 2007
catattack
iPokemon's Mini-Profile
|
Post by Bennett 🚀 on Nov 14, 2009 18:21:58 GMT -8
I do know some javascript
|
|
inherit
16846
0
Nov 19, 2012 15:20:20 GMT -8
Chris
3,036
December 2003
cddude
|
Post by Chris on Nov 15, 2009 14:02:16 GMT -8
I will throw out that I've seen some interesting switch statements before: switch(true){ case (pb_username == "tobias"): // Blah break; case (pb_username != "tobias" && somefunc()): // Blah break; } That way you can actually incorporate it like it is an if statement.
|
|
#00AF33
14306
0
1
Sept 8, 2023 8:54:17 GMT -8
Jordan
What is truth?
11,838
October 2003
jab2
|
Post by Jordan on Nov 15, 2009 14:06:09 GMT -8
You can do all kinds of weird things like that in Javascript. I don't think most programming languages would allow that, though.
|
|
inherit
16846
0
Nov 19, 2012 15:20:20 GMT -8
Chris
3,036
December 2003
cddude
|
Post by Chris on Nov 15, 2009 15:40:51 GMT -8
You can do all kinds of weird things like that in Javascript. I don't think most programming languages would allow that, though. I saw that trick originally in PHP. However, I'm not sure about other languages. I think I remember something about Java being very strict with Switch statements and only allowing certain types of variables to be used.
|
|