Python indentation deters newbies? beliavsky at
Tue Aug 17 02:50:47 CEST 2004

Roy Smith <roy at> wrote:
>In article <41214271_1 at>,
> "beliavsky at" <beliavsky at> wrote:
>> Jorge Godoy <godoy at> wrote:
>> >Just an attempt and trying to keep it like your code. 
>> Thanks. My code did not correctly illustrate breaking out of more than
>> level of loop. How would the following code be translated to Python?
>Ugh.  This is a truly ugly scenario.  You've got three nested loops, 
>with each inner loop having a break out of both itself and the 
>immediately containing loop.  Does this correspond to some real-world 
>algorithm, or is it just a diabolical scenario invented for the example?

Ok, here is something short but more realistic and IMO not "ugly".

You may want to exit a nested loop when testing if a condition involving
several variables is met, such as searching for a zero of a multivariate

In Python you can print i,j,k and exit() when m == 0, but in a larger program
you may want more control.

program xnest_loop
! find a Pythagorean triple
n = 5
ido: do i=1,n
   do j=1,n
      ij = i**2 + j**2
      do k=1,n
         m = ij - k**2
         if (m == 0) exit ido
      end do
   end do
end do ido
if (m == 0) then
   print*,"no triple"
end if
end program xnest_loop

There is a thread "Labeled loop break/continue" in comp.std.c about adding
similar functionality to C. Fortran has had a big head start over most other
languages, but they may catch up eventually :).

----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---

More information about the Python-list mailing list