Loops allow us to repeat sequences of instructions. In a repeat_until -loop a sequence of instructions is repeatedly followed until a specified statement is true.
A repeat_until -loop starts with a repeat instruction and ends with a until instruction. The instructions between repeat and until are followed (in order) until the statement after until is true. If you follow the algorithm and get to the instruction until and the statement after until is false, you jump back to repeat and execute the first instruction after repeat.
Every time we follow the instructions between repeat and _until we call an iteration of the ( repeat_until )-loop. We call the first time we follow these instructions the first iteration and so on.
Our first algorithm with a repeat_until -loop subtracts \(2\) from a given natural number \(n\) until we get number that is less than \(2\text{.}\) When the number is even the output is \(0\) , when the number the output is \(1\text{.}\)
In our next example, given a natural number \(n\) , computes the sum of the natural numbers up to \(n\text{.}\) In the algorithm, \(s\) is the sum so far and \(i\) is incremented in each iteration of the repeat_until loop. The algorithm computes \(1+2+3+\dots+(n-2)+(n-1)+n\text{.}\)
We follow the steps of Algorithm 2.31 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.
In Example 2.33 click your way through the steps of Algorithm 2.31 to see how the values of the variables change with each instruction. When following the loop you have to click on repeat as well as until.
A repeat_until -loop with a statement that is always false in the repeat instruction yields a never ending loop. A sequence of instructions (even if this is only the case fore certain input values) that contains such a loop is not an algorithm.
When the input \(a\) is 0 or a negative integer, this sequence of commands does not end. It never reaches the instruction return\(m\) since \(a\) will never be 0. In this case we do not have a finite sequence of instructions, so it does not match the definition of an algorithm ( Definition 2.1 ).
Until \(i\lt n\) the algorithm adds \(b\) to \(c\) and adds \(1\) to \(n\text{.}\) Thus the number of times \(b\) is added to \(c\) is \(n\text{.}\) As initially \(i\) is set to 0 (see step 2 ) the number of times \(b\) is added to \(0\) is \(n\text{.}\) So the output of the algorithm is \(n \cdot b\text{.}\) Also compare Definition 1.31 where we had defined multiplication as repeated addition.
The following caption is laid out as a circle: Stare blankly at screen, open news site, start reading, get bored, absently check smaller device, stare blankly at screen
Ugh, today’s kids are forgetting the old-fashioned art of absentmindedly reading the same half-page of a book over and over and then letting your attention wander and picking up another book.
Ugh, today’s kids are forgetting the old-fashioned art of absentmindedly reading the same half-page of a book over and over and then letting your attention wander and picking up another book.