Skip to content

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.

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 0
jump 8 lessThan stock 10

Note 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.

Inverted checks are not needed here: each jump is sufficient by itself.

jump 12 greaterThan enemiesNear 0
jump 12 lessThan health 200

The program goes to line 12 if at least one of the two happened.

tick 0
Set0
=
Set1
=
Jump -> 82
if
Jump -> 83
if
Set4
=
Operation5
=
Set6
=
Stop7
Set8
=
@counter0number
onnullnull
stocknullnull
givenullnull
donenullnull

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 0
jump 8 lessThan stock 10
set give 5
op sub stock stock give
set done 1
stop
set done 0

This 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.

tick 0
Set0
=
Read1
=
Jump -> 72
if
Operation3
=
Jump -> 14
if
Set5
=
Stop6
Set7
=
Stop8
@counter0number
addressnullnull
valuenullnull
wasFoundnullnull

The loop looks for the first non-empty slot in a cell — and stops walking as soon as it finds one:

read value cell1 address
jump 7 notEqual value 0
op add address address 1
jump 1 lessThan address 64
set wasFound -1
stop
set wasFound address

A 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.

tick 0
Set0
=
Operation1
=
Jump -> 42
if
Operation3
=
Operation4
=
Jump -> 15
if
Stop6
@counter0number
countnullnull
restnullnull
totalnullnull

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 2
jump 4 notEqual rest 0
op add total total count
op add count count 1
jump 1 lessThan count 10

Only 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.