473,657 Members | 2,582 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

break/continue - newbe

Ann
I have trouble sometimes figuring out where
break and continue go to. Is there some easy
way to figure it out, or a tool?
TIA
Ann
Jul 18 '05 #1
5 2534
Ann wrote:
I have trouble sometimes figuring out where
break and continue go to. Is there some easy
way to figure it out, or a tool?


Break and continue always operate on the most-nested loop that's
currently executing. To show an example, let's add some line numbers to
some code...

1) while spam:
2) if foo(spam):
3) continue
4) for n in range(spam):
5) if bar(n):
6) break
7) results.append( baz(spam, n))
8) spam = spam - 1

Now, when this loop runs, when foo(spam) evaluates as True we execute
the continue at line 3. At this time, we're running code that's at the
loop-nesting level just inside 'while spam' (line 1), so line 1 is the
loop statement that's affected by the continue on line 3. If line 3 is
triggered, then we skip lines 4-8 and go back to line 1.

If line 3 is *not* triggered, then we enter a for loop at line 4.
There's a break at line 6; if this gets triggered, then we look
backwards to find the most-current (i.e. most nested) loop, which is now
that for loop on line 4. So we break out of that for loop (which
comprises lines 4-7), and drop down to line 8.

So, in general, the way to determine how break and continue will affect
program flow is to look backwards (up) for the most recent loop
statement; the break/continue will be inside that statement's dependent
body. Break will drop you down to the next line after the loop body,
and continue will bring you back up to the top of the loop body and the
start of the next loop iteration.

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #2
Ann

"Jeff Shannon" <je**@ccvcorp.c om> wrote in message
news:10******** *****@corp.supe rnews.com...
Ann wrote:
I have trouble sometimes figuring out where
break and continue go to. Is there some easy
way to figure it out, or a tool?


Break and continue always operate on the most-nested loop that's
currently executing. To show an example, let's add some line numbers to
some code...

1) while spam:
2) if foo(spam):
3) continue
4) for n in range(spam):
5) if bar(n):
6) break
7) results.append( baz(spam, n))
8) spam = spam - 1

Now, when this loop runs, when foo(spam) evaluates as True we execute
the continue at line 3. At this time, we're running code that's at the
loop-nesting level just inside 'while spam' (line 1), so line 1 is the
loop statement that's affected by the continue on line 3. If line 3 is
triggered, then we skip lines 4-8 and go back to line 1.

If line 3 is *not* triggered, then we enter a for loop at line 4.
There's a break at line 6; if this gets triggered, then we look
backwards to find the most-current (i.e. most nested) loop, which is now
that for loop on line 4. So we break out of that for loop (which
comprises lines 4-7), and drop down to line 8.

So, in general, the way to determine how break and continue will affect
program flow is to look backwards (up) for the most recent loop
statement; the break/continue will be inside that statement's dependent
body. Break will drop you down to the next line after the loop body,
and continue will bring you back up to the top of the loop body and the
start of the next loop iteration.

Jeff Shannon
Technician/Programmer
Credit International

Thanks Jeff, that solves my problem. BTW: is there an easy way to
break/continue out more than one level?
Jul 18 '05 #3
"Ann" <An*@nospam.inv alid> wrote:
Thanks Jeff, that solves my problem. BTW: is there an easy way to
break/continue out more than one level?


not really; to continue more than one level, you have to break out of the
inner loop, and make sure the logic in that loop body brings you back to
the top.

to break more than one level, you can:

1) use an exception

class LeaveLoop(Excep tion): pass

def myfunction():
try:
for a in ...:
for b in ...:
if c:
raise LeaveLoop # break out of both loops
do something
except LeaveLoop:
pass

2) organize your code so you can use return rather than "multi-break"

def myfunction():
calculate()

def calculate():
for a in ...:
for b in ...:
if c:
return
do something

3) use flags

def myfunction():
run = 1
for a in ...:
for b in ...:
if c:
run = 0
if not run:
break
if not run:
break

Solution 2 is almost always the best way to do this; solution 3 is often the worst.

</F>

Jul 18 '05 #4

Ann wrote:
I have trouble sometimes figuring out where
break and continue go to. Is there some easy
way to figure it out, or a tool?
TIA
Ann


You need a tool called py2ftn. It would convert statements like "if a >
b: break" to e.g. "IF(A.GT.B)GOTO 12345".

A longer example; this:

while 1:
blah1
if cond1: break
blah2
if cond2: continue
blah3

becomes:

10000 IF(1)10001,1000 3,10001
10001 CONTINUE
BLAH1
IF(COND1)GOTO10 003
BLAH2
IF(COND2)GOTO10 002
BLAH3
10002 CONTINUE
GOTO10000
10003 CONTINUE

HTH,
John

Jul 18 '05 #5

Ann wrote:
I have trouble sometimes figuring out where
break and continue go to. Is there some easy
way to figure it out, or a tool?
TIA
Ann


You need a tool called py2ftn. It would convert a sequence of
statements like:

while 1:
blah1
if cond1: break
blah2
if cond2: continue
blah3

to:

10000 IF(1)10001,1000 3,10001
10001 CONTINUE
BLAH1
IF(COND1)GOTO10 003
BLAH2
IF(COND2)GOTO10 002
BLAH3
10002 CONTINUE
GOTO10000
10003 CONTINUE

HTH,
John

Jul 18 '05 #6

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

Similar topics

5
2607
by: Glen Wheeler | last post by:
Hello All. I've been coding in python for a reasonable amount of time now (coding in total for approx. 20 years) and am writing a performance intensive program in python. The problem I'm having is I want to use break and continue to get out from more than one level of loop. If I set a variable and then check it my program will take too much of a performance hit. Are there any alternatives people can think of?
3
2047
by: Jianli Shen | last post by:
const DInst *stopAtDst = 0; while (dinst->hasPending()) { if (stopAtDst == dinst->getFirstPending()) break; ///////break? break? break? is this break outof the whole while loop and never in the loop again??????????????????????? DInst *dstReady = dinst->getNextPending();
5
33705
by: viza | last post by:
Hi! Suppose I have int i,j,k; for(i=0;i<I;++i){ /* loop 1 */ for(j=0;j<J;++j){ /* loop 2 */ for(k=0;k<K;++k){ /* loop 3 */ if(test){
25
3905
by: chunhui_true | last post by:
In <<expert c>>I know the break in if wich is scoped in switch is break the switch,like: switch c case 1: if(b){ break; } ...... But like this: while(a){
5
40239
by: tony collier | last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK I have only seen RETURN used in functions. Does anyone know why RETURN is used instead of BREAK to kill loops?
14
6246
by: serrand | last post by:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did... int res; while (1) { res = 0; if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1) { aff_erreurs ("msgrcv", "Error when recieving message : %d", errno);
26
10202
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
26
3214
by: a.mil | last post by:
I am programming for code-speed, not for ansi or other nice-guy stuff and I encountered the following problem: When I have a for loop like this: b=b0; for (a=0,i=0;i<100;i++,b--) { if (b%i) continue; a=1; }
1
2649
by: Matt Chisholm | last post by:
Hi. I was wondering if there had ever been an official decision on the idea of adding labeled break and continue functionality to Python. I've found a few places where the idea has come up, in the context of named code blocks: http://groups.google.com/group/comp.lang.python/browse_thread/thread/a696624c92b91181/065b1dbc13ec2807?lnk=gst&q=labeled+break&rnum=1#065b1dbc13ec2807 and in the context of discussing do/while loops and...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
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...
0
7324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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
4302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1953
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1611
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.