for loop runaway

when i run this code (on 3 diferent arduino mega 2560) the for loop go up to “665”, return and do it again.

when i comment the “return” in the “if statement” i got : 8,9,10,11,12,13,14,15,16,17 (i want it to do so)

i dont understand since the condition of the “if statement” is false , its not supose to do anything.

for_loop_not_runaway.ino (372 Bytes)

for_loop_runaway.ino (370 Bytes)

        if(ser[j] == 1){

You are trying to access 8th to the 18th elements of a 10 element array. You can not count on correct behavior of any program that does that.

ok , thanks its working now !