Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to jump out of several nested control structures?

Reply
Thread Tools

How to jump out of several nested control structures?

 
 
Shawn
Guest
Posts: n/a
 
      10-20-2006
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?

}
}

}
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      10-20-2006
Shawn wrote:
> Hi,
>
> I am wondering how can I jump out of several layers of loops. break can
> only jump out of the inner most loop.


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

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

for the syntax and an example.

Patricia
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      10-20-2006
On Fri, 20 Oct 2006 13:44:00 -0400, Shawn wrote:
> 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

--
[ don't email me support questions or followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
 
Reply With Quote
 
Shawn
Guest
Posts: n/a
 
      10-20-2006
Patricia Shanahan wrote:

>
> No, break can jump out of any labeled statement enclosing it.
>
> See
> http://java.sun.com/docs/books/jls/s....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.
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      10-20-2006
Shawn wrote:
> Patricia Shanahan wrote:
>
>>
>> No, break can jump out of any labeled statement enclosing it.
>>
>> See
>> http://java.sun.com/docs/books/jls/s....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.


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
 
Reply With Quote
 
Shawn
Guest
Posts: n/a
 
      10-20-2006
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?
 
Reply With Quote
 
Shawn
Guest
Posts: n/a
 
      10-20-2006
Patricia Shanahan wrote:

>
> 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
 
Reply With Quote
 
Simon Brooke
Guest
Posts: n/a
 
      10-20-2006
in message <ehb1t0$8mi$(E-Mail Removed)>, Shawn ('(E-Mail Removed)')
wrote:

> 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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/

;; Conservatives are not necessarily stupid,
;; but most stupid people are conservatives -- J S Mill
 
Reply With Quote
 
Simon Brooke
Guest
Posts: n/a
 
      10-20-2006
in message <ehb6sj$a3j$(E-Mail Removed)>, Shawn ('(E-Mail Removed)')
wrote:

> Patricia Shanahan wrote:
>
>> No, break can jump out of any labeled statement enclosing it.
>>
>> See
>>

http://java.sun.com/docs/books/jls/s....doc.html#6842
>>
>>
>> for the syntax and an example.

>
> 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.

--
(E-Mail Removed) (Simon Brooke) http://www.jasmine.org.uk/~simon/

my other car is #<Subr-Car: #5d480>
;; This joke is not funny in emacs.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-20-2006
Simon Brooke wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to jump out of nested 'for'-loops? Peng Yu Python 3 09-29-2009 04:09 AM
RDOC: several related modules in several C files Victor \Zverok\ Shepelev Ruby 3 03-16-2007 04:15 PM
Double left click jump to top, same with ctrl or shift to jump to bottom? Eadwine Rose Javascript 2 10-15-2006 08:38 PM
Jump Menu (DropDown Menu) in ASP.net. Can someone help me out? Miguel Dias Moura ASP .Net 3 04-03-2004 08:17 AM



Advertisments