[Doubt] Loop's - 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: [Doubt] Loop's (
/showthread.php?tid=661674)
[Doubt] Loop's -
ApolloScripter - 10.12.2018
I have a question about loops, the value that does a loop generate from 1 to 10 for example, and is it checked in a decision statement as the IF is a specific value, or is the all loop?
Example:
PHP код:
new Array[100];
new Value[100];
Array[57]Â =Â 1;
Array[32]Â =Â 1;
for(new i = 0; i < 101; i++)
{
   if(Array[i] == 1)
   {
      Value[i] = 1 // This "i" will be only "57" and "32" or will be alll loop Value from 0 to 100, saving "1" in all spaces of the array?
   }
}Â
Re: [Doubt] Loop's -
ItsRobinson - 10.12.2018
Yeah, in theory, if you printed everything inside of Value array, it should print 0 up until 31 (count starts at 0) then more 0's until 56 (count starts at 0) where it would write another 1.
If that makes any sense
Re: [Doubt] Loop's -
Threshold - 11.12.2018
Only when i = 32 and i = 57 will the 'if' statement be triggered.
The statement will be called 100 times, but it is only 'true' when 'i' is equal to 32 or 57.
Eg.
PHP код:
if(Array[0] == 1) // false, skip
if(Array[1] == 1) // false, skip
if(Array[2] == 1) // false, skip
//Â ...
if(Array[31] == 1) // false, skip
if(Array[32] == 1) // true, continue!
{
    Value[32] = 1;
}
if(Array[33] == 1) // false, skip
if(Array[34] == 1) // false, skip
//Â ...
if(Array[56] == 1) // false, skip
if(Array[57] == 1) // true, continue!
{
    Value[57] = 1;
}
if(Array[58] == 1) // false, skipÂ
Also, another trick when doing loops like this is to use 'sizeof'.
PHP код:
for(new i = 0; i < sizeof(Array); i++)Â
https://sampwiki.blast.hk/wiki/Keywords:Operators#sizeof
Your code would actually cause an 'Out of Bounds Error', because it iterates through 0 to 100, but an array with a size of 100 actually uses the indexes 0 to 99.