## Section2.4The Assignment let_:=

The instruction let $$a:=b$$ assigns the value of $$b$$ to $$a\text{.}$$ For example after the instruction let $$a:=5\text{,}$$ the variable $$a$$ has the value $$5\text{.}$$ Assume that the variable $$a$$ has the value $$5\text{;}$$ then after let $$a:=a+1\text{,}$$ the variable $$a$$ has the value $$6\text{.}$$ This occurs because we first evaluate that $$a +1$$ is $$6$$ (since $$a = 5$$) and then assign $$6$$ to the variable $$a\text{.}$$

In the video in Figure 2.4.1 we introduce the assignment instruction let_:= and give first examples.

The assignment instruction let_:= can be used in two ways. We can assign a value to a variable that did not have a value so far or we can assign new value to an existing variable, that we change the value of an existing variable.

We give examples for the use of the instruction let_$$:=\text{.}$$

1. The easiest use of let is to assign a value to a variable, for example we can use it to assign the value $$0$$ to $$i\text{:}$$

let $$i:=0$$

After this instruction the value of the variable $$i$$ is $$0\text{.}$$

2. We can also use let to assign the value of an expression to a variable. Assume that the value of the variable $$c$$ is $$10\text{.}$$ Consider the instruction:

let $$d:=c+35$$

First the expression on the right is evaluated. As $$c=10$$ we get that $$c+35=10+35=45\text{.}$$ Then the computed value 45 is assigned to $$d\text{,}$$ so that the value of $$d$$ after this instruction is $$45\text{.}$$

3. It is more confusing when the same variable shows up on both sides of the $$:=$$ of a let instruction. Assume that the value of i is 5 and consider the instruction:

let $$i:=i+1$$

First the expression on the right is evaluated, we obtain $$i+1=5+1=6\text{.}$$ Then the result of this computation, namely 6, is assigned to $$i\text{,}$$ so that the new value of $$i$$ is 6.

The next algorithm computes the same values as Algorithm 2.2.3, namely $$c\text{,}$$ $$c^2\text{,}$$ $$c^3\text{,}$$ and $$c^4\text{.}$$

The Input and Output of Algorithm 2.2.3 and Algorithm 2.4.3 are the same, but the results are obtained in different ways. In particular the computation effort differs.

• Algorithm 2.2.3 needs one multiplication to compute $$c^2= c\cdot c\text{,}$$ two multiplications to compute $$c^3= c\cdot c\cdot c\text{,}$$ and three multiplications to compute $$c^4= c\cdot c\cdot c\cdot c$$ (count the multiplication symbols ‘$$\cdot$$’). This is a total of 6 multiplications.

• Algorithm 2.4.3 computes the same values as $$c^2=d=c\cdot c\text{,}$$ $$c^3=e=c\cdot d\text{,}$$ and $$c^4=f=c^2\cdot c^2\text{.}$$ It only needs 3 multiplications to compute all four values.

So we can consider Algorithm 2.4.3 to be about twice as fast as Algorithm 2.2.3.

We follow the steps of the Algorithm 2.4.3 for the input $$c=-3$$

Input $$c=-3$$

1. let $$d:=c\cdot c\text{:}$$ We have $$c=-3\text{.}$$ We compute $$c\cdot c=(-3)\cdot (-3)3= 9$$ and assign this value to the variable $$d\text{.}$$ Now $$d=9\text{.}$$

2. let $$e:=c\cdot d$$ : We have $$c=-3$$ and $$d=9\text{.}$$ We compute $$c\cdot d=(-3)\cdot 9=-27$$ and assign this value to the variable $$e\text{.}$$ Now $$e=-27\text{.}$$

3. let $$f:=d\cdot d$$ : We have $$d=9\text{.}$$ We compute $$d\cdot d=9\cdot 9=81$$ and assign this value to the variable $$f\text{.}$$

4. return $$c\text{,}$$ $$d\text{,}$$ $$e\text{,}$$ $$f$$ : We return the values of $$c\text{,}$$ $$d\text{,}$$ $$e\text{,}$$ and $$f\text{,}$$ namely $$-3\text{,}$$ 9, $$-27\text{,}$$ and 81

Output $$-3\text{,}$$ 9, $$-27\text{,}$$ 81

So when the input to Algorithm 2.4.3 is $$c=-3$$ the output is $$-3\text{,}$$ $$9\text{,}$$ $$-27\text{,}$$ $$81\text{.}$$

Using the same strategy as in Algorithm 2.4.3 the algorithm in Example 2.4.5 computes the eighth power of the input. This is the first in a series of interactive algorithms in which you can observe how the instructions change the values of variables by clicking on the instructions.

In Checkpoint 2.4.6 determine the values returned for by the algorithm for the given input values.

Consider the algorithm:

Algorithm

Input: Two integers $$a$$ and $$b\text{.}$$

(1) let $$c:=a+b$$

(2) let $$d:=a\cdot b$$

(3) let $$e:= d-c$$

(4) return $$d$$

What does the algorithm return when the input is $$a=-8$$ and $$b=-6$$ ?

What does the algorithm return when the input is $$a=-8$$ and $$b=10$$ ?

What does the algorithm return when the input is $$a=2$$ and $$b=-7$$ ?

What does the algorithm return when the input is $$a=4$$ and $$b=5$$ ?

$$48$$

$$-80$$
$$-14$$
$$20$$