SA-MP Forums Archive
How i can stop a loop correctly? +REP!! - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How i can stop a loop correctly? +REP!! (/showthread.php?tid=546749)



How i can stop a loop correctly? +REP!! - xHanks - 17.11.2014

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");
}
}



Re: How i can stop a loop correctly? +REP!! - TakeiT - 17.11.2014

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]);

    }
}



Re : How i can stop a loop correctly? +REP!! - Dutheil - 17.11.2014

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



Re: How i can stop a loop correctly? +REP!! - dominik523 - 17.11.2014

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