Week 1

Conditionals

A program can look at a value and take one path or another.

A conditional is a fork. If a test is true, one block runs. Elif is another test, checked only when the earlier ones were false. Else is what remains. You can combine tests with and and or. And means both. Or means at least one.

The order of the tests matters. A more specific test has to come before a broader one, or the broad one swallows it. Match is another way to choose among exact values. It is the same idea as a chain of equality tests.

A fresh example: a cloakroom that charges nothing for a coat, a small fee for a bag, and a larger fee for anything else. Say the coat rule first. If you say 'anything else' first, coats never get their own price.

The idea
Ask, then choose Ask Choose a path Answer

Problems