Break and Continue Statements in Scripts

Break and Continue statements are used within loops (For, Do, While and Repeat-Until) to redirect script execution.  These statements can only be used inside a loop.  Outside of a loop, they have no meaning and will generate an error.

A Break statement can be inserted by selecting “BREAK Statement” from the Script element’s Insert menu (or pressing Ctrl+B when in the Script dialog). A Continue statement can be inserted by selecting “CONTINUE Statement” from the Script element’s Insert menu (or pressing Ctrl+T when in the Script dialog).   There are no inputs for a Break or Continue statement; hence there is no dialog.  When you insert one of these statements, they are simply added to the script.

Break statements break out of the loop.  That is, they redirect execution to the statement following the loop in which they are contained.   Hence, Break statements are usually placed within an if statement inside of a loop to conditionally exit the loop earlier than planned. 

For example, in the script below, when ETime is equal to 0 (i.e., the first time the element is updated), the loop will be broken on the second time through (i.e., when n = 2).  As a result, Result will only be incremented once.

   Note: A Break statement within nested loops breaks the nearest enclosing loop

Continue statements transfer control to the bottom of the enclosing loop.  That is, they do not break out of the loop completely, they simply skip the statements between the Continue statement and the bottom of the loop.   

For example, in the script below, a vector (X) is being defined in a Do loop. In this case, the first 5 items of the vector are not assigned (they are skipped).  Items 6 through 20, however, are assigned.

   Note: A Continue statement within nested loops transfers control to the bottom of the nearest enclosing loop. 

Related Topics…