[Doubt] Loop's
#1

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 
0101i++)
{
   if(Array[
i] == 1)
   {
      
Value[i] = // 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?
   
}

Reply
#2

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
Reply
#3

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 0sizeof(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.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)