473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 6032
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: MarionEll | last post by:
--------------------------------------------------------- ************* Call for Participation ************** ************ Late Breaking News ************* *********** Extreme...
0
by: MarionEll | last post by:
XML Conference 2003 program now available…register with discounts now! IDEAlliance has just released the full program for XML 2003 scheduled December 7-12 at the Philadelphia Convention Center....
0
by: Amy | last post by:
I am looking for an rss feed that contains really important breaking news like "A large Tsunami just hit Asia" or "Terrorists attack the World Trade Center", but is empty if there really isn't...
0
by: melledge | last post by:
Deadline for XTECH 2005 Late-Breaking News and Vendor Presentations - 28 March The deadline for submission of Late-Breaking News and Vendor Presentations for IDEAlliance's XTCH 2005...
27
by: The Bicycling Guitarist | last post by:
Hi. I found the following when trying to learn if there is such a thing as a non-breaking hyphen. Apparently Unicode has a ‑ but that is not well-supported, especially in older browsers. Somebody...
22
by: stevenkobes | last post by:
If a word has a hyphen in it, IE will permit a line break at the hyphen, but Firefox/Mozilla won't. Apparently the Firefox behavior is standards-compliant, but it is not what I want. Is there a...
4
by: John Wood | last post by:
I saw that Microsoft have released a list of breaking changes in .Net here: http://msdn.microsoft.com/netframework/programming/breakingchanges/runtime/default.aspx While this is useful, it seems...
150
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and...
49
by: elmar | last post by:
Hi Clers, If I look at my ~200000 lines of C code programmed over the past 15 years, there is one annoying thing in this smart language, which somehow reduces the 'beauty' of the source code...
4
by: Rubin | last post by:
1) I want to show a breaking hyphen in Mozilla 1.5.0.4 How do I do that? "Unicode standard annex #14", <http://www.unicode.org/reports/tr14/>, defines 4 breaking hyphens. <quote> Breaking...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...

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.