Loops
Loops are used to run through a block multiple times. JM° offers several possibilities for this. For starters (and for those switching over), the use of the following control objects is recommended. In the following chapters, other possibilities are also shown.
Count
The count loop counts from a start value (default: 1) to an end value and executes the corresponding block each time.
Count( 10 )
Print "Hallo"
This will thus output 10x "hello".
In the next example, the loop runs from 3 to 7. Additionally, we can use the magic variable "it" to query the current value of the block.
Count( 3, 7 )
Print it + ". Punkt"
The Count object can also advance in larger increments. Here we count from 0 to 50, but in steps of 5.
Count( 0, 50, 5 )
Print it
While
For While, a block is repeated as long as the parameter passed is true.
b = true
While( b )
Print "b ist true"
b = false
Here the block is run through exactly once, because with the last command "b" is set to false.
Notice:
A block can contain any number of lines and also other blocks.
However, the following is not recommended, because this loop runs endlessly 😇
While( true )
Print "Never ending story"
Repeat
The Repeat loop is the counterpart of While. The block is run at least once and only then checked if the passed value is true.
Repeat( false )
Print "Buh!"
Thus, "Boo!" is output exactly once here. After that Repeat detects that the parameter is "false" and aborts.
i = 5
Repeat( i > 0 )
Print i
i =--
Here the block is repeated as long as the variable "i" is greater than 0. Thus "54321" is output.
For
Switchers may miss the "For" loop at this point. Most cases where a "For" loop is used in other languages can be solved with a "Count" loop.
But, yes, a for loop also exists in JM°. This always takes 3 parameters:
- Start value
- Condition
- Calculation
For( i = 1, i < 5, i=++ )
Print i
Here the variable "i" is defined with the start value 1. As long as "i" is now smaller than 5, the current value of "i" is output with ".print". Through the 3rd parameter, the value of the variable "i" is increased by 1 after each pass.
Important: Compared to other languages a "=++" (with assignment) and no "++" (calculation only) is used here!
Each
Mit der Each-Schleife lassen sich alle Elemente einer Sequenz (z.B. einer Liste) einzeln verarbeiten:
list = [3,8,4,1,9,2,6]
Each( list )
Print it
Diese Schleife wandert durch jedes Element in der übergebenen Sequenz und übergibt dieses an den angehängten Block. Wieder kann dann mit "it" der aktuelle Wert innerhalb des Blocks ausgelesen werden.
Loop
Die Loop-Schleife ist eine sehr einfache Schleife, welcher keinerlei Argumente übergeben werden können.
Loop
Print "Please hold the line!"
Diese Schleife wiederholt den angehängten Block. Als Ausgang dieser Schleife existieren nur 2 Möglichkeiten: - Die Schleife wird mit "Break" abgebrochen. - Die Anwendung wird mit "Exit" beendet.