Which is the best implementation for checking a value alternating +1 and -1?
Example source = 15
Call the method, source as parameter
source = 15 and if false then
source = 14 if false
source = 16 if false
source = 13 if false
source = 17
…
until source is 0 or a maximum predefined

method
GetIfTrue(source) then
// DONE return true
// EXIT
else
continue return false
end if

var source as integer = 15
Var minimum as integer = 0
var maximun as integer = 25
var checkUp, checkDown as integer
var r as Boolean = false
for i as integer = minimum to maximun
if i mod 2 = 0 then
r = CheckValue(source + checkUp)
checkUp = checkUp + 1
else
r = CheckValue(source + checkDown)
checkDown = checkDown - 1
end if
if r = true then exit
next

var source as integer = 18 // valor inicial a considerar
var valueToCheck as integer
Var minimum as integer = 0 // columna
var maximun as integer = 25 // columna
var checkUp, checkDown as integer
var r as integer = -1
for i as integer = minimum to maximun
if i mod 2 = 0 then
valueToCheck = valueToCheck + checkUp
if valueToCheck > maximun then Continue
checkUp = checkUp + 1
else
valueToCheck = valueToCheck + checkDown
if valueToCheck < minimum then Continue
checkDown = checkDown - 1
end if
r = CheckValue(valueToCheck)
if r <> -1 then exit
next
MessageBox r.ToText

CheckValue return an integer -1 fail, or the value that match
But I believe it could be improved

var n , nMin, nMax as integer
const theMax as integer = 20
var nStart as integer = 15
while nMin > 0 and nMax < theMax
n = n + 1
if n mod 2 = 0 then
nMin = nMin -1
else
nMax = nMax + 1
end if
wend
//by this point, you have reached 0 or theMax

Hi Enric (sorry for my english), please consider if this procedure can be useful to you. it’s a pretty fast algorithm because it doesn’t use mathematical functions like mod, but only addition and subtraction.
Regards

I don’t understand why you are using a recursive approach when the intermediate steps don’t seem to be necessary. If you just calculate the distances of the initial value to 0 and to the upper limit, you know which one will be reached earlier.
Or I did not understand your question. In that case sorry!

Imagine a grid of squares where columns are A,B,C, etc. and rows are 0,1,2,3

You need to find the short route from any position on the grid to the row 0. (ex. C23)
The shorter path should always be the same column, so, first, try to reach C0.
But it could be some obstacles in the grid, so if you could not reach the C0 in the example, the shorter to try will be D0 and if this fails B0, and so on,