Control flow
if/else, while, the counted for loop, and the loop-control statements. Run a cell to see its output beside the expected one.
if / else if / else
Viv [1]
The counted loop
for i to n runs i from 0 to n−1.
Viv [2]
while and break
Viv [3]
continue
Sum only the loop indices above 2.
Viv [4]
Conditions compose with and / or / not
Viv [5]
Nested loops
break leaves the innermost loop only.
Viv [6]
Computed and fractional bounds
The bound is any expression; i counts 0, 1, 2, … while i < bound, so a bound of 2.5 runs three times.
Viv [7]
The loop variable outlives a top-level loop
At top level the loop variable is a global; after the loop it holds its last value.
Viv [8]
A while that never runs
Viv [9]
Collatz length
A while loop with if/else inside; mod(n, 2) tells even from odd.
Viv [10]
An if expression
Only the branch chosen is computed.
Viv [11]