Could this be a infinite loop?
#1

PHP код:
    if (strcmp(tmp1"color"true) == 0)
    {
        new 
colorcolorstr[30];
        if (!
AdminCheck(playerid4)) return SendClientMessage(playeridERRORCOLOR"Usage: /gang [create|invite|kick|join|quit|setrank]");
        if (!
sscanf(tmp2"is[30]"idcolorstr))
        {
            if (
strlen(colorstr) != 6) return SendClientMessage(playeridERRORCOLOR"Invalid color code.");
            
strcat(tmp2"FF");
        }
        if (
sscanf(tmp2"ix"idcolor)) return SendClientMessage(playeridERRORCOLOR"Usage: /gang color [gangid] [colorcode]");
        
format(tmp1sizeof(tmp1), "SELECT * FROM `Gangs` WHERE `id` = %d"id);
        
result db_query(Databasetmp1);
        if (
db_num_rows(result) == 0)
        {
            
db_free_result(result);
               return 
SendClientMessage(playeridERRORCOLOR"Invalid gangid.");
        }
        
db_free_result(result);
        
format(tmp1sizeof(tmp1), "UPDATE `Gangs` SET `color` = %d WHERE `id` = %d"colorid);
        
db_query(Databasetmp1);
        
format(tmp1sizeof(tmp1), "SELECT * FROM `Gangmembers` WHERE `gangid` = %d"id);
        
result db_query(Databasetmp1);
        new 
rows db_num_rows(result);
        while (
rows)
        {
            
db_get_field_assoc(result"userid"tmp1sizeof(tmp1));
            new 
pid GetPlayerID(GetNameFromUserID(strval(tmp1)));
            if (
IsPlayerConnected(pid)) { SetPlayerColor(pidcolor); pcolor[pid] = color; }
            
db_next_row(result);
            
rows--;
        }
        
db_free_result(result);
        
SendMessageToGang(id"{8B72F2}Your gang color has changed.");
        return 
1;
    } 
I failed to understand the "0<rows"
Reply
#2

Just checked the loop part and no, it's not an infinite loop. It's just clear mathematical logic. (0 < rows) is equal to (rows > 0). Here, when the number of rows retrieved are greater than zero, the loop runs until the 'rows' variable becomes zero or less. That's why you can see "rows--" statement under the loop. For instance, you can consider this:

pawn Код:
new rows = db_num_rows(result); //Suppose that rows is now 3. (rows = 3)
        while (0 < rows) //(0 is of course less than 3, so the loop runs until 0 < 0 occurs.)
        {
            db_get_field_assoc(result, "userid", tmp1, sizeof(tmp1));
            new pid = GetPlayerID(GetNameFromUserID(strval(tmp1)));
            if (IsPlayerConnected(pid)) { SetPlayerColor(pid, color); pcolor[pid] = color; }
            db_next_row(result);
            rows--; //The value of rows keeps on decreasing.
        }
Reply
#3

Quote:
Originally Posted by Lordzy
Посмотреть сообщение
It's just clear mathematical logic. (0 < rows) is equal to (rows > 0).
"Clear". Right. Where possible I keep variables to the left and constants to the right because "do something while rows are greater than zero" sounds much better in my head than "do something while zero is less than rows".
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)