Alternative for "if" statements?

Non-platform specific questions.

Alternative for "if" statements?

Postby Hblade » Sat May 05, 2012 9:13 pm

Is it possible to do something like this?
Code: Select all
int VALUE=(VARIABLE=5);


That way VALUE will equal 1 when VARIABLE = 5?

Also, how would I determine an action like, say, ChangeAnimation("eh..", "walkright", NO_CHANGE)=(VARIABLE=5);

This way when VARIABLE is equal to 5, the animation will change? Sorry if I dont make sense.
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: Alternative for "if" statements?

Postby skydereign » Sat May 05, 2012 9:50 pm

Yes it is possible.
Code: Select all
int VALUE= VARIABLE==5;

Remember that == returns a 1 if the left and right are the same, and a 0 if they aren't. You can combine it with &&/||. That is how C handles if statements, if it is true, it returns a 1, if it is false it returns a 0. if statements will trigger on any non-zero condition.

And the way you wrote that doesn't make any sense, since ChangeAnimation returns a value, not a variable. So setting a value equal to VARIABLE=5 wouldn't work (and wouldn't make sense). You can use && to do what you are talking about, you see it more often in shell scripting, but I generally think it is a bad practice.
Code: Select all
variable==5 && ChangeAnimation("eh..", "walkright", NO_CHANGE);
// because it uses and, the first part has to be true, otherwise it stops
// but if it true (variable is equal to 5) it will check the next condition (which is ChangeAnimation)
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Alternative for "if" statements?

Postby Hblade » Sat May 05, 2012 10:00 pm

Thank you so much sky :)

And why would it be a bad practice?
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: Alternative for "if" statements?

Postby SuperSonic » Sat May 05, 2012 10:08 pm

Sky, you always beat me to helping people xD
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: Alternative for "if" statements?

Postby skydereign » Sat May 05, 2012 10:13 pm

Why don't you want to use an if statement? It isn't like this method is avoiding doing a comparison (meaning they are equally efficient), and even then, one comparison isn't worth making your code hard to read. If you get to using that method, your code can get even harder to read. For instance you can use the ternary operator in a similar way. And you can even fuse the two. But which is easier to read? This...
Code: Select all
variable==0 ? ChangeAnimation("Event Actor", "run", FORWARD) : variable==1 && ChangeAnimation("Event Actor", "walk", FORWARD);

Or this...
Code: Select all
if(variable==0)
{
    ChangeAnimation("Event Actor", "run", FORWARD);
}
else if(variable==1)
{
    ChangeAnimation("Event Actor", "walk", FORWARD);
}
// I didn't use a switch statement, since the above example logically uses a similar structure as if/else if

Also, I always recommend using brackets, which a lot of people don't do. But there are a lot of little things you can do to make your code easier to read. So, avoiding shortcuts (that don't have much advantage if any) is one of the best ways. In short scripts, using the && is perfectly fine, but as the code gets larger, the clearer you need to write the code.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Alternative for "if" statements?

Postby Hblade » Sat May 05, 2012 10:36 pm

Ahh I see now. Thanks :) So it uses the same CPU power as if's? (Not that its noticable anyways)

Also, 1 more thing:
Whats the ? do in your statement?

viewtopic.php?f=27&t=11975&p=84581#p84581

Is that a good use of the var==0 && code idea?
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: Alternative for "if" statements?

Postby skydereign » Sat May 05, 2012 11:14 pm

That's the ternary operator. It works as an if else statement.
Code: Select all
if(var==0) { r=0; } else { r=255; }
(var==0) ?(r=0) : (r=255);
/*(condition) ? this : otherwise this; */

As you can see it is a mini if/else. It can be used to shorten code when dealing with simple if/else, but I don't recommend doing anything complex with it for the same reason I don't recommend using condition && code.

Hblade wrote:Is that a good use of the var==0 && code idea?

I would say no, on the grounds that you'd be teaching the same bad habit, and you are demonstrating code to other people. When you write code for others, you should keep it as clean and direct as possible. You don't see people using that very often in C, because it isn't common practice.

There is nothing wrong with using if statements. On the occasion that they can be avoided by cleaning up your program's structure, you should. But if an if statement makes sense logically, and you aren't relying on complex branching because of if statements, then you shouldn't make your code more complex by avoiding it. An example would be using prime numbers so you can use a switch, instead of using an if.

Now, another thing. You use this.
Code: Select all
fm >= 1 && fm--;

Which would be just as easily done by doing this.
Code: Select all
fm-=(fm>=1);

They do the same thing, and it is clearer what the second one is doing than the first one. Also you were just asking about that.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Alternative for "if" statements?

Postby Hblade » Sun May 06, 2012 2:38 am

Thank you so much :) This is very very helpful!
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: Alternative for "if" statements?

Postby pyrometal » Sun May 06, 2012 3:07 pm

skydereign wrote:That's the ternary operator. It works as an if else statement.

Code: Select all
if(var==0) { r=0; } else { r=255; }
(var==0) ?(r=0) : (r=255);
/*(condition) ? this : otherwise this; */


You can even rewrite that shorter
Code: Select all
r = var ? 255 : 0;


Apologies for the drive by contribution! Have a nice day :)
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Re: Alternative for "if" statements?

Postby skydereign » Sun May 06, 2012 4:08 pm

pyrometal wrote:You can even rewrite that shorter

Oh I know. But that wasn't using the same form. Normally that is how I use the ternary operator. I notice your signature has changed, does that mean your previous project has been dropped?
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Alternative for "if" statements?

Postby pyrometal » Sun May 06, 2012 4:57 pm

skydereign wrote:I notice your signature has changed, does that mean your previous project has been dropped?

None of my projects are ever dropped, just put on pause. I do want to get back to all these, but I find there is a lack of tools out there for Linux game development. Hence why I have started Sprite Warrior! I've made good progress on it so far but its not ready for deployment yet. I will probably make a post about it once I am closer to completion. I am keeping GE in mind while building it so art created with it dosen't require use of secondary utilities for compatibility.

Glad to see the forum is still active :)
SPRITE WARRIOR:
Free, open-source & crossplatform pixel art editor (currently under construction).
User avatar
pyrometal
 
Posts: 706
Joined: Wed Nov 28, 2007 4:07 am
Location: Kingston, ON, Canada
Score: 86 Give a positive score

Re: Alternative for "if" statements?

Postby Hblade » Sun May 06, 2012 7:18 pm

pyrometal wrote:I find there is a lack of tools out there for Linux game development.


:D
Gimp
Inkscape
Gedit (For pregramming, google "Pimp My Gedit" for some interesting stuff)
Blender 3D
LMMS (Linux MutliMedia Studio)

Also some creative commons stuff:
http://soundcloud.com/creativecommons
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: Alternative for "if" statements?

Postby skydereign » Sun May 06, 2012 7:54 pm

pyrometal wrote:
skydereign wrote:I notice your signature has changed, does that mean your previous project has been dropped?

None of my projects are ever dropped, just put on pause. I do want to get back to all these, but I find there is a lack of tools out there for Linux game development. Hence why I have started Sprite Warrior! I've made good progress on it so far but its not ready for deployment yet. I will probably make a post about it once I am closer to completion. I am keeping GE in mind while building it so art created with it dosen't require use of secondary utilities for compatibility.

Glad to see the forum is still active :)

It's nice to know you are still around. And sprite warrior sounds quite intriguing, it'd be great to have a devoted spiriting tool for linux.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest