Control flow
c0de has one condition and four loops, all made of a keyword line and a code block between BEGIN and END. They look like the statements of BASIC or Pascal, but the details differ: there is no ELSE IF, FOR includes its end value, and REPEAT goes on while its condition is true. This lesson writes FizzBuzz, goes through a hash, and revisits the prime numbers of the guide.
What you need
A creative world with cheats on (see Tutorials), a trigger block, a wrench, a button, a few uninitialized variable items, a variable item named n of type INTEGER and one named limit of type INTEGER. For the prime numbers: a comparator with a redstone lamp.
The statements
| c0de | Python | Notes |
|---|---|---|
IF c ; … END ELSE … END IF | if c: … else: | no ELSE IF: nest another IF in the ELSE block |
FOR i INTEGER 1 ; n ; | for i in range(1, n + 1): | the end value is included |
FOR x FLOAT 0 ; 1 ; 0.25 ; | – | the third value is the step, 1 if left out |
EACH item list | for item in list: | for a hash: the keys, sorted |
WHILE c ; | while c: | checks before the block |
REPEAT … END REPEAT c ; | while True: … if not c: break | checks after the block, runs while c is true |
BREAK / BREAK c ; | break / if c: break | leaves the innermost loop |
RETURN | return | ends the function; its results keep their values |
EXIT 3 ; | sys.exit(3) | ends the whole process with an exit code from 0 to 15 |
Each of them is followed by a code block. Its END names the statement again: END FOR, END EACH, END WHILE. Only the first block of an IF with ELSE ends with a bare END. A code block can't be empty; NOOP fills one that has nothing to do.
FizzBuzz
PROGRAM fizzbuzz
NOOP
NOOP
FUNCTION fizzbuzz
PARAMETER n INTEGER 15
RESULT output STRING
BEGIN
FOR i INTEGER 1 ; n ;
BEGIN
IF i \ 15 = 0 ;
BEGIN
ASSIGN output output ⊕ "FizzBuzz " ;
END
ELSE
BEGIN
IF i \ 3 = 0 ;
BEGIN
ASSIGN output output ⊕ "Fizz " ;
END
ELSE
BEGIN
IF i \ 5 = 0 ;
BEGIN
ASSIGN output output ⊕ "Buzz " ;
END
ELSE
BEGIN
ASSIGN output output ⊕ i ⊕ " " ;
END IF
END IF
END IF
END FOR
END FUNCTIONSet the trigger block to the program fizzbuzz and the function fizzbuzz. With one uninitialized item, output is "1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz ". Put in the item n with another value to count further; with 0, the loop doesn't run and output stays empty.
- The FOR loop counts from 1 to
n, both included. Its start, end and step are evaluated once, before the first round: changingninside the loop doesn't change how often it runs. - Without ELSE IF, every further case is an IF inside the ELSE block of the one before, one level deeper each time. The first END of each IF stands alone, the last one is
END IF. - An IF needs a BOOLEAN.
IF i \ 3 ;would raise TYPE_MISMATCH: writeIF i \ 3 = 0 ;.
Going through a hash
EACH goes through a copy of an array or a hash. For an array, the loop variable gets the elements in order; for a hash, it gets the keys, sorted by their Unicode code points, and you read the value with the key as index.
PROGRAM stocklist
NOOP
NOOP
FUNCTION stock
RESULT report STRING
BEGIN
VARIABLE chest HASH INTEGER
BEGIN
torch 64
Diamond 3
apple 12
coal 0
END VARIABLE
EACH item chest
BEGIN
IF chest[item] > 0 ;
BEGIN
ASSIGN report report ⊕ item ⊕ "=" ⊕ chest[item] ⊕ " " ;
END IF
END EACH
END FUNCTIONSet the trigger block to the program stocklist and the function stock, put in one uninitialized item and press the button. Right-click the item report in the output inventory: it is "Diamond=3 apple=12 torch=64 ". Capital letters come before small ones, and coal is left out by the IF. The order is the same every time, unlike the order of a dict or a HashMap that you shouldn't rely on.
Prime numbers, revisited
The guide finds the primes up to 20 with two FOR loops. This version takes the limit as a parameter, stops testing at the square root, and returns the primes as an array:
PROGRAM primesrevisited
NOOP
NOOP
FUNCTION primes
PARAMETER limit INTEGER 20
RESULT found ARRAY INTEGER
RESULT count INTEGER
BEGIN
IF limit < 2 ;
BEGIN
EXIT 1 ;
END IF
FOR candidate INTEGER 2 ; limit ;
BEGIN
VARIABLE prime BOOLEAN TRUE
VARIABLE divisor INTEGER 2
WHILE divisor * divisor ≤ candidate ;
BEGIN
ASSIGN prime candidate \ divisor ≠ 0 ;
BREAK NOT prime ;
ASSIGN divisor divisor + 1 ;
END WHILE
IF prime ;
BEGIN
ASSIGN found[count] candidate ;
ASSIGN count count + 1 ;
END IF
END FOR
END FUNCTIONSet the trigger block to the program primesrevisited and the function primes, put in two uninitialized items and press the button. found is [2, 3, 5, 7, 11, 13, 17, 19] and count is 8. With an item limit = 100, count is 25.
IF limit < 2 ;ends the whole process withEXIT 1 ;. A comparator next to the trigger block then gives a signal of strength 1, and the lamp lights up; after a normal end, the exit code is 0. EXIT still writes the results of the entry function as they are at that moment: an empty array and 0.- The inner loop is a WHILE, because its end,
divisor * divisor ≤ candidate, changes withdivisor. A FOR would evaluate its end only once. BREAK NOT prime ;is a BREAK with a condition: it leaves the WHILE loop only if the condition is true, likeif not prime: break.VARIABLE prime BOOLEAN TRUEis in the code block of the FOR loop, so every round starts with a newprimethat is true. Variables of a code block are forgotten when it ends.ASSIGN found[count] candidate ;appends:countis always the length of the array, and assigning to the index right after the end adds an element.
Differences you'll trip over
FOR i INTEGER 1 ; 10 ;runs ten times, including 10 – unlikerange(1, 10).FOR i INTEGER 10 ; 1 ;doesn't run at all. Counting down needs a negative step:FOR i INTEGER 10 ; 1 ; -1 ;. A step of 0 raises INVALID_LOOP_STEP.- The loop variable of FOR and EACH can't be changed (a red line), and it doesn't exist after the loop.
- REPEAT runs while its condition is true. It is not the REPEAT … UNTIL of Pascal and many BASICs:
END REPEAT i ≤ n ;goes on as long asi ≤ n. - There is no ELSE IF, no CONTINUE, no labeled BREAK, no switch and no GOTO. BREAK leaves only the innermost loop.
- Changing an array or hash inside an EACH over it doesn't change the loop: it goes through a copy.
- EXIT ends the process, not the function, and only takes 0 to 15. 15 is also the code of a crash; a value outside raises INVALID_EXIT_CODE.
Try it yourself
- Write FizzBuzz with a REPEAT loop instead of FOR. What does it return for
n= 0?
Solution
PROGRAM fizzbuzzrepeat
NOOP
NOOP
FUNCTION fizzbuzz
PARAMETER n INTEGER 15
RESULT output STRING
BEGIN
VARIABLE i INTEGER 1
REPEAT
BEGIN
VARIABLE word STRING
IF i \ 3 = 0 ;
BEGIN
ASSIGN word "Fizz" ;
END IF
IF i \ 5 = 0 ;
BEGIN
ASSIGN word word ⊕ "Buzz" ;
END IF
IF word = "" ;
BEGIN
ASSIGN word "" ⊕ i ;
END IF
ASSIGN output output ⊕ word ⊕ " " ;
ASSIGN i i + 1 ;
END REPEAT i ≤ n ;
END FUNCTIONFor 15, the result is the same as before. For 0, it is "1 ": REPEAT checks its condition only after the first round. This version also builds each word with three IFs instead of nesting them: word is new in every round, so it starts empty.
- Add a result
sumto the primes that adds them up with an EACH loop.
Solution
PROGRAM primesum
NOOP
NOOP
FUNCTION primes
PARAMETER limit INTEGER 20
RESULT found ARRAY INTEGER
RESULT count INTEGER
RESULT sum INTEGER
BEGIN
IF limit < 2 ;
BEGIN
EXIT 1 ;
END IF
FOR candidate INTEGER 2 ; limit ;
BEGIN
VARIABLE prime BOOLEAN TRUE
VARIABLE divisor INTEGER 2
WHILE divisor * divisor ≤ candidate ;
BEGIN
ASSIGN prime candidate \ divisor ≠ 0 ;
BREAK NOT prime ;
ASSIGN divisor divisor + 1 ;
END WHILE
IF prime ;
BEGIN
ASSIGN found[count] candidate ;
ASSIGN count count + 1 ;
END IF
END FOR
EACH p found
BEGIN
ASSIGN sum sum + p ;
END EACH
END FUNCTIONThe sum of the primes up to 20 is 77. The function now has three results, so it needs three uninitialized items.
- You start the primes with
limit= 1. Which exit code does the comparator read, and which items come out?
Solution
Exit code 1, so the comparator gives a signal of strength 1. EXIT ends the process normally, it isn't a crash: the item limit and one uninitialized item go in, and the result items found (an empty array) and count (0) come out.
Read more
- The language: IF and ELSE, FOR loops, EACH loops, WHILE and REPEAT, BREAK, RETURN, EXIT
- Statements: Conditions and Loops: the blocks and their recipes
- Examples: Prime numbers: the original
- Running programs: Exit codes: what the comparator reads