Reference

How syntax works

Syntax is the small stuff that holds a program together — brackets, keywords, one symbol versus another. Most first mistakes are syntax, not ideas. The lab lets you take the training wheels off one at a time: tap tiles into the blanks, then type with a palette, then write it all yourself. Here are the syntax rules it drills. Open the syntax lab →

  • print( … ) uses round brackets

    A function call puts its value inside round ( ) brackets — never square [ ] or curly { }.

    print(6 + 4)

    prints 10

  • One = stores · two == asks

    A single = puts a new value into a box. Two == only ASKS "are these equal?" and stores nothing.

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

    prints 8

  • if uses curly braces { }

    The body that runs "if it is true" goes inside curly braces. Swift has no "then" and no "end".

    if 7 > 5 {
      print("big")
    }

    prints big

  • ..< stops before · ... includes

    0 ..< 3 is 0, 1, 2 (not 3). 0 ... 3 would add the 3. Pick the one that gives the count you want.

    for i in 0..<3 {
      print(i)
    }

    prints 0 ⏎ 1 ⏎ 2

  • return hands a value back

    Inside a function, return sends the answer back to whoever called it. print would only show it.

    func double(n: Int) -> Int {
      return n * 2
    }
    print(double(5))

    prints 10

  • .append and .count are the list words

    A Swift list grows with .append(x) and is measured with .count — not add / push / length / size.

    var xs = [1]
    xs.append(2)
    print(xs.count)

    prints 2

  • && needs both · || needs one

    && (AND) is true only when BOTH sides are true; || (OR) is true when either is. Swift uses the symbols.

    if true && false {
      print("both")
    } else {
      print("not both")
    }

    prints not both

  • Whole numbers divide with /, but // is a comment

    17 / 5 is 3 (the remainder is dropped). Watch out: // does not divide — it starts a comment!

    print(17 / 5)

    prints 3

Every program in SyntaxForge is written in a teaching subset of the Swift language — the same rules real programmers use, kept small so you can build it by hand. Build one →