Mat|r provides several statements for flow control, such as while loops to perform a task many times; ‘if / if’ Else to execute different code branches based on certain conditions; a bucle for-in that allows the user to easily iterate over arrays; and a ‘try/catch’ sentence with which you can execute codes that are exposed to extraordinary conditions, and manage them.


if / if else

This statements are used to execute different pieces of code based on certain conditions. For example if an error occurs, you can still execute a different part of the code or show a message. In order to do this, we make parts of our code conditional.

Simple if statements have a condition and only if this condition is met, the part of the code within the block is executed and then, the execution of the programm continues normally.

Integer count = 2
count = count - 2
if (count == 0) {
  broker.ui.showAlert("Count", "Count value is zero")
//Count, Count value is zero

if else statement if formed by a condition and 2 code blocks. And only if the condition is met, the code within the first block is executed. Otherwise, the code of the second block is executed, the else block.

Integer count = 2
count = count - 1
if (count == 0) {
  broker.ui.showAlert("Count", "Count value is zero")
}else {
  broker.ui.showAlert("Count", "Count value is different than zero")
//Count, Count value is different than zero


A while loop executes a group of statements until the conditions becomes false. These types of loops are used when the number of iterations in unknown before the first iteration begins.
A while loop starts by analysing a condition. If the condition is true, the code block is repeated until the condition becomes false.

Integer count = 3
while (count > 0) {
  broker.ui.showAlert("Count Value", count.toString())
  count = count - 1
//Count Value, 3
//Count Value, 2
//Count Value, 1


Aforin loop is used to iterate over a collection, for exemple over an array.

Array<String> names = []
for name in names {
    broker.ui.showAlert("Hi", name)
// Hi, John
// Hi, Freedy


These statements are used as exception handling mechanisms in Mat|r and allow the developer to handle and deal with unusual conditions. These statements disconnect the detection and handling of these conditions, and automate the spread of exceptions from the point they were detected to the handling point. As a result, the code will be cleaner, and easy to read and maintain.

With try statement a code block is defined, which is a domain where exceptions are handled: a code that can potentially throw an exception.

Below, within thecatch(e), a code block for the handling of exceptions thrown in the try block is defined. The catch parameter is the exception thrown in the try block.

Array<Integer> ages = [] //empty array
Integer ageAverage = 0
try {
  for age in  ages {
      ageAverage =  ageAverage + age
  ageAverage = ageAverage / ages.size()
} catch (e) {
  broker.ui.showAlert(, e.reason())

//DivisionByZero,   Has been a division by zero error.