You need to use return. There are 2 issues. First, in maxima the loop variable is local, and second, the return only exits one level of loop. The way that I have done this is: block([i], for ii:(i:j) to n do if M[ii][j] <> 0 then return(i:ii),