Python indentation deters newbies?
beliavsky@aol.com
beliavsky at 127.0.0.1
Mon Aug 16 20:50:47 EDT 2004
Roy Smith <roy at panix.com> wrote:
>In article <41214271_1 at 127.0.0.1>,
> "beliavsky at aol.com" <beliavsky at 127.0.0.1:7501> wrote:
>
>> Jorge Godoy <godoy at ieee.org> wrote:
>>
>> >Just an attempt and trying to keep it like your code.
>>
>> Thanks. My code did not correctly illustrate breaking out of more than
one
>> 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
function.
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*,i,j,k
else
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 :).
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&c2coff=1&frame=right&th=b90ddd6cd83f7232&seekm=lnr7qds9r6.fsf%40nuthaus.mib.org#link1
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com 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