University of Melbourne · FACULTY OF COMPUTER SCIENCE

COMP90059 Chap.6 Iteration and List Processing

- one subject, every graph, every model, every mark
12 Chapters4-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 6 of 9 · COMP90059

Iteration and List Processing

Iteration becomes predictable when four obligations are explicit: initialise state, decide whether another pass is needed, process the current item and update state toward termination. A for loop is natural when items come from a known iterable; a while loop suits repetition controlled by a continuing condition.

Accumulators condense a sequence into totals, counts, products or collected results, and their initial values must match the update operation. A loop invariant states what the accumulator means after every completed pass. Break, continue and loop else represent different control outcomes, while nested loops explore combinations by restarting the inner loop for each outer value.

Trace tables expose off-by-one errors and missing updates.

In this chapter

What this chapter covers

  • 01

    For loops visit items supplied by an iterable

  • 02

    While loops repeat while a condition remains true

  • 03

    Range follows a half-open start and stop convention

  • 04

    Accumulators begin with the identity for their update

  • 05

    Loop invariants describe state after each completed pass

  • 06

    Continue skips the remainder of one iteration

  • 07

    Break exits the nearest loop immediately

  • 08

    Nested loops generate ordered combinations of outer and inner values

  • 09

    Enumerate supplies positions and items without a manual counter

  • 10

    Zip coordinates parallel traversal until the shortest iterable ends

  • 11

    Search loops distinguish finding a match from exhausting all candidates

  • 12

    Mutation during traversal can change which list elements are visited

Worked example · free

Compute a filtered mean with two accumulators

Q [5 marks]. Ignore negative values in [2.5, -1.0, 3.5, 4.0, -2.0] and calculate the mean of accepted readings. Marks here support an authored practice breakdown, not an official university allocation.
A while loop exposes four obligations: initialise, test, process, update.A while loop exposes four obligations: initialise, test, process, update. A for loop packages the next-item update for a sequence.initialise statecontinue?process + updatefinishyesno
A while loop exposes four obligations: initialise, test, process, update. A for loop packages the next-item update for a sequence.
  • 1Initialise total to 0.0 and count to 0.
  • 1Accept 2.5, 3.5 and 4.0 while skipping both negative values.
  • 1The accepted total is 10.0 and the accepted count is 3.
  • 1Guard count before division because every value could have been rejected.
  • 1Compute 10.0 divided by 3, approximately 3.333.
The filtered mean is 10.0 divided by 3. Both total and count must update for every accepted item so the invariant remains true.
Sia tip — In a trace table, place the condition result before the accumulator updates so skipped items are unmistakable.
Glossary

Key terms

For loop
A loop that receives successive items from an iterable until the iterable is exhausted.
While loop
A loop that repeats while its Boolean continuation condition remains true.
Accumulator
A state variable updated across iterations to build a total, count, product or collection.
Loop invariant
A claim about loop state that is true after each completed iteration and explains the final result.
Break statement
A control statement that immediately exits the nearest enclosing loop.
Continue statement
A control statement that skips the remaining body of the current iteration and advances to the next.
Progress measure
A quantity that changes on every while-loop path and demonstrates that the continuation condition will eventually become false.
Loop exhaustion
Normal completion after an iterable has no more items or a while condition becomes false, without leaving through break or an exception.
Parallel traversal
Visiting corresponding items from multiple iterables together, usually with zip when stopping at the shortest input is the intended rule.
FAQ

Iteration and List Processing FAQ

When should I use a for loop?

Use a for loop when the program should visit items from a known iterable such as a list, string or range and does not need manual control of the iteration state.

When is a while loop clearer?

Use it when continuation depends on a changing condition and the number of iterations is not naturally fixed in advance, provided every path makes progress toward termination.

Why do filtered means need two accumulators?

The total records accepted values, while the count records how many contributed. Dividing by the original list length would include rejected items incorrectly and can conceal the all-rejected boundary case.

What does loop else mean?

The else suite runs when the loop finishes normally without break, which makes it useful for the no-match outcome of a search without maintaining a separate flag variable.

Why is changing a list during iteration risky?

Removing or inserting elements shifts later positions while the loop is still advancing, so items can be skipped or visited unexpectedly. Build a new result, iterate over a copy or use an index-controlled design whose update rule is proved explicitly.

How can I verify an accumulator loop?

State what the accumulator means after processing any prefix, choose an initial value that makes the claim true for the empty prefix and show that one body execution preserves it. After the loop, combine the invariant with the termination condition. Test zero iterations and one iteration before using a larger example.

Study strategy

Exam move

Trace one for loop, one while loop, one filtered accumulator and one nested loop in tables. Label the state before the first pass, the condition or next item, each update and the final state. State an invariant for the accumulator and verify it after the first two iterations. For while loops, prove that every path updates something that can make the condition false.

Add break and continue separately, then predict which statements they skip. For a nested loop, list the ordered pairs before writing output. Test zero iterations, one iteration, an all-rejected list and a match on the final item. These cases reveal initialisation and boundary mistakes quickly.

Working through Iteration and List Processing in COMP90059? Sia is AskSia’s AI Computer Science tutor — ask any COMP90059 Iteration and List Processing question and get a clear, step-by-step explanation grounded in how COMP90059 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 177 of your University of Melbourne subjects - and 1,000+ Bibles across every Australian university.
Sia - your COMP90059 tutor, unlimited, worked the way the exam marks it
The full 4-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works