Skip to main content
Logo image

Section 2.4 The 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{.}\) Suppose that the variable \(a\) has the value \(5\text{.}\) Then, after let \(a:=a+1\text{,}\) the variable \(a\) has the value \(6\text{.}\) First, we evaluate \(a +1\) as \(6\) because \(a = 5\text{.}\) Then we assign \(6\) to the variable \(a\text{.}\)
In the video in Figure 2.4.1 we introduce the assignment instruction let and give the first examples.
Figure 2.4.1. Algorithms -- The Assignment let by Matt Farmer and Stephen Steward
The assignment instruction let can be used in two ways. We can give a value to a variable that does not yet have a value, or we can give a new value to a variable that already has a value.

Example 2.4.2. Uses of let _ \(:=\).

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\) :
    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{.}\)
Now we show how Algorithm 2.4.3 works with an example.

Example 2.4.4. Algorithm 2.4.3 with input \(c=-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)= 9\) and assign this value to the variable \(d\text{.}\) Now \(d=9\text{.}\)
  2. let \(e:=c\cdot d\text{:}\) 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\text{:}\) 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\text{,}\) \(-27\text{,}\) and \(81\)
Output \(-3\text{,}\) \(9\text{,}\) \(-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{.}\)
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 \(d=c\cdot c\text{,}\) \(e=c\cdot d\text{,}\) and \(f=c^2\cdot c^2\text{.}\) It only needs 3 multiplications to compute the four values \(d=c^2\text{,}\) \(e=c^3\text{,}\) and \(f=c^4\text{.}\)
Algorithm 2.4.3 uses half as many multiplications as Algorithm 2.2.3 to compute the same values, and it uses no more operations. Therefore, Algorithm 2.4.3 is more efficient than Algorithm 2.2.3.
Using the same strategy as in Algorithm 2.4.3, the algorithm in Example 2.4.5 computes the eighth power of its input. This is the first in a series of interactive algorithms. Click the instructions to see how they change the variable values.

Example 2.4.5. Eighth power algorithm interactive.