hey I'm looking for are clean solution to this problem:
i start the loop with i = 0
in the second loop step the i = 1
, then i = -1
and then i = 2
ect.
how to programm this with a for
loop in a clean way?
f(0); //do stuff with 0 for(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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With