← Starting Henceforth

Episode 7

Decisions

Give forth a yes-or-no and it picks a path — if, else, then, and decisions inside decisions.

1:37 · March (Nutcracker, Op. 71)

Watch here · code in the app

Henceforth is the real terminal — one purchase ($9.99) for iPhone, iPad and Mac. Type the commands below into the app as you watch.

Code along

Tap to copy, then type it into Henceforth.

What you’ll learn

  • if … else … then picks a path from a yes-or-no on the stack: : positive? dup 0 > if ." positive!" else ." not positive." then drop ;
  • Any non-zero is true; zero is the only no. The condition is just a number — even? tests dup 2 mod 0 = and decides from it.
  • Decisions nest inside decisions — sign-of asks "over zero?", then in the else asks again. Bitcoin Script branches the same way: if a signature checks, spend; else, don't.
Transcript
  • every word so far just runs. can one... choose?
  • give forth a yes-or-no, and it picks a path.
  • if the number is over zero, say so. otherwise, the other branch.
  • five is positive — the if branch ran.
  • minus three took the else. same word, two paths.
  • and what counts as yes?
  • any non-zero is true. zero is the only no.
  • the test can be computed — remainder by two, is it zero?
  • six yes, seven no. the condition is just a number on the stack.
  • can a decision hold another?
  • over zero? positive. else, ask again — under zero, or nothing at all.
  • zero, positive, negative — decisions inside decisions.
  • why teach the dog to choose?
  • bitcoin script branches the same way — if a signature checks, spend; else, don't.
  • every spending rule is a decision.
  • more in episode ten.
  • henceforth.