Notes: Differences of Break And Return

Just a little note to myself (after wasting an hour). When running a loop inside a function, using break merely terminates the loop and not the function itself. To terminate the function, use return instead.

The code below, when the if statement is true will terminate the function. If the return is replace with a break, only the for loop terminates, and updateCurrentThumb( 0 ) is still called/performed.


private function findPosition( clip:MovieClip){
for( var i:Number = 0; i < thumbs.length; i++ ){
if( clip.getModel() == thumbs[i].getModel() ){
updateCurrentThumb( i );
return;
}
}
updateCurrentThumb( 0 );
}


About this entry