Greater than or less than in a switch?

Non-platform specific questions.

Greater than or less than in a switch?

Postby Hblade » Thu Oct 13, 2011 6:28 pm

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
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: Greater than or less than in a switch?

Postby EvanBlack » Thu Oct 13, 2011 6:42 pm

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?
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Greater than or less than in a switch?

Postby jimmynewguy » Thu Oct 13, 2011 7:04 pm

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);
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: Greater than or less than in a switch?

Postby Jagmaster » Thu Oct 13, 2011 7:10 pm

I hate ifs! Thank you!
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: Greater than or less than in a switch?

Postby EvanBlack » Thu Oct 13, 2011 7:13 pm

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.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Greater than or less than in a switch?

Postby Game A Gogo » Thu Oct 13, 2011 7:20 pm

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.
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: Greater than or less than in a switch?

Postby jimmynewguy » Thu Oct 13, 2011 7:26 pm

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!
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: Greater than or less than in a switch?

Postby EvanBlack » Thu Oct 13, 2011 7:34 pm

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.
Last edited by EvanBlack on Thu Oct 13, 2011 7:47 pm, edited 1 time in total.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Greater than or less than in a switch?

Postby Hblade » Thu Oct 13, 2011 7:40 pm

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:
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: Greater than or less than in a switch?

Postby Game A Gogo » Thu Oct 13, 2011 7:45 pm

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."
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: Greater than or less than in a switch?

Postby EvanBlack » Thu Oct 13, 2011 7:51 pm

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.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Greater than or less than in a switch?

Postby Jagmaster » Thu Oct 13, 2011 8:08 pm

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.
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: Greater than or less than in a switch?

Postby EvanBlack » Thu Oct 13, 2011 8:14 pm

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.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Greater than or less than in a switch?

Postby Jagmaster » Thu Oct 13, 2011 8:20 pm

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
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: Greater than or less than in a switch?

Postby EvanBlack » Thu Oct 13, 2011 8:28 pm

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;
}
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest