Course / Module 3
Module 3 of 7

Repetition and collections

Doing the same work for each item in a collection.

collections loops repetition

Up to now each value has had its own name. But often you have many values of the same kind: every line item on an invoice, every receipt in a folder, every claim in a batch. A collection holds many values under one name.

When you have a collection, you usually want to do the same work to each item in it. That is a loop: For each item in the collection, run these instructions. The program below adds up every amount in a list.

This idea is essential, and we are going to be honest with you about the state of the language here.

The idea in code
example.dvl
# A collection holds many values under one name.
amounts equals list of 120 and 340 and 90

total equals 0
For each amount in amounts:
  total equals total plus amount

Print total
Note: This is real Devlish syntax, but the browser engine shipped on this site is an early subset that does not yet execute lists and loops. Collections are the next major area of the language, so we teach the concept honestly rather than hide it. Run this one in the full native runtime; the browser playground will catch up.

Takeaway

A collection is many values under one name; a loop does the same work to each of them. It is the difference between handling one invoice and handling a thousand.