"logo.gif"  
Main
Read Online
Download PDF
Additional resources
Revision history
Known typos/bugs
Report a bug
License terms
About the author
"node120_1.gif" "node120_2.gif" "node120_3.gif"

2.8.6      Local goto statements: Break , Continue, Return

There are statements to realize local Goto within the loop - Break[] and Continue[]. They work in the same way as they work in C. For example, here we will break from the Do loop after 4 iterations :

"node120_4.gif"

"node120_5.gif"

"node120_6.gif"

"node120_7.gif"

"node120_8.gif"

The Return[] statement can also be used to break from the loops, but it acts differently. We did not cover it yet, but there are three scoping constructs used in Mathematica  to localize variables - Module, Block and With. If we have a For or While loop inside one of these constructs, then the Break[] statement will break from the loop only, and the code right after the loop (but inside the scoping construct) will start to execute. If we use Return[] however, we will also break from the entire localizing construct which encloses the loop (if there are nested localizing constructs, we break from the innermost one only). Not so for < Do > loop though : it is a localizing construct by itself, so using Return[] we will break out of Do but will remain in whatever localizing construct encloses Do. These comments will become more clear once you get familiar with Module, Block and With (end of chapter IV).

"node120_9.gif" "node120_10.gif" "node120_11.gif"

Created by Wolfram Mathematica 6.0  (05 February 2009) Valid XHTML 1.1!