Raised This Month: $ Target: $400
 0% 

for loop question.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 07-13-2010 , 22:29   for loop question.
Reply With Quote #1

I thought these 2 loops do the same thing. But the first one doesn't work like it should and the second one works fine. Could someone explain this?

1
Code:
for( new i = 1; i <= playerCount; i++ )
2
Code:
for( new i = 0; i < playerCount; i++ )
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
wrecked_
Veteran Member
Join Date: Jan 2010
Location: New York (GMT-5)
Old 07-13-2010 , 22:49   Re: for loop question.
Reply With Quote #2

Could you show us how you obtained the value of playerCount and the first line of your loop?
__________________
[ Paid Requests ]
DO NOT PM ME ABOUT BLOCKMAKER
NO PRIVATE SUPPORT
wrecked_ is offline
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 07-13-2010 , 22:51   Re: for loop question.
Reply With Quote #3

This is the original stock:
Code:
stock update_alive_count( )
{
    g_iTerroristCount = 0;
    g_iCTCount = 0;
    
    new Players[32], playerCount, id;
    get_players( Players, playerCount );
    
    for( new i = 1; i <= playerCount; i++ )
    {
        id = Players[i];
        
        if( !is_user_alive( id ) )
            continue;
        
        switch( cs_get_user_team( id ) )
        {
            case CS_TEAM_T:
            {
                g_iTerroristCount++;
            }
            case CS_TEAM_CT:
            {
                g_iCTCount++;
            }
        }
    }
}
But because that one wasn't working i made the second loop and then it worked. So i added some client_print's to see what's wrong and ended up with this.
Code:
stock update_alive_count( )
{
    g_iTerroristCount = 0;
    g_iCTCount = 0;
    
    new Players[32], playerCount, id;
    get_players( Players, playerCount );
    
    for( new i = 1; i <= playerCount; i++ )
    {
        id = Players[i];
        
        if( !is_user_alive( id ) )
            continue;
        
        switch( cs_get_user_team( id ) )
        {
            case CS_TEAM_T:
            {
                g_iTerroristCount++;
            }
            case CS_TEAM_CT:
            {
                g_iCTCount++;
            }
        }
    }
    client_print(0, print_chat, "loop 1 %i ct and %i terro", g_iCTCount, g_iTerroristCount);
    
    for( new i = 0; i < playerCount; i++ )
    {
        id = Players[i];
        
        if( !is_user_alive( id ) )
            continue;
        
        switch( cs_get_user_team( id ) )
        {
            case CS_TEAM_T:
            {
                g_iTerroristCount++;
            }
            case CS_TEAM_CT:
            {
                g_iCTCount++;
            }
        }
    }
    client_print(0, print_chat, "loop 2 %i ct and %i terro", g_iCTCount, g_iTerroristCount);
}
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
wrecked_
Veteran Member
Join Date: Jan 2010
Location: New York (GMT-5)
Old 07-13-2010 , 22:53   Re: for loop question.
Reply With Quote #4

You're setting i to 1. That means you're indexing the array with 1 first, and array indices begin at 0. This means you're skipping the first cell in Players[]. So, in short, you're skipping a player in the first method.
__________________
[ Paid Requests ]
DO NOT PM ME ABOUT BLOCKMAKER
NO PRIVATE SUPPORT
wrecked_ is offline
Alucard^
AMXX Moderator: Others
Join Date: Sep 2007
Location: Street
Old 07-13-2010 , 23:47   Re: for loop question.
Reply With Quote #5

Code:
for( new i = 1; i <= playerCount; i++ )     // if playercount is 32 for example... the reuslt is // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... 32, when "i" reach // to 33, the loop stop for( new i = 0; i < playerCount; i++ )     // if playercount is 32 for example... the result is // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10... 31, when "i" reach // to 32, the loop stop
__________________
Approved Plugins - Steam Profile

Public non-terminated projects:
All Admins Menu, HLTV parameters, Subnick,
Second Password (cool style), InfoZone,
Binary C4 plant/defuse, and more...

Private projects:
NoSpec (+menu), NV Surf Management,
PM Adanved System, KZ longjump2, and more...
Alucard^ is offline
Send a message via Skype™ to Alucard^
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 07-13-2010 , 23:52   Re: for loop question.
Reply With Quote #6

Thanks Alucard and Wrecked. i understand how it works now.
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 07-14-2010 , 00:15   Re: for loop question.
Reply With Quote #7

There are two methods commonly used to iterate through player indexes and the loops used for each are different;this may be what is confusing you. The two methods use the exact for-loops that you posted above.

1. Iterate through all valid player indexes (1 to maxplayers() [max of 32])
for ( i = 1 ; i <= maxplayers ; i++ )

2. Iterate through player indexes retrieved with get_players()
When iterating through player indexes retrieved with get_players, a loop from 0 to Num-1 is done (Num being the # of players retrieved with get_players()). This is because get_players() populates an array with player indexes and the array elements are accessed starting with 0 to num-1.
for ( i = 0 ; i < num ; i++ )
__________________

Last edited by Bugsy; 07-14-2010 at 00:31.
Bugsy is offline
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 07-14-2010 , 00:45   Re: for loop question.
Reply With Quote #8

Thanks bugsy, very usefull explanation
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 07-14-2010 , 00:48   Re: for loop question.
Reply With Quote #9

To generalize what Bugsy said, the way you set up you iterator ("i" in this case) will be determined by how you use it. The majority of the time if you are going to use it directly in an array you will start with i = 0 because in Pawn all arrays start at index zero.

So, you can start at i = 5 if you want to but, again, that depends on what you are going to use i for.
__________________
fysiks is offline
drekes
Veteran Member
Join Date: Jul 2009
Location: Vault 11
Old 07-14-2010 , 00:50   Re: for loop question.
Reply With Quote #10

Thanks Fysiks. i see what was wrong with it.
__________________

Quote:
Originally Posted by nikhilgupta345 View Post
You're retarded.
drekes is offline
Send a message via MSN to drekes
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 07:05.


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