Breaking out of a 2-level FOR loop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dennis M. Marks

    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! =-----
  • Lasse Reichstein Nielsen

    #2
    Re: Breaking out of a 2-level FOR loop

    "Dennis M. Marks" <denmarks@dcsi. net> writes:
    [color=blue]
    > 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?[/color]

    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;}
    }
    ---
    [color=blue]
    > Most languages have an EXIT statement. It there one in javascript?[/color]

    Yep, break.
    [color=blue]
    > I must break out of 2 FOR loops when I find a match. It would be a
    > waste of time to complete the loops.[/color]

    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 - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Douglas Crockford

      #3
      Re: Breaking out of a 2-level FOR loop

      > I have a 3 level array. First level is a list of trains. Second level[color=blue]
      > 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.[/color]

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

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


      Comment

      • Dr John Stockton

        #4
        Re: Breaking out of a 2-level FOR loop

        JRS: In article <17112003183230 4785%denmarks@d csi.net>, seen in
        news:comp.lang. javascript, Dennis M. Marks <denmarks@dcsi. net> posted at
        Mon, 17 Nov 2003 18:32:30 :-
        [color=blue]
        > I must break out of 2 FOR
        >loops when I find a match.[/color]

        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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

        Comment

        Working...