A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Breaking out of multiple loops

  1. #1
    Member
    Join Date
    Nov 2004
    Posts
    48

    Breaking out of multiple loops

    Imagine you have code in a nested loop. You want to break out of both loops as a result of a condition. Something like this:

    Code:
    for (var j = 0; j < HEIGHT; j++){
    
    for (var k = 0; k < WIDTH; k++) {
    if (matrix[j][k] == 0) {
    break; break; }
    }
    }
    Ugh, that looks horrible, sorry. My point is, the code above won't work, because the actionscript "reader" or "compiler" or whatever isn't going to recognize any code that continues past the first "break;" statement.

    Is there some way of writing, "break every loop"?

  2. #2
    Senior Member the_one_18's Avatar
    Join Date
    Mar 2002
    Posts
    217
    try this
    for (var j = 0; j < HEIGHT; j++){

    for (var k = 0; k < WIDTH; k++) {

    if (matrix[j][k] == 0) {


    }
    break;
    }
    break;
    }
    to breaks inside if will only affect if!
    MeHdI

  3. #3
    Member
    Join Date
    Nov 2004
    Posts
    48
    First, I'm pretty sure that "break;" doesn't break an if-else block of code; it just seemingly ignores all statements after "break;" that come before the "}" signifying the end of the loop body.

    But also, I'm trying to make the two loops break as a result of the condition inside. Meaning, there could be all sorts of statements inside those loops, but I'd like some kind of escapeway if all that code is, after some number of loops, unnecessary.

  4. #4
    Senior Member the_one_18's Avatar
    Join Date
    Mar 2002
    Posts
    217
    then simply write this in if statement
    k=WIDTH;
    j=HEIGHT;
    this will seth both for loop conditions to false and break them
    MeHdI

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center