How to jump out of several nested control structures?

S

Shawn

Hi,

I am wondering how can I jump out of several layers of loops. break can
only jump out of the inner most loop.

for ( ...) //layer 1
{
for (...) //layer 2
{
for (...) //layer 3
{
//code
if (jump==true) //How can I jump out of all the layers at once?

}
}

}
 
G

Gordon Beaton

I am wondering how can I jump out of several layers of loops. break
can only jump out of the inner most loop.

No, it can jump much further than that. Label your loops and specify a
label in the break statement:

gurka:
for (...) {
for (...) {
break gurka;
}
}

/gordon
 
S

Shawn

Patricia said:
No, break can jump out of any labeled statement enclosing it.

See
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#6842


for the syntax and an example.

Patricia

Wow. break is similar to GOTO, according to the following statements
from the link you gave to me:

"A break statement with label Identifier attempts to transfer control to
the enclosing labeled statement (§14.7) that has the same Identifier as
its label; this statement, which is called the break target, then
immediately completes normally. In this case, the break target need not
be a while, do, for, or switch statement. A break statement must refer
to a label within the immediately enclosing method or initializer block.
There are no non-local jumps."

I almost have never seen people using break as GOTO feature.
 
P

Patricia Shanahan

Shawn said:
Wow. break is similar to GOTO, according to the following statements
from the link you gave to me:

"A break statement with label Identifier attempts to transfer control to
the enclosing labeled statement (§14.7) that has the same Identifier as
its label; this statement, which is called the break target, then
immediately completes normally. In this case, the break target need not
be a while, do, for, or switch statement. A break statement must refer
to a label within the immediately enclosing method or initializer block.
There are no non-local jumps."

I almost have never seen people using break as GOTO feature.

It is very different from GOTO, because it can ONLY be used to break out
of a statement enclosing the break.

That said, I'm obviously aware of it, but I don't think I've used it so
far in my own code.

Patricia
 
S

Shawn

Patricia Shanahan wrote:

It is very different from GOTO, because it can ONLY be used to break out
of a statement enclosing the break.

"Break out of a statement enclosing the break". What does it mean? I am
sorry.

According to the cited paragraph, it doesnt' need for loop, while loop,
etc. It seems the following code is legal:

public class MyClass
{
...//code

public void myMethod()
{
...//code
if (wantToEnd) break here
...//more code


here: System.out.println("I am going to exit");
return;
}

}

Do you think the above feature is GOTO?
 
S

Shawn

Patricia said:
It is very different from GOTO, because it can ONLY be used to break out
of a statement enclosing the break.

That said, I'm obviously aware of it, but I don't think I've used it so
far in my own code.

Patricia

Sorry. I see what you mean now.

here:
{
...//many layers inside
if (jump==true) break here; //jump out of here scope
...//more code executed if not jumped

}
//reach here by jump or by normal execution
 
S

Simon Brooke

Shawn ('[email protected]') said:
I am wondering how can I jump out of several layers of loops. break can
only jump out of the inner most loop.

Define your own exception class, wrap the whole lot in a try... catch, and
throw your special exception when you want to jump out of the loop.
 
S

Simon Brooke

Shawn ('[email protected]') said:
Wow. break is similar to GOTO, according to the following statements
from the link you gave to me:

Yup, that's why I prefer the 'special exception' solution. Jumping to
labels is spaghetti programming in the making, whereas a special exception
just unwinds the stack.
 
R

Robert Klemme

Simon said:
Yup, that's why I prefer the 'special exception' solution. Jumping to
labels is spaghetti programming in the making, whereas a special exception
just unwinds the stack.

IMHO using exceptions for this is a case of abuse. From my point of
view refactoring to another method and using "return" is much cleaner -
and probably faster, too. Before I'd use an exception for a non
exceptional condition I'd rather use "break" with label. However, so
far I never felt the need for any of those. I typically use the
"return" solution or have proper loop conditions - whatever seems more
appropriate.

Regards

robert
 
M

Mark Rafn

Shawn said:
Wow. break is similar to GOTO

Nope. Break can only transfer control to an enclosing scope. It can only
go to the end of a block. GOTO can jump anywhere. This is a humongous
difference.

That said, it can be abused. I've seen code that contained
do {
stuff;
if (!something) break;
more stuff;
} while (false);

As a way to avoid putting "more stuff" into a separate method because there
were a lot of local variables that would have to be passed in and out.
There are no non-local jumps."

This is the key phrase that makes break safe and goto unsafe.
 
C

crazzybugger

Shawn said:
Hi,

I am wondering how can I jump out of several layers of loops. break can
only jump out of the inner most loop.

for ( ...) //layer 1
{
for (...) //layer 2
{
for (...) //layer 3
{
//code
if (jump==true) //How can I jump out of all the layers at once?

}
}

}

Just use the return statement with no arguements...........suppose you
have a loop say
for(...){
for(...){
for(,.....){
//your code
if(yourConditionSatisfied)
return
}
}
}
or you dont want the function to end
do something like this
try{
for(...){
for(...){
for(...){
//your code
if(yourConditionSatisfied)
throw new
yourException();
}
}
}
}catch(YourException e){ //dummy }
//continue your code here
 
T

Tor Iver Wilhelmsen

Shawn said:
Wow. break is similar to GOTO,

GOTO is a *general* purpose jump. Most languages need *special*
purpose jumps - like Java's break, continue, while, if and for. The
difference is that GOTO's jump-anywhere nature makes it hard to
analyse flow compared to the restricted and block-oriented
alternatives.

The C# developers apparently chose to let the programmer shoot
themselves in the foot like that. But only within a method or switch
statement, of course.
 
T

Thomas Hawtin

Mark said:
That said, it can be abused. I've seen code that contained
do {
stuff;
if (!something) break;
more stuff;
} while (false);

As a way to avoid putting "more stuff" into a separate method because there
were a lot of local variables that would have to be passed in and out.

Code like this can often be written more sensibly. But if the programmer
thought it not worth the costs of splitting into a separate method, the
technique doesn't seem completely unreasonable. (For the record I almost
did it once that I remember. I then just rearranged the code into a
clearer form.)

BTW, you don't need the do while false;:

block: {
stuff;
if (!something) {
break block; /***/
}
more stuff;
}

FWIW: I think all break statements should have labels (although that is
unidiomatic) and continue should be outlawed (although I used it in an
interview the other week). Oh, and nested loops tend to be an indicator
that your method is getting a bit long (2D structures excepted).

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top