473,807 Members | 2,853 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

multiple breaks

TP
Hi everybody,

Several means to escape a nested loop are given here:

http://stackoverflow.com/questions/1...oops-in-python

According to this page, the best way is to modify the loop by affecting the
variables that are tested in the loops. Otherwise, use exception:

"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."

In the following example, is this possible to affect the two iterators to
escape the two loops once one "j" has been printed:

for i in range(5):
for j in range(i):
print j
# I would type "break 2" in shell bash
# In C, I would set j=i-1 and i=4
# In Python, is this possible to affect the two iterators?

Or the only means is to use exception?

Thanks in advance

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+ ,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
Nov 13 '08 #1
5 1745
On Thu, Nov 13, 2008 at 2:07 AM, TP <Tr**********@p aralleles.inval idwrote:
Hi everybody,

Several means to escape a nested loop are given here:

http://stackoverflow.com/questions/1...oops-in-python

According to this page, the best way is to modify the loop by affecting the
variables that are tested in the loops. Otherwise, use exception:

"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."

In the following example, is this possible to affect the two iterators to
escape the two loops once one "j" has been printed:
Non-exception alternative:

done = False
for i in range(5):
for j in range(i):
print j
done = True
break
# I would type "break 2" in shell bash
# In C, I would set j=i-1 and i=4
# In Python, is this possible to affect the two iterators?
if done:
break
>
Or the only means is to use exception?
No, you could add a boolean variable and a break condition like above.

Cheers,
Chris
--
Follow the path of the Iguana...
http://rebertia.com
>
Thanks in advance

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+ ,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
--
http://mail.python.org/mailman/listinfo/python-list
Nov 13 '08 #2
On Thu, 13 Nov 2008 11:07:25 +0100, TP wrote:
According to this page, the best way is to modify the loop by affecting
the variables that are tested in the loops. Otherwise, use exception:

"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."

In the following example, is this possible to affect the two iterators
to escape the two loops once one "j" has been printed:

for i in range(5):
for j in range(i):
print j
# I would type "break 2" in shell bash # In C, I would set j=i-1
and i=4
# In Python, is this possible to affect the two iterators?

Or the only means is to use exception?
You could put the code into its own, maybe local, function and use
``return``.

Ciao,
Marc 'BlackJack' Rintsch
Nov 13 '08 #3
TP wrote:
Hi everybody,

Several means to escape a nested loop are given here:

http://stackoverflow.com/questions/1...oops-in-python
>
According to this page, the best way is to modify the loop by affecting
the variables that are tested in the loops. Otherwise, use exception:

"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."

In the following example, is this possible to affect the two iterators to
escape the two loops once one "j" has been printed:

for i in range(5):
for j in range(i):
print j
# I would type "break 2" in shell bash
# In C, I would set j=i-1 and i=4
# In Python, is this possible to affect the two iterators?

Or the only means is to use exception?
Here's one way to turn multiple iterators into a single one:

def pairs(n):
for i in range(n):
for k in range(i):
yield i, k

for i, k in pairs(5):
print i, k

I've yet to see an example where a multi-level break would improve the
code's readability. It is typically a speed hack.

Peter

PS: Have a look into the itertools if you consider that approach.
Nov 13 '08 #4
Chris Rebert wrote:
On Thu, Nov 13, 2008 at 2:07 AM, TP <Tr**********@p aralleles.inval idwrote:
>Hi everybody,

Several means to escape a nested loop are given here:

http://stackoverflow.com/questions/1...oops-in-python

According to this page, the best way is to modify the loop by affecting the
variables that are tested in the loops. Otherwise, use exception:

"If, for some reason, the terminating conditions can't be worked out,
exceptions are a fall-back plan."

In the following example, is this possible to affect the two iterators to
escape the two loops once one "j" has been printed:

Non-exception alternative:

done = False
>for i in range(5):
for j in range(i):
print j
done = True
break
> # I would type "break 2" in shell bash
# In C, I would set j=i-1 and i=4
# In Python, is this possible to affect the two iterators?
if done:
break
>Or the only means is to use exception?

No, you could add a boolean variable and a break condition like above.
Though I would have to ask why you would want to. An exception seems
rather cleaner, though of course tastes vary.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Nov 13 '08 #5
On Thu, 13 Nov 2008 02:16:32 -0800, Chris Rebert wrote:
On Thu, Nov 13, 2008 at 2:07 AM, TP <Tr**********@p aralleles.inval id>
wrote:
>>
In the following example, is this possible to affect the two iterators
to escape the two loops once one "j" has been printed:
Non-exception alternative:

done = False
for i in range(5):
for j in range(i):
print j
done = True
break
if done:
break
Another alternative is explicitly jumping in the outer loop::

for i in range(5): # use xrange for larger ranges
for j in range(i):
print j
break
else:
continue # applies already to the outer loop
break

HTH,

--
Robert "Stargaming " Lehmann
Nov 15 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13250
by: Jon LaRosa | last post by:
I'm am trying to remove multiple line breaks ("\n"). Here's my code: $imageStr = trim($_POST); $imageStr = str_replace("\n\n", "\n", $imageStr); ---------- $_POST is a textarea field, and I am trying to remove extra hard returns that a user types into the text area.
0
1474
by: Ralph Freshour | last post by:
I'm not sure the follow multiple table query is the right way to do what I need to do although it seems to be working: $php_SQL = "SELECT * ". "FROM basics, personal, photos ". "WHERE basics.member_name = personal.member_name ". "AND basics.member_name = photos.member_name ". "AND basics.account_creation_date >= DATE_SUB(NOW(), INTERVAL 30 DAY)";
1
3515
by: Rob | last post by:
I have an ASP.NET application that uses forms-based authentication. A user wishes to be able to run multiple sessions of this application simultaneously from the user's client machine. The web.config file is configured as such: <authentication mode="Forms"> <forms loginUrl="Login.aspx" protection="All" name="myApplication"/> </authentication>
10
2141
by: Rich Kucera | last post by:
Holding all versions at 5.0.4, Multiple stacks with multiple-version configurations inevitable Will have to wait to see what the impact of problems such as http://bugs.php.net/bug.php?id=33643 to the application world. We may wait for a suitable popular resolution and jump to that version in the future. There's already a rift between PHP4 and PHP5, and then further developments such as these create another, splitting the camp into 4....
5
6520
by: cvisal | last post by:
Hi all Im working on productivity calculations (Time calculations) and need some help in coding. Database Tool:MS-Access 2003. The general operator punch-in time is 5:30 AM and the punch-out time is 2:00PM. The Break times are 1) 9:30 AM to 9:45 AM 2) 11:00AM to 11:30 AM 3) 12:30PM to 12:45 PM
4
2806
by: cimple | last post by:
I've always made sure never to have multiple return paths in a function because i thought it produced bigger code (and for readability). I remember Lippman from Inside the Object Model saying something about this. For every return point in a function, destructor calls for every object created on the stack needs to be called. But when i tested this, i saw no increase in executable size in release builds. Is my compiler (VS7.0, Win2k) just...
4
1759
by: m.shidoshi | last post by:
I was recently put in charge of heading up my company's website, and while I have a lot of experience on the design side of things, I'm still very new to the programming side. When I started, the website had just gotten a revision, but the site was an utter mess, so I've been trying to fix it up. As I've gone along, I've learned some aspects of PHP, but my knowledge is still very limited. Here is the problem I'm trying to fix. A backend...
33
1022
by: Geometer | last post by:
Hello, and good whatever daytime is at your place.. please can somebody tell me, what the standard behavior of strtok shall be, if it encounters two or more consecutive delimiters like in (checks omitted) char tst = "this\nis\n\nan\nempty\n\n\nline"; ^^^^ ^^^^^^ char *tok = strtok(tst, "\n");
3
2798
by: Bart Van der Donck | last post by:
Hello, I'm having a hard time trying to configure printed output that consists of multiple pages. The idea is the following: <div style=" border: 1px solid blue; position: absolute; top: 20px; left: 20px;
5
5824
by: jbedwe02 | last post by:
Hi all, I have no clue how to slove this problem & am desperate for an answer: I'm creating a spreadsheet and need to now how to seperate data within a single cell into multiple cells. The info is currently stored in a single cell using line breaks and I want to seperate the info in each line break into specific cells. Example: red
0
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10112
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5685
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3854
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.