Jump to content
  • Advertisement
Sign in to follow this  
Stowelly

nested if statements

This topic is 5392 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
Guest Anonymous Poster
nothing wrong with if { } statements.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The question I have for you is how you will get the proper logic if you don't?

Share this post


Link to post
Share on other sites
Quote:
Original post by kuphryn
How deep?

Kuphryn


id say in my programs a maximum of 6/7... so they really are all good as far as optimisation goes then ??

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
The question I have for you is how you will get the proper logic if you don't?


good point.... was only asking this because if theres enough of them in your program it can lead to very confusing code so instantly assumed there was a nicer way to do it

Share this post


Link to post
Share on other sites
Well, there's nothing wrong with nesting if() statements (minus code legibility), technically. Although it's generally considered standard to use a switch() if you're checking for states of a variable (which I assume you already knew...but just in case).

But then again, I guess if you're nesting them, it won't really matter.

NOTE: Whenever possible, make your code more secksy and confusing by using the tertiary operator (? :). It's like Internet Penis Enlargement™ without using Linux.

Share this post


Link to post
Share on other sites
If you have 6 or 7 nested ifs, there may be a better way to do it. Here are a couple of ideas.

If there are two ifs without accompanying elses, they can be grouped into an if (&&). It's the same thing performance-wise, but the code looks cleaner.

For simple if/elses, you can use the ?: operator. Again, same effect, but fewer lines of code.

Use switch statements for series of if/elses where applicable.

Sometimes if you have a complex set of binary conditions, you can represent them with bits and switch on the result. For example:

int mask = 0;
if (enemy is a monster)
mask |= 1;
if (enemy is alive)
mask |= 2;
if (enemy is visible)
mask |= 4;
switch (mask)
{
case 0: enemy is a not-visible, dead non-monster
case 1: enemy is a not-visible, dead monster
case 2: enemy is a not-visible, alive non-monster
case 3: enemy is a not-visible, alive monster
case 4: enemy is a visible, dead non-monster
case 5: enemy is a visible, dead monster
case 6: enemy is a visible, alive non-monster
case 7: enemy is a visible, alive monster
}



The same thing would have involved countless nested and chained if/elses.

So those are my suggestions :)

Share this post


Link to post
Share on other sites
@jyk:
I totally forgot about bitmasking and I was about to look up an article on it. I totally love you now.

rating++.

EDIT: Okay, I lied, I wasn't about to look up an article on it. Geez, get off my back. Everybody hates to rate people up nowadays, but nobody hesitates to rate down.

Share this post


Link to post
Share on other sites
yeah ive thought about using switch statements but i think due to some deep seeding beginers problem i had with the syntax ive always just not used them.

cheers for you help

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!