469,916 Members | 1,924 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,916 developers. It's quick & easy.

Breaking out of a 2-level FOR loop

I have a 3 level array. First level is a list of trains. Second level
are items about the train. Third level is where there are multiples of
the second level item.

The search will be of myArray[i][2][j]

i is train entry, 2 is the item i'm searching and j are the individial
items.

My search will be FOR (i=0 etc , FOR (j= 0 etc.
The limit will be the number of items in the array level. How do I
break out of the FOR loops when I find a match? Most languages have an
EXIT statement. It there one in javascript? I must break out of 2 FOR
loops when I find a match. It would be a waste of time to complete the
loops.

--
Dennis M. Marks
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 20 '05 #1
3 5888
"Dennis M. Marks" <de******@dcsi.net> writes:
My search will be FOR (i=0 etc , FOR (j= 0 etc.
The limit will be the number of items in the array level. How do I
break out of the FOR loops when I find a match?
You use the "break" statment. It breaks out of the closest enclosing
loop or switch (for, while, do or switch statements).

---
for (...) {
if (whatever) {break;}
}
---
Most languages have an EXIT statement. It there one in javascript?
Yep, break.
I must break out of 2 FOR loops when I find a match. It would be a
waste of time to complete the loops.


For that you need a label on the outer loop, and a label argument
to the break command.

---
OuterLoopLabel:
for(...) {
for(...) {
if (whatever) {break OuterLoopLabel;}
}
}
---

That should be enough escaping for this problem.

If you ever need a quick escape from nested function calls, you can
use exceptions:
---
function recursion(n) {
if (n<10) {recursion(n+1);}
else {throw n;}
}

try {
recursion(0);
} catch (n) {
alert(n);
}
---

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
> I have a 3 level array. First level is a list of trains. Second level
are items about the train. Third level is where there are multiples of
the second level item.

The search will be of myArray[i][2][j]

i is train entry, 2 is the item i'm searching and j are the individial
items.

My search will be FOR (i=0 etc , FOR (j= 0 etc.
The limit will be the number of items in the array level. How do I
break out of the FOR loops when I find a match? Most languages have an
EXIT statement. It there one in javascript? I must break out of 2 FOR
loops when I find a match. It would be a waste of time to complete the
loops.


You could just read the manual, you know.
For statements can have labels.

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

http://www.crockford.com/javascript/survey.html
Jul 20 '05 #3
JRS: In article <17*************************@dcsi.net>, seen in
news:comp.lang.javascript, Dennis M. Marks <de******@dcsi.net> posted at
Mon, 17 Nov 2003 18:32:30 :-
I must break out of 2 FOR
loops when I find a match.


To avoid use of a label, you can put the FOR loops in a function and use
return.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by The Bicycling Guitarist | last post: by
22 posts views Thread by stevenkobes | last post: by
49 posts views Thread by elmar | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.