Conditions use word operators — eq, ne, lt, gt, lte, gte,
combined with and, or, not. Blocks are indentation, no colons:
1 2 3 4 5 6
age = 20
if age gte 18 and age lt 65
log['working age']
else
log['not working age']expected output — from the book, verified by crow test --book
working ageChain conditions with else if (there is no elif keyword — it's just an
if in the else branch):
1 2 3 4 5 6 7 8
score = 72
if score gte 90
log['A']
else if score gte 70
log['B']
else
log['C']expected output — from the book, verified by crow test --book
BThere is no ternary expression: use an if/else statement, and set
the variable before it. A variable set inside a branch belongs to that
branch and ends with it, so a value the rest of the program reads is set
first and changed inside:
1 2 3 4 5
n = 42
size = 'small'
if n gt 10
size = 'big'
log[size]expected output — from the book, verified by crow test --book
bigReading a variable after the branch that set it ended is refused before the program runs, with the line and this fix.
1 2 3 4
i = 0
while i lt 3
i = i + 1
log[i]expected output — from the book, verified by crow test --book
3(No += in Crow — write i = i + 1. The compiler optimizes it the same.)
for iterates arrays and ranges:
1 2 3 4
total = 0
for n in [1, 2, 3, 4]
total = total + n
log[total]expected output — from the book, verified by crow test --book
10But for is bigger than this in Crow: it's an expression that collects
values, and when its body only computes, it runs its iterations in
parallel on every core. That story deserves its own chapter, and it's
chapter 6.
1 2 3
doubled = for n in [1, 2, 3]
n * 2
log[doubled]expected output — from the book, verified by crow test --book
[2, 4, 6]