Skip to contentSkip to Content
Language guideControl flow

Control flow

Mantis executes scripts top to bottom, but provides robust control flow mechanisms to handle real-world hardware interactions. You can use conditional logic based on button states, and repeat blocks using loops with precise control.

Conditions

Mantis fully supports conditional branching using IF, THEN, ELSE, and END_IF. The conditionals evaluate runtime hardware states rather than computed data.

Available condition evaluators are:

  • BUTTON_PRESSED — True if the physical Upload button is currently held down.
  • BUTTON_RELEASED — True if the physical Upload button is currently released.
  • TRUE — Always true.
  • FALSE — Always false.

Basic IF / THEN

if-then.mantis
1REM Set a natural typing speed
2SET_SPEED $Human
3
4REM Check if the button is held
5IF BUTTON_PRESSED THEN
6STRINGLN The button is currently held down!
7END_IF

IF / ELSE

if-else.mantis
1REM Set a natural typing speed
2SET_SPEED $Human
3
4REM Branch based on the button state
5IF BUTTON_PRESSED THEN
6STRINGLN Holding the button!
7ELSE
8STRINGLN Button is not held.
9END_IF

Additionally, there are two event-driven commands tied to the button:

  • WAIT_FOR_BUTTON_PRESS — pauses the whole script until the button is physically pressed, then continues.
  • BUTTON_DEF END_BUTTON — defines a handler that runs whenever the button is pressed during execution.

Loops

LOOP repeats a block of commands. Close it with END_LOOP:

loop.mantis
1REM Set natural typing speed
2SET_SPEED $Human
3
4LOOP
5STRING .
6DELAY 500
7END_LOOP

Written bare, or as LOOP TRUE, the block repeats forever.

Mantis also supports counted loops, which will run a finite number of times. The maximum loop nesting depth (MAX_LOOP_DEPTH) is 8.

counted-loop.mantis
1REM Set natural typing speed
2SET_SPEED $Human
3
4LOOP 5
5STRINGLN This will print exactly five times.
6DELAY 100
7END_LOOP

Loop Control (BREAK / CONTINUE)

You can exit a loop early or skip to the next iteration using BREAK and CONTINUE. These apply to the innermost open loop.

loop-control.mantis
1REM Set natural typing speed
2SET_SPEED $Human
3
4LOOP
5 IF BUTTON_PRESSED THEN
6 STRINGLN Button pressed, breaking loop!
7 BREAK
8 END_IF
9
10 STRING .
11 DELAY 1000
12END_LOOP
Last updated on