I want to use a for-loop to implement an iterative method. At the end of the loop I also want to check if max_iter is reached:
function iterative_method()
iter_max = 10
for iter in 1:iter_max
# some computations
# ...
# if converged
# break
# end
end
@assert iter!=iter_max "max iteration = $iter_max reached!"
end
Unfortunately the @assert cannot work as iter is out of scope:
julia> iterative_method()
ERROR: UndefVarError: iter not defined
Question: how to make iter visible outside the for loop block?