breaking up an if statement into multiple if statements?
now i just wanna get an educated response on this cause i honestly am not sure if theres any benefit of one over the other.. and if there is, if its really enough of a benefit to make any difference
but say you have an if statement like this: Code:
im being told by someone that this is a better way to do it: Code:
so just hoping to get some clarification on this... |
Re: breaking up an if statement into multiple if statements?
First one is just fine, if this == 0, other statements won't be checked.
|
Re: breaking up an if statement into multiple if statements?
I was going to say something to that effect but I was looking for where I read it :). It was a post by Exolent.
|
Re: breaking up an if statement into multiple if statements?
A switch is better to use when you have if statements like this:
PHP Code:
PHP Code:
|
Re: breaking up an if statement into multiple if statements?
alright...
and yeah i usually use switches if they seem appropriate.. but with an if statement such as this for example i figured one if statement would be fine Code:
|
Re: breaking up an if statement into multiple if statements?
If you're hoping to avoid checking the values, make sure you put them in the following order:
if( !most_likely_to_be_zero || ... || ... || ... || !least_likely_to_be_zero ) or, more clearly: if( most_likely_to_be_true || ... || ... || ... || least_likely_to_be_true ) EDIT: wow, a lot of posts at once O_O |
Re: breaking up an if statement into multiple if statements?
I usually put statements that doesn't require a native call before the other one.
In the last example you gave, i would for example put "ent > SETTING_MAXPLAYERS || g_round_restart" at first. |
Re: breaking up an if statement into multiple if statements?
i always kinda had a feeling that once it hit something that caused the statement to be true or whatever that it wouldnt bother checking the rest of it since its going to be true anyway..
but for some reason i never thought of ordering it like that.. good call! :D |
| All times are GMT -4. The time now is 02:17. |
Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.