3

How do I "restart" my function when an if condition is or isn't met.
In this pseudocode, if the if condition is met, I would like to stop what the function was doing and restart again.

function example()

     .
     .
     .

 for i in 1:N
        for j in 1:N
            if Mat[i,j] > 1
                Mat[i,j] += restart? # Here the process should restart,
            end
        end
    end

1 Answers1

2

Rather than adding additional branching and complicating a function, return values offer a great ability for control flow in I.M.H.O. much cleaner fashion. Adding a little separate function is simple

function do_thing_once!(mat)
    # Returns true if successful
    for i in 1:N
        for j in 1:N
            if mat[i,j] > 1
                mat[i,j] += 123
                return false
            end
        end
    end
    return true
end

function do_all_things!(mat)
    success = false
    while !success
        success = do_thing_once!(mat)
    end
end
Mikael Öhman
  • 2,294
  • 15
  • 21