Reference

How to read code

Reading code means running it in your head before the computer does. The one rule: predict first, then reveal — you cannot run code you have not predicted. Here are the six reading moves the lab drills. Ready to try one? Open the trace lab →

Before this: SyntaxForge (build it from tiles). After this: KindForge (read what the compiler says). The twelve Syntax Bridge programs are the same in all three: built in SyntaxForge, read here, and handed to the compiler in KindForge.

  • Read top to bottom

    Run each line in order, one at a time — the same way the computer does. Never jump ahead.

    let a = 6
    let b = 4
    print(a + b)

    Say what it prints, then check.

  • A var is a box you can refill

    A var can be given a new value. Always keep the NEWEST value, not the first one.

    var n = 3
    n = n + 5
    print(n)

    Say what it prints, then check.

  • Whole-number division drops the rest

    Dividing two whole numbers throws away the remainder toward zero — 7 / 2 is 3, not 3.5.

    print(7 / 2)

    Say what it prints, then check.

  • ..< stops before · ... includes

    A range with ..< never reaches the last number; ... does include it. This is the #1 off-by-one to check.

    for i in 1..<4 {
      print(i)
    }

    Say what it prints, then check.

  • Arrays count from 0

    The first item is at index [0], the second at [1]. And .count is how many there are.

    let p = ["cat","dog"]
    print(p[0])
    print(p.count)

    Say what it prints, then check.

  • \( … ) fills in a value

    Inside a string, \( name ) is replaced by that value. The name becomes what it holds.

    let x = 9
    print("age \(x)")

    Say what it prints, then check.

19 ways to misread a program

Most wrong answers come from a small number of misreadings, and every good reader has made them. When your answer comes from one of these, the lab tells you which. PairForge and the Syntax Bridge use the same names. KindForge and LibraryForge (ages 15–18) use the older wording printed under a name.

  1. Reading the name, not what is in the box A name is a label on a box. The computer uses what is inside the box, not the name written on it. Ages 15–18: Reading the identifier instead of its value. An identifier is a label: the program uses the value bound to it. In a string literal only \( ) interpolates; bare text stays text. See it in a program →
  2. Gluing numbers together like letters With two numbers, + adds them. Only text joins end to end. See it in a program →
  3. Doing the lines out of order The computer does one line at a time, from the top, each one finished before the next. See it in a program →
  4. Stopping before the last change Keep going until the line that prints. Every line above it that changes the box counts. See it in a program →
  5. Mixing up whole numbers and decimals Two whole numbers divide to a whole number: the rest is thrown away, not rounded. Numbers with a point keep the fraction. Ages 15–18: Confusing integer and floating-point division. Int / Int truncates: the remainder is discarded, not rounded. A fractional result needs both operands to be Double. See it in a program →
  6. One too many, or one too few Check both ends. ..< stops before the last number, ... includes it, and a count is one more than the last position. Ages 15–18: An off-by-one at a range boundary. ..< excludes the upper bound and ... includes it; a count is one more than the last index. See it in a program →
  7. Counting from 1 instead of 0 The first item in a list is at [0], so [1] is the second one. Ages 15–18: Indexing from 1 instead of 0. Arrays are zero-indexed: [0] is the first element and [1] the second. See it in a program →
  8. Running the loop only once The body of a loop runs again for every value, not just the first. See it in a program →
  9. Running both paths An if, an else if or a switch runs at most one path: the first one that matches. The others are skipped. Ages 15–18: Executing both branches. if / else if / else and switch run at most one branch: the first that matches. The rest are skipped. See it in a program →
  10. Thinking the loop checks its test all the time A while loop checks its test only at the top of each pass. A pass that starts always finishes, even if it goes past the line. See it in a program →
  11. Thinking two lines happen at the same time The first line runs and changes its box. The next line then uses that new value. See it in a program →
  12. Mixing up which box is which Each name is its own box. Making a new box from x leaves x alone, and a function’s own x is a different box from the one outside. See it in a program →
  13. Forgetting the function hands its answer back When a function finishes, the line that called it carries on, with the answer in its place. See it in a program →
  14. Doing a different sum from the one written Do exactly the sum on the line: % is what is left over, * multiplies, and a + inside a loop adds once per pass. See it in a program →
  15. Mixing up && and || && needs both sides to be true. || needs just one of them. See it in a program →
  16. Thinking two loops move together The inner loop runs all the way through for each single pass of the outer loop. See it in a program →
  17. Adding text like numbers Quotes make text. With two texts, + joins them end to end. It only adds when both sides are numbers. See it in PairForge →
  18. Letting the edge number in > and < are strict: 5 > 5 and 5 < 5 are both false. Only >= and <= let the edge number in. See it in PairForge →
  19. Thinking the loops take turns A loop inside a loop is not one after the other: the inner one runs all the way through on every outer pass, so the counts multiply. See it in PairForge →

The table is older than the computer

The trace table you fill in is not new. People were keeping the same kind of table long before there was a computer to run.

  1. A printed table from 1843: numbered operations down the side and a column for each of the engine’s variables, filled in with what each holds after the step, mostly written as small formulas like 2n − 1.
    Note G, 1843 ✓ Seen · a record of the real thing Ada Lovelace wrote this table for an engine that was never finished. Each row is one step, each column is a box, and each entry shows what is in that box after the step. Ada Lovelace, Note G, in her translation of L. F. Menabrea, “Sketch of the Analytical Engine” (1843). Public domain, via Wikimedia Commons. Source
  2. A handwritten page from an 1899 store ledger: rows of goods such as shoes, spools and lamps, each with a price and an amount, under a total carried over at the top.
    A store’s book, 1899 ✓ Seen · a record of the real thing A shopkeeper worked out one amount on each row (8 pairs of shoes at $1.10 make $8.80) and carried the total from the page before. Millinery Book Ledger, W. S. Clark store, Tarboro, North Carolina (1899), page 92 (top half). Edgecombe Community College via the Digital Public Library of America. Public domain, via Wikimedia Commons. Source
  3. var total = 0
    let shoes = 8 * 110
    total = total + shoes
    let spools = 100 * 18
    total = total + spools
    print(total)
    StepLinetotalshoesspoolsPrints
    110
    22880
    33880
    441800
    552680
    662680
    Your trace Two of the store’s rows (the shoes and the spools) as a program, in cents. The Jotter’s table is the same object: a row for each step, a column for each box, and only what changes is written in. Made by TraceForge’s machine from the program above. Print a table to fill in yourself

Every program in TraceForge is written in a teaching subset of the Swift language — the same rules real programmers use, kept small so you can trace it by hand. Try a puzzle →