• ### Popular Now

• 12
• 12
• 9
• 10
• 13

#### Archived

This topic is now archived and is closed to further replies.

# hi, i was wondering if you're program contains lots of "if..else", does it in anyway

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

## Recommended Posts

''greatly'' reduces the speed of your game? i mean, what if you have a very long list of "if...else" statement in one particular state of your game? or this just affects a very small fraction of your game''s speed therefore it doesn''t matter? Liked to hear some advice from you guys so that if it does affect then I will reduce my "if...else statements..."(note: the DX common files have lots of this!)thanks people!

##### Share on other sites
If you put the most common condition on the first if, all the else's will be avoided in the code execution, most of the time.

Too much of anything will ultimately affect speed. But in the case of if-else's, the moment an if's condition is satisfied, that code within the block is executed and all the rest of the if-else block is avoided.

[edited by - Waverider on March 18, 2002 10:02:47 PM]

##### Share on other sites
Its probably not going to kill you, but if you have a ton of if-else statements you proably could orginize your code a bit better. Or rather, what you are trying to do could probably be done a better way. Though this isnt always true.

I wouldnt worry about it. Go with what you got and keep learning. 6 months from now you''ll look back at this and laugh.

Ratman

##### Share on other sites
When your new, it seems every program becomes a large mess of if/else''s. With practice you''ll learn how to better encapsulate everything into algorithms, classes, and Finite State Machines (what I have been using a lot lately w00t!). If statements are very cheap, so don''t worry about the speed loss.

When I develop I try to follow this philosophy. If its graphics, make it fast and mean, else make it lean and clean. Anything graphical eats up lots of cycles. If you profile your project you''ll be surprised how little of your time is spent in big chunks of logic code compared to just 1 blit call.

- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com

##### Share on other sites
from beginner to beginner
You might wish to look into the switch() statement if the IFs are bothering you too much.

##### Share on other sites
hey guys thanks for all your thoughts!

actually same with the switch case statements, i just want to know if I really must ''do something'' when my codes has too many logic checkings,

##### Share on other sites
You could use the ternary instead of a if/else statement. I did read some where it was faster. Im not sure about using nested ternary operations as a good idea though.

int y = 7;
int x;

if(y > 5)
x = 1;
else
x = 0;

x = ((y > 5) ? 1 : 0);

-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

##### Share on other sites
You could use the ternary instead of a if/else statement. I did read some where it was faster. Im not sure about using nested ternary operations as a good idea though.

int y = 7;
int x;

if(y > 5)
x = 1;
else
x = 0;

or

x = ((y > 5) ? 1 : 0);

-----------------------------
"There are ones that say they can and there are those who actually do."

"...u can not learn programming in a class, you have to learn it on your own."

##### Share on other sites
The ternary operator is not faster than if-then statements.

Under gcc with optimisations off:

b = (a == 56) ? 1 : 2;  cmpl $56,-4(%ebp) jne L3 movl$1,%eax  jmp L4  .p2align 4,,7L3:  movl $2,%eaxL4: movl %eax,-8(%ebp)  if(a == 56) b = 1;else b = 2; cmpl$56,-4(%ebp)  jne L5  movl 1,-8(%ebp) jmp L6 .p2align 4,,7L5: movl2,-8(%ebp)L6:

So, with optimisations off, the ternary operator is -slower- than an if-then statement!

With optimisations on, they compile to the same code:
  movl $2,%eax cmpl$56,-4(%ebp)  jne L3  movl \$1,%eaxL3: