Control flow

// if, elif, and else open their blocks with ;
temperature = 18
if temperature < 10;
    print("cold")
elif temperature < 20;
    print("mild")
else;
    print("warm")

// while repeats as long as its condition holds
n = 1
while n < 100;
    n = n * 2
print(n)

// for counts with range, which stops before its end
for i in range(3);
    print("lap", i)
for i in range(10, 13);
    print(i)

// cont skips to the next iteration and break leaves the loop
for i in range(10);
    if i == 2;
        cont
    if i == 5;
        break
    print("kept", i)

// comparisons chain, and and/or/not combine conditions
x = 5
print(1 < x < 10, x == 5 and x != 6, not (x > 3 or x < 0))

prints

mild
128
lap 0
lap 1
lap 2
10
11
12
kept 0
kept 1
kept 3
kept 4
true true false

Conditions

if, elif, and while take a condition that is a bool or an int, where 0 is false and every other int is true. Strings, floats, and lists are not conditions, so write s.len() > 0 or x != 0.0 instead.

Comparisons are ==, !=, <, <=, >, and >=. They chain the way they do in Python: 1 < x < 10 means 1 < x and x < 10, and evaluates x once. < and its relatives compare numbers; == and != also compare strings, bools, and none, but not lists.

and, or, and not combine conditions and short-circuit, so x != 0 and 10 / x > 1 never divides by zero.

Loops

while checks its condition before each iteration. for walks a list or counts with range:

  • range(n) counts 0, 1, ..., n - 1

  • range(a, b) counts a, a + 1, ..., b - 1

range is only allowed as the thing a for loop iterates over; it does not make a list. Its bounds are evaluated once, before the first iteration, and assigning to the loop variable inside the body does not change the next iteration.

break leaves the innermost loop, and cont skips to its next iteration.

Next, Functions.