# Error.10 in for loop

Loops are obsolete, learn arrays!,

but at the beginning of developing a new idea,
I'm always make a for loop first,

``````function dothings( k1234z )
{
ret = 0; // result
barNo_case1 = 0; v_case1 = 0;
barNo_case2 = 0; v_case2 = 0;
barNo_case3 = 0; v_case3 = 0;
for( i = BarCount; i >= 0; i-- )
{
if ( k1234z[i] == 1 ) { barNo_case1 = i;  v_case1 = O[i]; }
// Error 10. Array subscript out of range. You must not access array elements outside 0..(BarCount-1) range. You attempted to access non-existing 200-th element of array..
if ( k1234z[i] == 2 ) { barNo_case2 = i;  v_case2 = O[i]; }
// Error 10. Array subscript out of range. You must not access array elements outside 0..(BarCount-1) range. You attempted to access non-existing 200-th element of array..
// ...
}
// base on barNo_case* and v_case* above to do something much complex
// finally generate result array named 'ret' ...
return ret; // result
}
k1234z = IIf(O<C, 1, IIf(O==C,2, 3)); // create some cases
k1234z = dothings( k1234z ); // do something complex base on cases above
``````

Does changing that to `for( i = BarCount - 1; i >= 0; i-- )` give you the desired output?

1 Like

Yes!
I need to check values from the right-side to the left (from BarCount-1 to 0).

For example,
after recording values on the right-side (ex. bar 1000=a, bar 900=b, bar 800=c),
then I can decide some values (ex. 5 values),
when this loop go to the left-side (ex. bar 700).

Such requirement seems complex...
Normally, single line array processing statement can assign one value only,
for example,` barNo_case1 = ValueWhen(k1234z == 1, BarIndex());`

but I need to record some values on the right-side first,
then I can assign some other values (ex. 5 values) when it matches my case,
so I try to deal it with a reversed for loop.

Since there is not much of detail presented in regards to what you exactly want to decipher, it leaves lot of room for assumptions.

It seems to me that you are trying to tackle a self-referencing or recursive function. These posts of Tomasz might help:

Array subscript out of range. You must not access array elements outside 0..(BarCount-1) range. You attempted to access non-existing 200-th element of array

If an array has `5` elements. And you write `for( i = 5; i >= 0; i-- )`. It means that you are asking the compiler to loop:

1st Loop i = 5
2nd Loop i = 4 (since you're using i--)
3rd Loop i = 3
4th Loop i = 2
5th Loop i = 1
6th Loop i = 0 (since you wrote i>= 0)

But the array has 5 elements only, compiler will throw an error right away as you are trying to access something that does not exist.

Now if you write `for( i = 4; i >= 0; i-- )`. It means you are asking the compiler to loop and it will work fine without an error (obviuosly):

1st Loop i = 4
2nd Loop i = 3
3rd Loop i = 2
4th Loop i = 1
5th Loop i = 0

That's why `for( i = BarCount - 1; i >= 0; i-- )` does not throw an error. You have set 200 bars (0 to 199) as your preference:

So, when you write `i = BarCount` it access the `i = 200th` bar which obviously does not exist as the count starts from `0`.

Quoting from Understanding how AFL works:

All array indices in AFL are zero-based, i.e. counting bars starting from bar 0 (oldest). The latest (newest) bar has an index of (BarCount-1)

(Source: QuickAFL facts)

Still assuming by the way!
You might want to try nested for-loop, but, such a loop needs to be handled carefully:

``````n = 5;
for( i = BarCount - 1; i >= 0; i-- ) {
//...
for( j = i - n; j >= n && j <= BarCount - 1; j++ ) {
//...
}
//...
}
``````
2 Likes

You are very correct!

I've found that my mistake is to take `i = BarCount;`
and the correct way is to take `i = BarCount - 1;`

You solved my problem (Verify passed), Thank you very much!!

1 Like