Raised This Month: $ Target: $400
 0% 

breaking up an if statement into multiple if statements?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
diamond-optic
Veteran Member
Join Date: May 2005
Old 04-05-2009 , 15:04   breaking up an if statement into multiple if statements?
Reply With Quote #1

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:
if(!this || !that || !something || !etc || whatever || evenmore || !yadayada)     return FMRES_IGNORED


im being told by someone that this is a better way to do it:
Code:
if(!this || !that)     return FMRES_IGNORED if(!something || !etc)     return FMRES_IGNORED if(whatever || evenmore)     return FMRES_IGNORED if(!yadayada)     return FMRES_IGNORED



so just hoping to get some clarification on this...
__________________
diamond-optic is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-05-2009 , 15:11   Re: breaking up an if statement into multiple if statements?
Reply With Quote #2

First one is just fine, if this == 0, other statements won't be checked.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-05-2009 , 15:17   Re: breaking up an if statement into multiple if statements?
Reply With Quote #3

I was going to say something to that effect but I was looking for where I read it . It was a post by Exolent.
__________________
fysiks is offline
stupok
Veteran Member
Join Date: Feb 2006
Old 04-05-2009 , 15:20   Re: breaking up an if statement into multiple if statements?
Reply With Quote #4

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
__________________
stupok is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-05-2009 , 15:18   Re: breaking up an if statement into multiple if statements?
Reply With Quote #5

A switch is better to use when you have if statements like this:

PHP Code:
if ( == 
    
//do something
else if ( == )
    
//do something 
else if ( == )
    
//do something 
If you have multiple if checks it is better to use a switch statement.

PHP Code:
switch ( var )
{
    case 
1//do something
    
case 2,3,4
    {
        
//do stuff
    
}
    case 
5//do something
    
case 6,7,8//do stuff
    
default: //Nothing hit, do stuff

__________________
Bugsy is offline
diamond-optic
Veteran Member
Join Date: May 2005
Old 04-05-2009 , 15:20   Re: breaking up an if statement into multiple if statements?
Reply With Quote #6

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:
if(!get_pcvar_num(p_whatever) || !is_user_connected(id) || is_user_bot(id) || !is_user_alive(id) || ent > maxplayers || g_round_restart)     return HAM_IGNORED
__________________
diamond-optic is offline
ConnorMcLeod
Veteran Member
Join Date: Jul 2006
Location: France (95)
Old 04-05-2009 , 15:21   Re: breaking up an if statement into multiple if statements?
Reply With Quote #7

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.
__________________
- tired and retired -

- my plugins -
ConnorMcLeod is offline
diamond-optic
Veteran Member
Join Date: May 2005
Old 04-05-2009 , 15:24   Re: breaking up an if statement into multiple if statements?
Reply With Quote #8

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!
__________________
diamond-optic is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 02:17.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode