How i can stop a loop correctly? +REP!!
#1

I made this code, i want about if it found one empty slot, the loop stop, how i can do it?

pawn Код:
CMD:comprarveh(playerid, params[])
{
if(sscanf(params,"d", params[0])) { } else {
if(params[0] >= 400 && params[0] <= 611)
{
new Variable, solicitud[254];
for(new i; i < MAX_VEHICLES; i++)
{
format(solicitud, sizeof(solicitud), "SELECT * FROM `vehiculos` WHERE %d", i);
mysql_pquery(mysql, solicitud, "ProcesarCompra", "ddd", Variable, i, params[0]);
}
}
}
}

//---------------------------------------------

stock ProcesarCompra(Variable, i, params[0])
{
new columnas, filas;
cache_get_data(columnas, filas)
if(!columnas)
{
Print("Hay un slot libre");
}
}
Reply
#2

You don't need a loop there.
pawn Код:
CMD:comprarveh(playerid, params[])
{
    if(sscanf(params,"d", params[0])) return 1;
   
    if(params[0] >= 400 && params[0] <= 611)
    {
        new Variable, solicitud[254];

        format(solicitud, sizeof(solicitud), "SELECT * FROM `vehiculos` WHERE %d", params[0]);
        mysql_pquery(mysql, solicitud, "ProcesarCompra", "dd", Variable, params[0]);

    }
}
Reply
#3

You forgot to specify the field in your condition :
Код:
SELECT * FROM `vehiculos` WHERE ?=%d
Reply
#4

To stop any loop, you can use "break":
Код:
for(new i = 0; i<10; i++)
{
	if(i == 5)
	    break;
	    
	printf("%d ", i);
}
// it will print 0, 1, 2, 3, 4
Another useful keyword is called "continue" which can be used to skip part of the loop:
Код:
for(new i = 0; i<10; i++)
{
	if(i == 5)
	    continue;
	    
	printf("%d ", i); // when i reaches 5, this will be skipped
}
// it will print 0, 1, 2, 3, 4, 6, 7, 8, 9
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)