EDITING
CORRECTING
LOOPS
FLOWCHART LOOPS
100

What must you do before changing an existing algorithm?

Read and trace it until you understand what it currently does

100

Define a logic error

An error where the program runs without crashing but gives the wrong result

100

How many times does FOR i ← 0 TO 9 run?

10

100

Flowcharts have no loop symbol. What do you draw instead?

A decision diamond with an arrow going back

200

What is the most ELSE parts one IF statement can have?

One

200

Name the two ways to find an error in an algorithm

A dry run with a trace table, and testing with test data

200

Rewrite END FOR in our pseudocode style

NEXT i

200

Which process box does a FOR loop hide?

The counter increment, e.g. c ← c + 1

300

Task 1.1:

INPUT n

IF n > 10 THEN OUTPUT n * 2

ELSE OUTPUT n + 5

What is the output for input 10?

15 (10 is not greater than 10, so the ELSE part runs: 10 + 5)

300

Fix this line from a "find the smallest" algorithm: smallest ← -99999999

smallest ← 99999999 (start very high so the first value replaces it)

300

total ← 0, then FOR i ← 1 TO 4: total ← total + i. Trace total.

1, 3, 6, 10. The final total is 10

300

Write the decision box for FOR c ← 0 TO 2

Is c ≤ 2? Yes → run the loop body, No → exit the loop

400

Edit total ← a + b so the algorithm also adds a third number

Add INPUT c, then change the line to total ← a + b + c

400

This line should square n: square ← n × 1. Testing with n = 1 gives the right answer. Give a test value that exposes the bug.

Any value except 0 or 1, e.g. n = 5: expected 25, but the program outputs 5 (5 ← 5 × 1)

400

What are the first and last values of RANGE(1, 11)?

1 and 10 (the end value 11 is not included)

400

What happens if the loop's arrow goes back to counter ← 0?

An infinite loop: the counter resets every time and never reaches the end