13.07.2015 Views

2.5.9 Loops and Control Structures - Wolfram Research

2.5.9 Loops and Control Structures - Wolfram Research

2.5.9 Loops and Control Structures - Wolfram Research

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2.5 Evaluation of Expressions 343In Mathematica, bothWhile <strong>and</strong> For always evaluate the loop test before evaluating the body of theloop. As soon as the loop test fails to be True, While <strong>and</strong> For terminate. The body of the loop is thusonly evaluated in situations where the loop test is True.The loop test fails immediately, so thebody of the loop is never evaluated.In[12]:= While[False, Print[x]]In a While or For loop, or in general in any Mathematica procedure, the Mathematica expressions yougive are evaluated in a definite sequence. You can think of this sequence as defining the “flow of control”in the execution of a Mathematica program.In most cases, you should try to keep the flow of control in your Mathematica programs as simple aspossible. The more the flow of control depends for example on specific values generated during theexecution of the program, the more difficult you will typically find it to underst<strong>and</strong> the structure <strong>and</strong>operation of the program.Functional programming constructs typically involve very simple flow of control. While <strong>and</strong> Forloops are always more complicated, since they are set up to make the flow of control depend on thevalues of the expressions given as tests. Nevertheless, even in such loops, the flow of control does notusually depend on the values of expressions given in the body of the loop.In some cases, however, you may need to construct Mathematica programs in which the flow of controlis affected by values generated during the execution of a procedure or of the body of a loop. Mathematicaprovides various functions for modifying the flow of control. The functions work somewhat asthey do in languages such as C.Break[ ]Continue[ ]Return[expr]Goto[name]Throw[expr]exit the nearest enclosing loopgo to the next step in the current loopreturn the value expr, exiting all procedures <strong>and</strong> loops in afunctiongo to the element Label[name] in the current procedurereturn expr as the value of the nearest enclosing Catch(non-local return)<strong>Control</strong> flow functions.The Break[ ]causes the loop toterminate as soon as t exceeds 19.In[13]:= t = 1 Do[t *= k Print[t]12624If[t > 19, Break[]], {k, 10}]Web sample page from The Mathematica Book, Second Edition, by Stephen <strong>Wolfram</strong>, published by Addison-Wesley Publishing Company (hardcoverISBN 0-201-51502-4; softcover ISBN 0-201-51507-5). To order Mathematica or this book contact <strong>Wolfram</strong> <strong>Research</strong>: info@wolfram.com;http://www.wolfram.com/; 1-800-441-6284.© 1991 <strong>Wolfram</strong> <strong>Research</strong>, Inc. Permission is hereby granted for web users to make one paper copy of this page for their personal use. Further reproduction,or any copying of machine-readable files (including this one) to any server computer, is strictly prohibited.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!