actionscript-3for-loopbreakfor-else

Actionscript 3: Else clause on loop?


From Python I've been used to that you can use an else-clause that is triggered if a loop has not been terminated by a break. Apparently this function isn't in ActionScript 3, but is there som kind of workaround?

Thank you!


Solution

  • You have to use a boolean variable to keep track of the loop state and then you can check the value of that variable after the loop. For example:

    // If the loop executes all iterations, this variable will stay false
    var bLoopBreak:Boolean = false;
    
    for ( ... )
    {
        ...
    
        if ( some_condition )
        {
            // Break out of loop and set variable
            bLoopBreak = true;
            break;
        }
    
        ...
    }
    
    if ( bLoopBreak )
    {
        // for loop has been terminated through a break
    }