Skip to content

Choosing a value without a branch

A branch for the sake of one value is unnecessary — but select is not always needed either: a comparison already has an answer

select puts one of two values into a variable: the first if the condition holds, the second if it does not. In the block it reads as words: total = if copper >= 100 then 1 else 0.

Before picking it up it is worth understanding when it is not needed — otherwise people start writing with it what is shorter without it.

tick 0
Sensor0
=
in
Operation1
=
Select2
= if
then else
Select3
= if
then else
@counter0number
coppernullnull
enoughnullnull
captionnullnull
howManynullnull

Press “step” four times.

The example’s second line is op greaterThanEq enough copper 100, and enough ends up holding a one. No choice was required: a comparison answers with a one or a zero by itself, and that is already a finished value.

As soon as the values stop being one and zero, a comparison stops helping.

The third line chooses between two strings: caption becomes “enough”. That cannot be had through op at all — a comparison can only answer with a number.

The fourth lineselect howMany lessThan copper 100 copper 100 — caps a number from above: if there is less than a hundred copper, take what there is; otherwise take exactly a hundred. howMany ends up as 100.

The general rule: select is needed when both values are arbitrary — numbers other than zero and one, strings, items, buildings, variables.

What is being chosen With what
a one or a zero by a condition op with a comparison
one of two ready values select
one of two actions jump: that is a branch, not a value
the larger or smaller of two numbers op max and op min — shorter

Both halves of select are values that already exist: numbers, strings, variables, objects. The instruction runs nothing and skips nothing, it simply takes one of two.

There are eight of them, and the buttons carry the same signs — ==, ===, not and the rest from the table. The words “if”, “then” and “else” in the block are a translation; without it they are if, then, else.

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.