Raised This Month: $ Target: $400
 0% 

if elseif else?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
vip-colgate
Junior Member
Join Date: Jan 2010
Location: Game World
Old 05-26-2013 , 22:59   if elseif else?
Reply With Quote #1

I knew that for a long time ago...
Code:
if( one ) ...
else if( two ) ...
else ...
and...
Code:
if( one ) ... return
if( two ) ... return
...
Can someone tell me what's the difference between them?
Which one is better? or no matter?
__________________
vip-colgate is offline
Blizzard_87
Veteran Member
Join Date: Oct 2012
Old 05-26-2013 , 23:04   Re: if elseif else?
Reply With Quote #2

this may not be best explanation but here goes.

PHP Code:
if( hasadmin1[id] ) {
    
execute command.... // if player hasadmin1 then command is done and others are ignored
}
else if( 
hasadmin2id ] ) {
    
execute command.... // if player does NOT have admin1 then this check is next....
}
else {
    
execute command... // if player does NOT have admin1 or admin2 then this command is executed.
}



if( 
hasadmin1id ] ) execute command....

if( 
hasadmin2id ] ) execute command....
// both of these checks will happen and if player has both admin1 and admin2 then both commands will execute . 
__________________
Blizzard_87 is offline
vip-colgate
Junior Member
Join Date: Jan 2010
Location: Game World
Old 05-26-2013 , 23:10   Re: if elseif else?
Reply With Quote #3

Are you sure?
Code:
if( one ) { ...; return; }
if( two ) { ...; return; }
...
__________________
vip-colgate is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 05-26-2013 , 23:19   Re: if elseif else?
Reply With Quote #4

Quote:
Originally Posted by vip-colgate View Post
Are you sure?
Code:
if( one ) { ...; return; }
if( two ) { ...; return; }
...

Yes.
ddhoward is offline
Blizzard_87
Veteran Member
Join Date: Oct 2012
Old 05-26-2013 , 23:54   Re: if elseif else?
Reply With Quote #5

Quote:
Originally Posted by vip-colgate View Post
Are you sure?
Code:
if( one ) { ...; return; }
if( two ) { ...; return; }
...
if your returning a value after the first IF then the others wont be checked.

but if your if check has more then 2 else if you should use a switch its better.

if( bla ){
...
}
else {
......
}

is ok for checking one or two things.
__________________
Blizzard_87 is offline
^SmileY
Veteran Member
Join Date: Jan 2010
Location: Brazil [<o>]
Old 05-27-2013 , 08:16   Re: if elseif else?
Reply With Quote #6

IF you use only these statements for a function, you not need to use any return, like this:

PHP Code:
public client_authorized(id)
{
    if(
is_user_admin(id))
    {
        
g_Type 1;
    }
    else if(
is_user_hltv(id))
    {
        
g_Type 2;
    }
    else
    {
        
g_Type 0;
    }

__________________
Projects:

- See my Git Hub: https://github.com/SmileYzn
PHP Code:
set_pcvar_num(pCvar, !get_pcvar_num(pCvar)); 

Last edited by ^SmileY; 05-27-2013 at 08:16.
^SmileY is offline
Send a message via MSN to ^SmileY Send a message via Skype™ to ^SmileY
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 05-28-2013 , 00:18   Re: if elseif else?
Reply With Quote #7

Quote:
Originally Posted by vip-colgate View Post
I knew that for a long time ago...
Code:
if( one ) ...
else if( two ) ...
else ...
and...
Code:
if( one ) ... return
if( two ) ... return
...
Can someone tell me what's the difference between them?
Which one is better? or no matter?
The latter (second one) is poor coding and might not work in all situations (i.e. it is context dependent). Always use the former.
__________________

Last edited by fysiks; 05-28-2013 at 00:19.
fysiks is offline
Reply


Thread Tools
Display Modes

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 16:18.


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