22 Chapter00.
nb
All programming languages provide a few basic means for the construction of algorithms. On the most
basic level, a computer program is a sequence of instructions that the computer executes one after the
other. Programs become more sophisticated when you start changing the flow of execution. Mathemat-
ica provides the same sort of mechanisms for flow control as is found in traditional programming
languages such as C. While the syntax varies from one computer language to the next, there are two
primary kinds of control structures used: branching and iteration.
Mathematica, from a programming perspective, adopts a functional programming paradigm, which
gives it a substantially different flavor from an imperative, or procedural, paradigm. Those readers with
experience with imperative languages, and object-oriented languages particularly, will find that a
functional approach requires a slight shift in the way you think about programs.
Branching
We will first discuss the concept of branching and its implementation in Mathematica. Branching is a
mechanism that allows you to choose between expressions based on conditions that can only be deter-
mined during a program's execution or evaluation. This is also called a selection or conditional
statement.
If
As an example, suppose that you want to display a message based on whether a particular value is
positive. First we'll assign a value to the symbol z.
In[93]:= z=5
Out[93]= 5
The following will output the string “That's positive” if the value stored in z is greater than zero.
In[94]:= If@z > 0,
"That's positive"
D
Out[94]= That's positive
First note that, in order to begin a new line within a single input cell, you simply press the return or
enter key on the alpha-numeric keyboard. The line breaks and extra spaces are not required, however,
and in fact Mathematica ignores them entirely. But they often make functions easier to read and
understand.
Typically, the condition in an If depends on a value input to a function, some intermediary calcula-
tion, or a value that changes during execution. In these examples, think about the symbol z as storing
some value that varies based on some other computations. For instance, z could be the value of some
function at a particular point. Then the value of z would depend on which point was chosen.
Let us now dissect the expression above. You can think of the expression as an application of a func-
tion If to two arguments. This perhaps seems odd to think of If as a function, especially if you are
familiar with imperative languages, and it may be more comfortable to think of the input above merely
as an expression with head If. However, If is a perfectly valid function, in that it accepts arguments
and returns output.
The first argument to If is a conditional expression, that is, an expression that Mathematica can evalu-
ate to True or False. Conditional expressions may include expressions that include relational opera-
tors (e.g., <, <=, ==, >, >=, !=), logical operators (&&, ||, !), or functions that return logical values
(True or False). We will see many examples of conditional expressions in Chapter 1.