Perl Monk, Perl Meditation  
PerlMonks 
Re: An exploration of higherlevellanguage approaches to the "First allzero row in an NxN matrix" problem (goto Considered Harmful)by ambrus (Abbot) 
on Sep 25, 2011 at 07:47 UTC ( #927717=note: print w/ replies, xml )  Need Help?? 
I am quite partial to the while with stop variable solution. This can be coded easily in almost any language in a gotoless way, and C compilers these days should be able to optimize the stop variables away so it's as efficient as the solution with goto. It is also easy to derive by hand from the version using last. Here's a perl implementation, but written in such a style that it's easy to translate to other languages. I show two versions. Either version of the function returns the index of the first allzero row, or 1 if none of the rows are nonzero.
A more difficult example for using this style can be found in the notebook info1gy4 (fallback if previous link is broken), near the middle. This I wrote for an introduction to programming course (though here we're interested in the extra material we haven't really covered on the course). The programs are in babyPython. The problem solved is the following. (I encourage you all to try to write your own solutions for this one.) We would like to demonstrate a theorem of Gauss that says any natural number can be written as a sum of three triangular numbers. The triangular numbers are the numbers of form n*(n+1)/2 where n is an integer. For this, we want to print one such decomposition to three triangular numbers for each number up to 80. You must not print more than one solution per number, because that would degenerate to a wall of text, and it would also be hard to see whether any number is skipped. The two relevant solutions, translated to perl for your convenience, can be found in Write any natural number as sum of three triangular numbers (second and third solutions). (The output of the two solutions differ, but they're equally valid.) Update: moved triangular numbers example code from this reply to separate meditation.
In Section
Meditations

