Information Technology Topic

Pseudocode: For and While Loops

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. A loop repeats lines of pseudocode: a For loop counts from a start value to an end value in steps of 1, and a While loop repeats while its condition is true, testing it before every pass
  2. A running total needs Store 0 in total before the loop and Store total + value in total inside it; a While loop needs something inside it that can make the condition false, or it never ends
  3. Loops in this syllabus are not nested: each For or While loop stands on its own, closed with End For or End While, and the lines that belong inside are indented

Section 1Why we need loops

Some jobs repeat. A bus conductor adds up four fares. A teacher adds thirty marks. A shop keeps taking numbers until the day is done. Writing the same two lines thirty times would be slow and error-prone, so pseudocode has loops. A loop repeats the lines inside it. One run through those lines is called a pass (or an iteration). This topic covers the two loops the syllabus names: the For loop, which counts, and the While loop, which repeats while a condition is true. Loops use the same keywords as the last topic: Read, Input, Store, Write, Output and Display. The only new words are For, While, End For and End While. As before, there is no nesting: each loop stands on its own and the loops are closed one by one.

Section 2The While loop

A While loop repeats its lines while a condition is true. The condition is tested before every pass, including the first. Store 1 in n While n <= 5 Display n Store n + 1 in n End While n starts at 1. The test n <= 5 is true, so 1 is displayed and n becomes 2. This repeats until n becomes 6, when the test is false and the loop ends. The values displayed are 1, 2, 3, 4, 5. The body ran 5 times, but the test was made 6 times. Because the test comes first, the body may never run. If the condition is false at the very start, the lines inside are skipped completely. Here's the trick: a While loop has four parts. A starting value (Store 1 in n), a test (n <= 5), the work (Display n) and an update that moves towards the end (Store n + 1 in n).

Section 3The While loop must be able to stop

If nothing inside a While loop changes what the condition tests, the condition stays true and the loop never ends. The algorithm then does not terminate. Store 1 in n While n < 5 Display n End While n stays at 1, so n < 5 is always true and 1 is displayed again and again. Adding Store n + 1 in n inside the loop fixes it, and the loop then displays 1, 2, 3, 4. Watch out: also check that the update can reach the end. A loop that adds 2 to n and stops only when n equals 10 will never end if n starts at 1, because n jumps over 10. Using a test such as n < 10 avoids the trap.

Section 4The For loop

A For loop is used when you know how many times to repeat. It counts from a start value to an end value. For count from 1 to 5 Display count End For count is set to 1, the body runs, count goes up by 1 automatically, and so on until count has been 5. The values displayed are 1, 2, 3, 4, 5. You do not write the starting value or the update yourself: the For line does both. The loop variable counts up in steps of 1, and you should not change it inside the loop. The number of passes is end - start + 1. For i from 3 to 7 runs 7 - 3 + 1 = 5 times. If the start is already past the end, as in For count from 5 to 1, the body does not run at all. Easy to mix up: count from 0 to 4 and count from 1 to 5 both run 5 times, but they give different values of the loop variable.

Section 5Running totals with a loop

A loop is perfect for adding up a list of values. Set the total to 0 before the loop, then add each value inside it. Store 0 in total For trip from 1 to 4 Read fare Store total + fare in total End For Display total A conductor in Kingston enters the four fares 120, 150, 120 and 200. total goes 120, then 270, then 390, then 590. After the loop, Display total shows 590. Three things have to be right. The total is set to 0 before the loop; if it is not, the first Store has no total to add to. The adding Store is inside the loop, so it happens each pass. The Display is after End For, so only the final total is shown. Put the Display inside the loop and it shows 120, 270, 390, 590 instead. Here's the trick: ask three questions. What starts before the loop? What happens each pass? What is shown after the loop?

Section 6While loops with a stop value

Sometimes you do not know how many values there will be. A While loop can keep reading until a special stop value, such as -1, is typed. Read mark Store 0 in total Store 0 in count While mark <> -1 Store total + mark in total Store count + 1 in count Read mark End While Display total Display count The first mark is read before the loop, so the test has a value to look at. Inside the loop the mark is added and counted, and the last line of the loop reads the next one. With 60, 75, 90 and -1 the loop runs 3 times. total is 225 and count is 3, so the screen shows 225 and 3. The stop value -1 is tested but never added. If the first mark is -1, the test is false straight away. The loop runs 0 times and total and count both stay 0. An average found by Store total / count in average would then divide by zero, which an algorithm should not do. Easy to mix up: use a For loop when the number of passes is known (4 fares, 30 marks). Use a While loop when the loop ends on a condition (until -1 is typed, or while money is below a target).

Section 7Common loop errors, and tracing

To test a loop, follow it by hand and write down the value of each variable after every pass. A small table with one row per pass works well. The usual faults are: no update inside a While loop, so the loop never ends; the test off by one, so the loop runs one pass too many or too few (< where <= was needed); the total not set to 0 before the loop; and the Display inside the loop when only the final answer was wanted. Here is a While loop that doubles money until it reaches 100: Store 1 in money Store 0 in year While money < 100 Store money * 2 in money Store year + 1 in year End While Display year Display money money goes 1, 2, 4, 8, 16, 32, 64, 128 and year counts 1 to 7. When money is 64 the test 64 < 100 is true, so it doubles to 128 and year becomes 7. Then 128 < 100 is false and the loop ends. The screen shows 7 and 128. The syllabus does not ask for nested loops, so each loop in an answer stands on its own, closed with End For or End While, and everything inside is indented.

Examples

A bus conductor in Kingston records the fares for four passengers using this pseudocode: Store 0 in total For trip from 1 to 4 Read fare Store total + fare in total End For Display total The fares typed in are 120, 150, 120 and 200. (a) Show the value of total after each pass. (b) State the value displayed and how many times the body of the loop runs. (c) The loop line is changed to For trip from 1 to 3. State the value displayed. (d) Explain why Store 0 in total must come before the loop.

(a) Pass 1: fare 120, total 0 + 120 = 120. Pass 2: fare 150, total 270. Pass 3: fare 120, total 390. Pass 4: fare 200, total 590. (b) 590 is displayed, because the Display line is after End For. The body runs 4 times, because 4 - 1 + 1 = 4. (c) Only the first three fares are read, so total is 120 + 150 + 120 = 390 and 390 is displayed. The last fare is never added, so a loop that is one pass short gives a wrong total. (d) The first pass works out total + fare. If total has not been given a value, there is nothing to add the fare to and the algorithm fails. Store 0 in total gives it a starting value, and it must be outside the loop, otherwise it would reset the total to 0 on every pass.

A teacher in Mandeville enters marks until -1 is typed, using this pseudocode: Read mark Store 0 in total Store 0 in count While mark <> -1 Store total + mark in total Store count + 1 in count Read mark End While Display total Display count The values typed are 60, 75, 90 and -1. (a) Trace total, count and mark after each pass of the loop. (b) State the two values displayed. (c) Two lines are added after the loop: Store total / count in average and Display average. State the average. (d) State what happens if the first value typed is -1, and why.

(a) Before the loop: mark 60, total 0, count 0. After pass 1: total 60, count 1, mark 75. After pass 2: total 135, count 2, mark 90. After pass 3: total 225, count 3, mark -1. The test -1 <> -1 is false, so the loop ends. (b) 225 and then 3 are displayed. The stop value -1 was never added. (c) average = 225 / 3 = 75, so 75 is displayed. (d) The test -1 <> -1 is false at the very start, so the loop body runs 0 times. total and count stay at 0, and the lines Display total and Display count show 0 and 0. An average of total / count would then be 0 / 0, which cannot be worked out, so the algorithm would fail at that line.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from