Looping

Robin contains three kind of loops that can be used to repeat part of an automation.

Simple Loop

The first kind of loop is a simple loop with which you can run part of an automation for a certain number of times. The syntax for this loop is as follows.

loop i from 1 to 10 step 1
# Steps within loop

end

This loop will repeat 10 times. The variable i is used to increment up to 10. Step 1 denotes that variable i will be increased by 1 every time. As all blocks, the loop requires an end in the end of the block.

The variable and step in a simple loop are optional. The Loop can be set in a format counting from 1 to 10. The default step is 1.

For each Loop

The For each loop, iterates through a list, a data-table or a data-row.

In the example below, the list includes numbers from 1 to 5. Hence this loop will repeat five iterations.

loop foreach value in [1,2,3,4,5]
# Steps within loop

end

While Loop

The while loop, will repeat the actions nested in the loop, as long as the conditions set in the loop meet.

In this example, the variable i is checked to be true. As long as it’s true it will keep iterating.

loop while i = true
# Steps to perform while file.txt exists

    set x to true
end

Note, that any action which provides a boolean variable result can be used within a While loop using parenthesis ().

In the example below, the file exists action is used.

loop while (File.Exists File: "C:\file.txt")
# Steps to perform while file.txt exists

    set x to true
end

The list below includes the actions that may be used withtin a loop block.

  • Services.Installed
  • Services.Paused
  • Services.Running
  • Services.Stopped
  • File.Exists
  • Folder.Exists
  • Folder.Empty
  • System.ProcessRunning

Exiting a Loop

There is an option to exit a loop during one iteration. This is achieved using the “Exit Loop” statement. That way the loop will be terminated.

The below examples calculates if a number is more than 4 and then exits the loop.

set list to []
loop foreach value in [1,2,3,4,5,6]
    if value < 5 then
        set list to list + value
    else
    exit loop
    end
end

Moving to the next iteration of a Loop

Alternatively, to just skip the current loop iteration, use the “Next Loop” statement.

The below example calculates if the number is odd and adds it to a list. If the number is even, it goes to the next iteration.

set list to []
loop foreach value in [1,2,3,4,5,6]
    set modulo to value mod 2
    if modulo = 0 then
        next loop
    end
    set list to list + value
end