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 →

  • 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)

    prints 10

  • 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)

    prints 8

  • 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)

    prints 3

  • ..< 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)
    }

    prints 1 ⏎ 2 ⏎ 3

  • 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)

    prints cat ⏎ 2

  • \( … ) 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)")

    prints age 9

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 →