nested if statements

Started by
20 comments, last by flangazor 19 years, 6 months ago
just wondering if using these alot in games is bad practise i.e hinders performance and if theres better methods
http://stowelly.co.uk/
Advertisement
nothing wrong with if { } statements.
How deep?

Kuphryn
The question I have for you is how you will get the proper logic if you don't?
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 ??
http://stowelly.co.uk/
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
http://stowelly.co.uk/
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.
Things change.
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 :)
@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.
Things change.
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
http://stowelly.co.uk/

This topic is closed to new replies.

Advertisement