|
-
Pumpkin Carving 2008
Polling... multiple ifs or switch?
I'm just wondering how many ifs people use before they use switch instead. I have an old tile game that I'm redoing the engine on now that I'm more experienced, and I have to test for 12 different tiles with a movement in each direction. So what's the max ifs you should have before a switch is more effecient, three, four? Also, would it matter what kind of coding your putting inside of the cases to determine what would be better?
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
switch?? can you point me to a switch tutorial i dont know what you mean by switch??
-
M.D.
i don't think switch is faster or slower so it doesn't really matter, if you don't put breaks in your switches then it could get slow. Its just a better way to look at code (i think).
-
Trainee coder
Sorry IP cant help you, but in responce to Alex, just use flash help (open up flash, click on help found on the toolbar, click on flash help and search for "switch statement") and switch will be explained there.
Viza
-
I'm not sure if has an effect on speed, but I would do it for about 5 cases. And those cases should be very brief with no nesting. Mostly just to make my code readable. Also I always think if you're using cases you could have probably done something with arrays instead.
-
Untitled-1.fla
In bytecode, there's no difference between a series of if - else if and a switch statement. When publishing, a switch is converted to if - else if equivalent using a local variable. For clarity, it's a good idea to use switch though. But it's not drastically faster. This means it's still important to place the most likely cases at the top to speed it up.
-
Hype over content...
I've got to admit, I've never used switch once.
The quickest way of the lot is to do it this way:
Code:
if(a==1){
} else {
if(a==2){
} else {
if(a==3){
}
}
}
As a complete alternative you could just call a function directly if you're having a different outcome for each tile, eg
Code:
var tileAttribute=_map[ypos][xpos][0];
var call=this["func"+tileAttribute];
call();
....
function func0(){
}
function func1(){
}
Squize.
-
Pumpkin Carving 2008
Malee - you mean breaks are not necessary?
This.Alex - an even easier way to find help topics is to type the part or thing you want in the actual code window so it changes color, letting you know it's a built in thing. Then right click on it and click view help.
Strille - That's really good to know. Thanks for the input.
Squize - I thought it was deemed bad practice to use a lot of if's in a game, or at least I remember seeing that somewhere. I did however like the idea of calling a function for each one.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Hype over content...
Good practise and speed issues hardly ever agree 
Squize.
-
Senior Member
 Originally Posted by ImprisonedPride
Malee - you mean breaks are not necessary?
If memory serves me right, break is only necessary when you want to stop evaluating the remaining cases ... (been long time since C class...)
-
Heli Attack!
I use switch if I'm evaluating a single variable, sometimes you cant get around nested ifs:
if(x && y)
{
if(z || x)
{
//blah
}
}
But if I'm evaluating the same variable, I'll switch if its more than an if else.
-
ok thanks do you think switch is worth knowing or can if functions do everything that switch can do??
-
Heli Attack!
You can do some things in switches which are easier than ifs
code:
switch(a)
{
case 0:
//case 0
break;
case 1:
//case 1
break;
case 2:
//case 2
case 3:
//case 3
break;
}
In this example, if 'a' is 2, then it executes cases 2 and 3, which would be a pretty messy if statement with repeated blocks of code.
-
Senior Member
C++ breaks on breaks off
breaks on
Code:
#include<iostream>
// my example
void main()
{
xix=1;
switch(xix)
{
case 1:
cout<<xix<<endl;
break;
case 2:
cout<<xix<<endl;
break;
case 3:
cout<<xix<<endl;
break;
case 4:
cout<<xix<<endl;
break;
}
cin>>xix;
}
out put:
breaks off
Code:
#include<iostream>
//note the same code just without breaks
void main()
{
xix=1;
switch(xix)
{
case 1:
cout<<xix<<endl;
case 2:
cout<<xix<<endl;
case 3:
cout<<xix<<endl;
case 4:
cout<<xix<<endl;
}
cin>>xix;
}
output
switch statements are mostly used on error massages. like this.
Code:
#include<iostream>
//note the same code just without breaks
void main()
{
errorcode=0;
while(errorcode<10)
{
errorcode++;
switch(errorcode)
{
case 1:
cout<<errorcode<<" windows error 101"<<endl;
break;
case 2:
cout<<errorcode<<" windows blue screen of death"<<endl;
break;
case 3:
cout<<errorcode<<" windows time for a mac"<<endl;
break;
default:
cout<<errorcode<<" windows it happens"<<endl;
break;
}
cin>>errorcode;
}
Last edited by zervell; 11-25-2006 at 09:18 PM.
CEO OF
-
Heli Attack!
Works the same in Flash, it's the standard case for switch statements.
-
Pumpkin Carving 2008
 Originally Posted by Ray Beez
If memory serves me right, break is only necessary when you want to stop evaluating the remaining cases ... (been long time since C class...)
If that's true, than I guess it's what I want in my case, because I'm only checking to see what frame the tile is. Once I found it, there'd be no reason to keep checking because the switch is in a function that runs only on keypresses anyhow.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
Please, Call Me Bob
its amazing how little actionscript i know (just learned :P)
if you ask me, all versions seem like they act at the same speed
personaly, i would avoid several conditions and whatnot all in one place completely, and try to find shortcuts instead
instead of:
function asdf(variablehere){
switch(variablehere){
case 1:
trace("1");
break;
case 2:
trace("2");
break;
case 3:
trace("3");
break;
default:
break;
}
}
i think it would be a tad bit easier to:
function asdf(variablehere){
if(variablehere<=3){
if(variablehere>=1){
trace(variablehere);
}
}
}
teehee, good thing i learn fast, otherwise id be left clueless
-
Heli Attack!
you could write it:
code: function asdf(variablehere){
switch(variablehere){
case 2:
case 3:
trace(variablehere);
}
}
The point is, switches have their uses, you generally wouldn't use one in this situation.
-
Pumpkin Carving 2008
I think someone mentioned this, but I think I just do it habitually because it really neatens up the coding structure, though I suppose nested ifs wouldn't be anymore disorganized. Less }'s I suppose.
The 'Boose':
ASUS Sabertooth P67 TUF
Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
8GB G.Skill Ripjaws 1600 DDR3
ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
New addition: OCZ Vertex 240GB SATA III SSD
WEI Score: 7.6
-
you guys are talking like flash nerds lol (no offence) ok so how would i make an object move using switch heres the if one.
if(Key.isDown(Key.RIGHT)){
_root.flashkit._x+=s;
}
how would you turn that into a switch (i gotta know these things a)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|