Page 1 of 1

Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 6:28 pm
by Hblade
How can this be done? Example:
Code: Select all
switch(X) {
        case>0:
        A->x+=X; break;
        case<0:
        A->x-=X; break; }
switch(Y) {
        case>0:
        A->y+=Y; break;
        case<0:
        A->x-=X; break; }


This results in an error

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 6:42 pm
by EvanBlack
why not just use an if/else?

Code: Select all
if(x > 0){ //do something)
if else(x < 0) { //do something else}

if(y > 0){ //do something)
if else(y < 0) { //do something else}


Is the same and cleaner?

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:04 pm
by jimmynewguy
Or if you're using switch just because you "hate" ifs, then you should be able to use:
Code: Select all
A->x += (X > 0) - (X < 0);
A->y += (Y > 0) - (Y < 0);

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:10 pm
by Jagmaster
I hate ifs! Thank you!

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:13 pm
by EvanBlack
jimmynewguy wrote:Or if you're using switch just because you "hate" ifs, then you should be able to use:
Code: Select all
A->x += (X > 0) - (X < 0);
A->y += (Y > 0) - (Y < 0);



Thats even better. I don't hate ifs, don't understand why people do... but I don't understand that code very well either.

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:20 pm
by Game A Gogo
if's are too CPU extensive! avoid if's at all cost! even if that includes your life!!!

Seriously though, avoiding if's will increase performance.

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:26 pm
by jimmynewguy
As for the code, it's like this.
Code: Select all
result = (x > 0);

So we have if x is greater than 0 then return 1, otherwise return 0. In the code I showed hblade I just checked the two ways it could go down and their returns would change the variable. So, if X > 0 then we get 1 for the first and 0 for the second. 1 - 0 = 1 so we add one. If it's X is 0 then they're both 0 and we don't change X and if X < 0 then 0 - 1 = -1 and adding 1 is like subtracting.

I kinda went thorough in case someone else read this and didn't understand either. Makes me feel like I'm "talking down to you" and didn't want that since I can tell you know a lot more about coding than I do with all that iso stuff :lol: Just so you know!

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:34 pm
by EvanBlack
Not always.. If your only comparing a few values then if is the same or better than switch. Sure if you are comparing significant amounts of values then switch is better. But otherwise they are the same.

Code: Select all
If( value1 == value3) { //do something}
else { //do something else}


is much better than.
Code: Select all
switch(value1)
        case value3: { //do something; break;}
        default:  { //do something}


especially if what you are comparing isn't just integers.

where as:

Code: Select all

switch(value1)
            case 0: //do this; break;
            case 1: //do this; break;
            case 2: //do this; break;
            case 3: //do this; break;
            ...
            case 100: //do this; break;



is much better than the if code which I don't want to try to write.


It just knowing when to use which.

Honestly, I would avoid a switch in place of a for statement if it got to be too long.


But because we are talking about script here, specifically for gE. We need to look at how gE handles and compiles the script. Because it could treat multiple if statements just like switch statements and compile them exactly the same and treating them in the most efficient matter. Then it would just be a matter of aesthetics.



jimmynewguy wrote:As for the code, it's like this.
Code: Select all
result = (x > 0);

So we have if x is greater than 0 then return 1, otherwise return 0. In the code I showed hblade I just checked the two ways it could go down and their returns would change the variable. So, if X > 0 then we get 1 for the first and 0 for the second. 1 - 0 = 1 so we add one. If it's X is 0 then they're both 0 and we don't change X and if X < 0 then 0 - 1 = -1 and adding 1 is like subtracting.

I kinda went thorough in case someone else read this and didn't understand either. Makes me feel like I'm "talking down to you" and didn't want that since I can tell you know a lot more about coding than I do with all that iso stuff :lol: Just so you know!



THANK YOU SO MUCH!! I just don't know the math that well, but it makes perfect sense now! I just couldn't figure out how it was returning a value.

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:40 pm
by Hblade
EvanBlack wrote: I don't hate ifs, don't understand why people do...

Neither do I. o.o whats wrong with them haha?

Thanks guys, I just went ahead and used ifs. The reason I wanted "Switch" is because I know most people hate ifs D:

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:45 pm
by Game A Gogo
EvanBlack wrote:Not always.. If your only comparing a few values then if is the same or better than switch. Sure if you are comparing significant amounts of values then switch is better. But otherwise they are the same.

I mean avoiding them both ;)

if's will make your game run choppy if you use them too much. Fuzzy once transformed a program that had a lot of if's in it into a program without any if's! the program ran a lot smoother.
Everything in your computer shouldn't be based on "what if?" it should be based on "it is."

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 7:51 pm
by EvanBlack
Yeah that is the most preferable XDD but sometimes we have to work through the what ifs until we can understand the pattern to follow.

Thanks Game A Gogo for clearing that up.

It is true, if you can tell your cpu this IS it rather than ask it, "is this it?" is faster because then it doesn't have to find out first, it just does the math on the known variables.

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 8:08 pm
by Jagmaster
This makes sense now. Thank you Game a gogo.

Fuzzy wrote:Every time you use if a bunny dies.

(\__/)
(x__x)
(>__<)

Bunny can't dominate the world if you use if's too much. Remember that.

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 8:14 pm
by EvanBlack
Jagmaster wrote:This makes sense now. Thank you Game a gogo.

Fuzzy wrote:Every time you use if a bunny dies.

(\__/)
(x__x)
(>__<)

Bunny can't dominate the world if you use if's too much.



I would just like to also add.. Using if isn't always bad. It just depends how frequent the CPU is checking the if statement.

If it is only checking the if statement say once every 10 mins then there is nothing wrong with it.

If its checking the if statement every single frame or even more during a single frame then you will notice a performance drop.

Don't be afraid to use the if, just use it sparingly and understand the best use of the if. Sometimes you will NEED an if statement, it will be significantly better then other options which could eat up other resources such as memory or cache. There is a trade off for everything.

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 8:20 pm
by Jagmaster
Yeah, I get it. I use if a little too excessively though.
In the past I've done:
if(a==1)
{
b+=1;
}
when I should have used:
b+=1*a;

But yeah, you still need 'em sometimes.

And, I wanted an excuse to bring up bunnies. :P

Re: Greater than or less than in a switch?

PostPosted: Thu Oct 13, 2011 8:28 pm
by EvanBlack
You know what would be REALLY useful! A script profiler for gE. So we can figure out what needs to be optimized.

BTW- Its true... ifs do kill bunnies... but not only every time you use an if, but every time an if is called.

MyActor->DrawActor->Script Editor:
Code: Select all
if(1)
{
    --bunnies;
}