Raised This Month: $32 Target: $400
 8% 

Loops conditions ( which one is better )


Post New Thread Reply   
 
Thread Tools Display Modes
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-04-2015 , 14:38   Re: Loops conditions ( which one is better )
Reply With Quote #11

You should try to avoid using a native in the for loop in instances when it can be called multiple times. get_maxplayers() returns a constant value that is not going to change while the loop iterates so it's safe to use a variable.

Calling native in for-loop:
PHP Code:
public Test()
{
    
//Same as for( new i = 1 ; i <= get_maxplayers() ; i++ )
    
for( new 1<= gp() ; i++ )
    {
        
server_print"Iteration %d" );
    }
}

public 
gp()
{
    
server_print"get_maxplayers() called" );
    return 
get_maxplayers();

Result:
Code:
get_maxplayers() called
Iteration 1
get_maxplayers() called
Iteration 2
get_maxplayers() called
Iteration 3
get_maxplayers() called
Iteration 4
get_maxplayers() called
Iteration 5
get_maxplayers() called
Iteration 6
get_maxplayers() called
Iteration 7
get_maxplayers() called
Iteration 8
get_maxplayers() called
Storing value in a variable
PHP Code:
public Test()
{
    new 
iMaxPlayers gp();
    for( new 
1<= iMaxPlayers i++ )
    {
        
server_print"Iteration %d" );
    }
}

public 
gp()
{
    
server_print"get_maxplayers() called" );
    return 
get_maxplayers();

Result:
Code:
get_maxplayers() called
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
__________________
Bugsy is offline
Freezo Begin
BANNED
Join Date: Mar 2014
Location: Morocco
Old 07-04-2015 , 16:32   Re: Loops conditions ( which one is better )
Reply With Quote #12

Thanks a lot Bugs, now i understand it better. Thanks again .
Freezo Begin 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:07.


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