## SectionA.1An Introduction to Algorithms

Most of the algorithms in this book will contain a combination of three kinds of steps: the assignment step, the conditional step, and the loop.

### SubsectionA.1.1Assignments

In order to assign a value to a variable, we use an assignment step, which takes the form:

\begin{equation*} \textrm{Variable = Expression to be computed} \end{equation*}

The equals sign in most languages is used for assignment but some languages may use variations such as := or a left pointing arrow. Logical equality, which produces a boolean result and would be used in conditional or looping steps, is most commonly expressed with a double-equals, ==.

An example of an assignment is k = n - 1 which tells us to subtract 1 from the value of n and assign that value to variable k. During the execution of an algorithm, a variable may take on only one value at a time. Another example of an assignment is k = k - 1. This is an instruction to subtract one from the value of k and then reassign that value to k.

### SubsectionA.1.2Conditional steps

Frequently there are steps that must be performed in an algorithm if and only if a certain condition is met. The conditional or "if ... then" step is then employed. For example, suppose that in step 2 of an algorithm we want to assure that the values of variables x and y satisfy the condition x <= y. The following step would accomplish this objective.

Steps 2.1 through 2.3 would be bypassed if the condition x > y were false before step 2.

One slight variation is the "if ... then ... else" step, which allows us to prescribe a step to be taken if the condition is false. For example, if you wanted to exercise today, you might look out the window and execute the following algorithm.

### SubsectionA.1.3Loops

The conditional step tells us to do something once if a logical condition is true. A loop tells us to repeat one or more steps, called the body of the loop, while the logical condition is true. Before every execution of the body, the condition is tested. The following flow diagram serves to illustrate the steps in a While loop.

Suppose you wanted to solve the equation $$f(x) = 0\text{.}$$ The following initial assignment and loop could be employed.

Caution: One must always guard against the possibility that the condition of a While loop will never become false. Such "infinite loops" are the bane of beginning programmers. The loop above could very well be such a situation, particularly if the equation has no solution, or if the variable takes on real values

In cases where consecutive integer values are to be assigned to a variable, a different loop construction, a For loop, is often employed. For example, suppose we wanted to assign variable k each of the integer values from m to n and for each of these values perform some undefined steps. We could accomplish this with a While loop:

Alternatively, we can perform these steps with a For loop.

For loops such as this one have the advantage of being shorter than the equivalent While loop. The While loop construction has the advantage of being able to handle more different situations than the For loop.

### ExercisesA.1.4Exercises

#### 1.

What are the inputs and outputs of the algorithms listed in the first sentence of this section?

#### 2.

What is wrong with this algorithm?

The algorithm only works when a > b.

#### 3.

Describe, in words, what the following algorithm does:

#### 4.

Write While loops to replace the For loops in the following partial algorithms:

1. S = 0

2. for k = 1 to 5: S = S + k^2

1. The floor of a number is the greatest integer less than or equal to that number.

1. m = a positive integer greater than 1

2. B = floor(sqrt(m))

3. for i = 2 to B: if i divides evenly into m, jump to step 5

4. print "m is a prime" and exit

5. print "m is composite" and exit

#### 5.

Describe in words what the following algorithm does:

#### 6.

Fix the algorithm in Exercise 2.