looping in two directions looping in two directions php php

looping in two directions


f(0); //do stuff with 0for(var i = 1; i<len; i++) //where len = positive boundary{    f(i);  //do stuff with i    f(-i); //do stuff with -i}

Should do what you want


If you don't mind having the inner loop appear 3 times:

f(0);for (var i = 1; i <= 3; ++ i) {  f(i);  f(-i);}

2 times with an if:

for (var i = 0; i <= 3; ++ i) {  f(i);  if (i > 0)     f(-i);}

single time but with an ugly expression:

for (var j = 1; j <= 7; ++ j) {   var i = j / 2;   if (j % 2) i = -i;   f(i);}


Each loop, you appear to be adding n*(-1)^(n+1), where n is the step you are currently taking, starting at 1, and starting at i=0.

initialize i = 0n=0, i+=0*(-1)^1   # 0n=1, i+=1*(-1)^2   # 1n=2, i+=2*(-1)^3   # -1n=3, i+=3*(-1)^4   # 2

etc.

From here, it depends on what language you would wish to write in. Iterate from n = 0 to wherever you are stopping.

edit this is a bad answer. but fun =D

(I added that last bit because as soon as I made that edit, someone downvoted me =( )