Skip to content
Exit presentation

Programming Proper I

Once for each thing you have

Three bus stops: Market Gate, Canal Bridge, School Gate. Print each name once.

Loading lab…

Three stops in a list.
Step 1 of 5
  1. Three stops in a list.
  2. Pointer at item 1: prints Market Gate.
  3. Pointer at item 2: prints Canal Bridge.
  4. Pointer at item 3: prints School Gate.
  5. List ends. Loop stops.

A for loop can print each name once for you.

The rule

for (const item of list) runs once per item. The number of turns comes from the list, not the block.

A turn is one time the block runs. The name item is yours to choose. Each turn, it holds the next thing in the list. The loop below uses stop.

A list holds several values in order.

const stops = ["Market Gate", "Canal Bridge", "School Gate"];

`` for (const stop of stops) { print(stop); } ``

The list has 3 names, so the block runs 3 times. Turn by turn, stop holds Market Gate, then Canal Bridge, then School Gate.

Three loops, and where each gets its number of turns
The loopTurns come from
A repeat blockA number written in the block
whileWhenever the condition turns false
for ... ofThe length of the list

Add a fourth stop to the list. The loop then runs 4 times with no other edit. There is no number in the block to edit.

[] is an empty list, a real list with nothing in it. The block runs no times. The program still finishes normally.

Jump to lesson

This track

  1. 0 Start here
  2. 1 The same program, typed instead of dragged
  3. 2 A box you can change, and a box you cannot
  4. 3 Four kinds of value, and why the quotes matter
  5. 4 What the signs do, and the one that does two jobs
  6. 5 A fork in the line
  7. 6 A loop that runs until something stops it
  8. 7 Once for each thing you have
  9. 8 What the message says, and what it does not
  10. 9 The big picture
  11. 10 Track quiz

Other tracks