Skip to main content

Robot Task Activities - Loops​

The Loops activities repeat a body of activities, jump between points in a sequence, and end or skip loop iterations.

The Loops category in the activity list, with While, Do While, For, For Each, Flow, Label, Go to, End Loop and Continue Loop

Each loop activity has a LoopId property: a short ID, filled in when the activity is added, that loop variables use to refer to the loop. See Loop Variables.


While​

Runs the body while the condition is true. The condition is checked before each iteration, so if it is false at the start, the body never runs.

  • Left operand and Right operand: the two values to compare. Both accept variables and are resolved again before each check
  • Operator: the comparison. "Boolean" supports "Equal" and "NotEqual". The number types and "DateTime" support "Equal", "NotEqual", "Larger", "LargerOrEqual", "Smaller" and "SmallerOrEqual". "String" supports "Equal", "NotEqual", "Contains" and "NotContains"
  • Type: the value type of the comparison: "Boolean", "Decimal", "Double", "DateTime", "Int16", "Int32", "Int64" or "String"
  • MaxIterations: in the Properties pane, the highest number of iterations allowed. Default: "1000"
  • Body: the activities to repeat

Do While​

Runs the body while the condition is true. The condition is checked after each iteration, so the body always runs at least once. The settings are the same as for While.

note

When a While or Do While loop would run more than MaxIterations times, the activity fails with the error "The maximum loop iteration has reached."


For​

Runs the body for each value of a counter, from For X up to, but not including, To Y.

The For activity with the For X, To Y and Step fields and the Body

  • For X: the start value of the counter
  • To Y: the end value of the counter. The body does not run for this value
  • Step: the amount added to the counter after each iteration. A negative step counts down. A step of "0" repeats the body without end, because the For activity has no MaxIterations limit
  • Body: the activities to run in each iteration

For example, For X "1", To Y "3" and Step "1" runs the body twice, for 1 and 2. For X must be smaller than To Y, or larger when Step is negative. If the two are equal, the activity fails. All three fields accept variables, so the end value can come from a value read while the Task runs.

warning

To run the body once for each row of a list, starting at 1, set To Y to the row count plus 1. With To Y set to the row count itself, the last row is skipped and the Task reports success.


For Each​

Runs the body once for each line in a block of text, such as the content of a data file or the value of a variable.

The For Each activity with the For each row x in, Use column, Field separator, Text qualifier, Line break and Start row fields and the Body

  • For each row x in: the text to loop through, for example {USERVAR(List)}
  • Use column: the index of the column the loop reads from each line. Default: "0"
  • Field separator: the character between columns, for example "Tab" or "Comma". Default: "Tab"
  • Text qualifier: the character around values that contain the separator
  • Line break: the line break the text uses, for example "CrLf". Default: "CrLf"
  • Start row: the first line to process. Default: "1". Set it to "2" to skip a header line
  • Body: the activities to run for each line

Empty lines are skipped.


Flow​

Runs the activities inside it in order, and lets Go to activities jump to Label activities. Place the Label and Go to activities directly inside the Flow: Go to finds only labels that are direct children of the Flow.

The Flow activity with a Label activity and a Go to activity whose Label field is set

Label​

Marks a position that a Go to activity can jump to.

Go to​

Jumps to the label chosen in its Label field. Running continues from that Label rather than from the next activity. If the label is not found, the activity fails.


End Loop​

Ends the loop it is in. Running continues with the first activity after the loop.

Continue Loop​

Ends the current iteration and starts the next one from the top of the loop body.

Continue Loop is the key to clean exception handling inside a loop. When a record cannot be processed because of a problem with its data, write the record to an exception log and then use Continue Loop as the last activity in that branch. The loop moves on to the next record instead of stopping the whole Task or retrying the same record. See Data Processing & Logging for a full walkthrough of that pattern.


Loop variables​

Loop activities expose values about the current iteration as variables. The number of the current iteration, starting at 1, is:

{RPALOOP(a381|Iterations)}

The first parameter is the LoopId of the loop, so the value differs per loop. To insert the reference rather than typing it, right-click the loop activity, point to Loop Variables and click Number of loop iteration so far. This copies the reference, without the braces, to the clipboard. The same menu copies the other loop variables, such as the current value and the maximum number of iterations, and Copy LoopId copies the ID alone.

A common use is passing the iteration number as the row number to a List Variable lookup:

{USERVAR(lvDateReformat|GetCell|{RPALOOP(a381|Iterations)}|Date|false)}

Tips​

  • Prefer For Each over a counted For loop when looping through a data file, so that the loop matches the number of records present
  • Set Start row to "2" when the source text has a header line, or the header is processed as data
  • Put the activity that marks a record as complete at the very end of the loop body. A record is then marked as processed only if every step before it succeeded
  • Avoid nesting more than two loops. Move the inner work into a separate Robot Task if the logic grows