Nested conditions and early exit
A jump has one condition and one target. Everything else — «and», «or», else, leaving a loop — is assembled out of several jumps
Exactly one check fits into a jump: two values and one comparison. There is no “and”, no “or”
and no “else if” in it — and there never will be. But several jumps can be put in a row, and
that covers everything.
“And” is two jumps in a row
Section titled ““And” is two jumps in a row”The condition “switched on and there is enough stock” is written as two lines, and both lead to the same place — past the work:
jump 8 equal on 0jump 8 lessThan stock 10Note that the checks are inverted. jump sends execution away, so it has to be sent away
when the condition for working is not met. That is the main reason other people’s mlog
programs read badly, and it is worth getting used to straight away.
“Or” is two jumps to the same place
Section titled ““Or” is two jumps to the same place”Inverted checks are not needed here: each jump is sufficient by itself.
jump 12 greaterThan enemiesNear 0jump 12 lessThan health 200The program goes to line 12 if at least one of the two happened.
Early exit instead of nesting
Section titled “Early exit instead of nesting”Here is that pair of checks in full. Everything that should abort the work is checked at the top, and each check sends execution to the end:
jump 8 equal on 0jump 8 lessThan stock 10set give 5op sub stock stock giveset done 1stopset done 0This is called an early exit, and the checks themselves are guards. The work stays one flat piece, with no steps in it.
The temptation to write it the other way round — “if switched on, then if there is enough stock, then…” — leads to a staircase of jumps where every branch goes to its own line. In text that is still readable; in blocks it becomes a tangle of arrows.
Leaving a loop early
Section titled “Leaving a loop early”The loop looks for the first non-empty slot in a cell — and stops walking as soon as it finds one:
read value cell1 addressjump 7 notEqual value 0op add address address 1jump 1 lessThan address 64set wasFound -1stopset wasFound addressA jump from the middle of a loop to the line after it is an early exit. The program stops
at address 3, where 42 sits, instead of walking all sixty-four slots for nothing.
The line set wasFound -1 is what happens if the loop reached the end and found nothing. The
“not found” answer is always worth preparing: without it the variable keeps the previous
iteration’s value, and the program takes an old find for a new one.
Skipping a step
Section titled “Skipping a step”The opposite case: this pass of the loop should not be done, but it is too early to leave. The jump goes not outside but to the counter’s increment:
op mod rest count 2jump 4 notEqual rest 0op add total total countop add count count 1jump 1 lessThan count 10Only even numbers make it into the sum — the total comes out as 20. Odd ones are wound past
without touching the sum.
Unofficial fan project, not affiliated with Anuke. Mindustry sprites, fonts and translations © Anuke, used under GPL-3.0; Fira Code under OFL-1.1. Site code is GPL-3.0.