I'm having trouble developing a for-loop that starts at a given integer (start) and then counts up and down from this starting point inside a given range (min to max). I want the result to be in order by distance to the given start. If a for-loop is not the right tool here, i am open for other suggestions!
Since this is hard to explain i have a example down below:
var start = 4;
var max = 10;
var min = 0;
var stepSize = 1;
for(var i=startIndex; i>=min && i<=max; ???) {
Console.WriteLine(i)
}
The result should look like this:
4 5 3 6 2 7 1 8 0 9 10
The really tricky part for me is the end where the alternation has to stop in order to reach the max value of 10 and not go below 0 in the meantime.
I started experimenting and had a solution that would work for the first bit:
const int start = 4;
const int stepSize = 1;
for (var (i,j,k) = (start,1,5); Math.Abs(j)<=11; k=(k+j),j=-j-Math.Sign(j)*stepSize,i=(k+(start-5))%11) {
Console.WriteLine(i);
}
This works fine for the first part where alternation is needed, but it breaks once the alternation needs to stop. The result from this experiment is:
4 5 3 6 2 7 1 8 0 9 -1