SA-MP Forums Archive
Looping - 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)
+---- Forum: Discussion (https://sampforum.blast.hk/forumdisplay.php?fid=84)
+---- Thread: Looping (/showthread.php?tid=602312)



Looping - Banditul18 - 05.03.2016

I have a question that blown my mind, what is the good looping code? Because i see a lot of them...
The foreach? This:
Code:
for(new i=0; i <= GetPlayerPoolSize(); i++)
or:
Code:
for(new i = GetPlayerPoolSize(); i > -1; i--)
What is the good and optimized?


Re: Looping - Slice - 05.03.2016

You could just save the return value of GetPlayerPoolSize in a global variable every time a player connects/disconnects. Given that you don't know the complexity of GetPlayerPoolSize, you shouldn't make assumptions about it without testing.

The best way to figure whether or not something is efficient, you should just test it yourself.


Re: Looping - Slice - 05.03.2016

You can use the magic --> operator.

pawn Code:
new i = GetPlayerPoolSize() - 1;

while (i --> 0) {
// code..
}



Re: Looping - SickAttack - 06.03.2016

Don't complicate your life, there's no difference in a single loop as such (nanoseconds, nothing you should be worried about).

pawn Code:
for(new i = 0, j = GetPlayerPoolSize(); i <= j; i ++)