Lesson 2 — Deciding and repeating
Step 1: asking a question
A parrot repeats; a program decides. A comparison asks a question and answers true or false: == (is it the same), != (is it different), <, <=, >, >=:
The book says it answers:
true false true true
One warning to file away: values of different kinds are never equal — the number 3 is not the text "3" — so a question always compares like with like.
Your turn. Ask two questions: is 10 greater than 7, and is "bread" the same as "bread"? Say both answers.
The worked answer — try yours first, then peek
The book says it answers:
true true
Step 2: walking the road the answer opens
if runs a stretch of code when its question answers true, and else — when there is one — runs the other stretch when it answers false. The stretch lives between braces. Here is a door-keeper:
The book says it answers:
Come in - you are grown.
Change the 29 to 12 in your own copy and run it again: the other road is taken. The program did not get smarter; it asked, and walked the road the answer opened.
Your turn. Keep a number of coins on a shelf. If there are at least 100, say "A full purse." Otherwise say "Still gathering." Try it with 250.
The worked answer — try yours first, then peek
The book says it answers:
A full purse.
Step 3: again, while a question holds
Deciding once is half the craft. The other half is repeating. while repeats its stretch as long as its question keeps answering true:
The book says it answers:
3 2 1 Go
Read it as a story: n starts at 3; the question "is n still at least 1?" is asked before every round; inside, the program says n and then makes n smaller — and that making-smaller is what lets the loop ever end. A loop whose question never turns false would run forever, so the language stands guard: a program whose loops pass a million times without finishing is stopped and told so, in plain words.
Your turn. Count up instead of down: start at 1 and say 1, 2, 3, 4, 5, one per line, with a while.
The worked answer — try yours first, then peek
The book says it answers:
1 2 3 4 5
Step 4: again, a counted number of times
When the number of rounds is known ahead of time, repeat says it outright. Here it builds a row of stars one star at a time — text can grow on a shelf just like a number can:
The book says it answers:
*****
Your turn. Build a row of 8 dashes the same way, and say it.
The worked answer — try yours first, then peek
The book says it answers:
--------
Step 5: walking a row of things
A list is a row of values written between [ ]. for … in walks a list, taking each item in turn under a name:
The book says it answers:
Hello, Ada Hello, Byron Hello, Caleb That is 3 friends.
(length counts the items in a list. It is a toolbox word, and the toolbox gets a lesson of its own next; lists get one too. For now: a list is a row of values, and for visits each one.)
Your turn. Walk the list ["sun", "moon", "stars"] and say "I see the " before each one.
The worked answer — try yours first, then peek
The book says it answers:
I see the sun I see the moon I see the stars
Putting it together: the greeter that checks its work
The book says it answers:
Hello, Ada Hello, Byron Hello, Caleb Everyone was greeted.
The counter greeted is the program keeping a tally on a shelf while it works — the seed of every ledger, score, and total you will ever write.
The ledger grows: a first total
The ledger's gifts, kept for now as a row of amounts, and added up by walking the row:
The book says it answers:
Total so far: 2462 cents
Three gifts, one walk, one total. The amounts have no names attached yet — the next lesson fixes that.
Your turn
Use while to print the 3-times table from 1 to 5, one line per round, in this shape: 3 x 1 = 3. Let the program reckon the answers — do not type them yourself.
The worked answer — try yours first, then peek
The book says it answers:
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15
You can now…
- ask questions with
==,!=,<,<=,>,>=,
and get true or false answers;
- walk one of two roads with
ifandelse; - repeat while a question holds, with
while; - repeat a counted number of times, with
repeat; - walk a list item by item with
for … in; - keep a running tally while a loop works.
---