We follow the steps of
Algorithm 2.5.3 for the input
\(n=4\text{.}\) For each step of the algorithm, we give the new values of the variables that change values in that step.
1.
let \(i:=0\) : So the variable
\(i\) has the value
\(0\) now.
2.
let \(s:=0\) : So the variable
\(s\) has the value
\(0\) now.
3.
repeat : A
repeat-
until -loop starts here.
3.a.
let \(i:=i+1\) : Because the old value of
\(i\) was zero the new value of
\(i\) is
\(0+1=1\text{.}\)
3.b.
let \(s:=s+i\) : Because the old value of
\(s\) was zero and the value of
\(i\) is
\(1\) the new value of
\(s\) is
\(0+1=1\text{.}\)
4.
until \(i=n\) : Because
\(i=1\) and
\(n=4\) the statement
\(i=n\) is false. We repeat the loop and continue with
step 3.a
3.
repeat : We continue the A
repeat-
until -loop.
3.a.
let \(i:=i+1\) : Because the old value of
\(i\) was
\(1\) the new value of
\(i\) is
\(1+1=2\text{.}\)
3.b.
let \(s:=s+i\) : Because the old value of
\(s\) was
\(1\) and the value of
\(i\) is
\(2\) the new value of
\(s\) is
\(1+2=3\text{.}\)
4.
until \(i=n\) : Because
\(i=2\) and
\(n=4\) the statement
\(i=n\) is false. We repeat the loop and continue with
step 3.a.
3.
repeat : We continue the A
repeat-
until -loop.
3.a.
let \(i:=i+1\) : Because the old value of
\(i\) was
\(2\) the new value of
\(i\) is
\(2+1=3\text{.}\)
3.b.
let \(s:=s+i\) : Because the old value of
\(s\) was
\(3\) and the value of
\(i\) is
\(3\) the new value of
\(s\) is
\(3+3=6\text{.}\)
4.
until \(i=n\) : Because
\(i=3\) and
\(n=4\) the statement
\(i=n\) is false. We repeat the loop and continue with
step 3.a.
3.
repeat : We continue the A
repeat-
until -loop.
3.a.
let \(i:=i+1\) : Because the old value of
\(i\) was
\(3\) the new value of
\(i\) is
\(3+1=4\text{.}\)
3.b.
let \(s:=s+i\) : Because the old value of
\(s\) was
\(6\) and the value of
\(i\) is
\(4\) the new value of
\(s\) is
\(6+4=10\text{.}\)
4.
until \(i=n\) : Because
\(i=4\) and
\(n=4\) the statement
\(i=n\) is true. We continue with
step 5.
5.
return \(s\) : The algorithm returns
\(s=10\text{.}\)