﻿Iterative blocks   # Iterative blocks

GoManga MathStudio Language has various type of loops. Built-in loops can be used in the majority of the algorithms, moreover, if you need to make your custom loop, you can use the goto sentence. GoTo

In MathStudio Language you can use the goto sentence to iterate inside the code. You can declare a marker using the ":" (for example: :MarkerName) token before the name of the marker and then use the goto MarkerName. Usage example:

```[i] = 0

:loopMark
[i]++
output {i}
if {i} < 10 then
goto loopMark
endif

pause

//The code prints the sequence of numbers from 1 to 10.```

Note that if there are two marker with the same name, the code will be executed from the first one in the code. Preselected Loop

The loop block is a preselected loop. The code between then and endloop is exectuted while the condition between loop and then is satisfied. This is a form of iteration which does not involve recursion. Instead, the syntax creates a loop which is a body of code which is repeated over and over again while some condition is true. The loop is terminated when the specified condition is false. For example:

```[i] = 0

loop {i} < 10 then
output {i}
[i]++
endloop

pause

//The code prints the sequence of numbers from 0 to 9.```

The loop block above is equal to the following code:

```[i] = 0

:startLoop
if not({i} < 10) then
goto endLoop
endif
output {i}
[i]++
goto startLoop
:endLoop

pause```

Note that a code written with goto sentences is less readable than a code written with loops. Postselected Loop

The do...while block is a postselected loop. The code between do and while is exectuted and repeated if the condition after while is satisfied. This is a form of iteration which does not involve recursion. The syntax creates a loop which is a body of code which is executed at least one time and repeated over and over again while some condition is true. The loop is terminated when the specified condition is false, after the last execution. For example:

```[i] = 0

do
output {i}
[i]++
while {i} < 10 then

pause

//The code prints the sequence of numbers from 0 to 10.```

The do...while block above is equal to the following code:

```[i] = 0

:startLoop
output {i}
if {i} < 10 then
goto startloop
endif

pause``` Count loop

The for block is a count loop. The code between then and end is exectuted until the variable between for and to reaches the value between to and do with the operation indicated between do and then. Unlike many other kinds of loops, the for-loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for-loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For-loops are also typically used when the number of iterations is known before entering the loop. For-loops are the shorthand way to make loops when the number of iterations is known, as every for-loop could be written using a preselected loop. For example:

```for [i] = 0 to 10 do [i]++ then
output {i}
endfor

pause

//The code prints the sequence of numbers from 0 to 9.```

The code above is equal to the following code:

```//start of the for loop
[i] = 0

:startLoop
if not({i} < 10) then
goto endLoop
endif
output {i}
[i]++
goto startLoop
:endLoop
//end of the for loop

pause```