Select chapter

6. Logic Control

We have learned that a Block usually contains multiple commands. In the examples earlier, the commands in a Block are executed one after another. For example, a Block for writing data into an Excel file has four commands—Open Excel File, Read Cell, Save Excel, Close Excel—, and it executes them one by one. This structure of executing commands one by one is called a sequential structure. However, in practice, there are often scenarios that call for structures beyond a sequential one. In this chapter, we will introduce more complicated structures and how to use logic control to implement these structures in Laiye RPA.

6.1 Conditional

Let's first introduce a conditional structure. As the name suggests, this structure branches into multiple directions based on how a specified condition evaluates when the process reaches this structure. If the condition is satisfied, then the process will execute one branch. Otherwise, the process will execute the other.

In the command area of Laiye RPA Creator, insert the Conditional command under Basic Commands – Grammar to create a conditional.

FIGURE 71.png
Figure 71: Conditional command

In Laiye RPA's assembly area, we can visualize the logic of a conditional. The branch that executes when the conditional is satisfied reads If "Condition", then, and the area below indicates to us that we can insert commands there. Let's insert an Output Debug Information command that outputs "This message is printed if the condition is true." The branch that executes when the conditional is not satisfied reads Else, and the area below indicates that we can insert commands there as well. Let's insert an Output Debug Information command that outputs "This message is printed if the condition is false." Now, the assembly area should look like Figure 72.

FIGURE 72.png
Figure 72: Conditional branch command - Add output debugging information

6.2 Loop Structure

Let's look at another important structure, a loop structure. A loop structure instructs the process to execute cyclically according to certain rules. Different loop structures can be classified into count loops and conditional loops. In fact, there are also two special types of count loops that iterate through arrays and dictionaries, respectively, but since we have not introduced arrays and dictionaries yet, we will leave these loops for later.

6.2.1 Count Loop

Let’s look at the count loop first. In the command area of Laiye RPA Creator, insert the Count Loop command under Basic Commands – Grammar to create a count loop. Insert an Output Debug Information command in the loop body and configure it to output the index name i.

FIGURE 73.png
Figure 73: Count loop

What exactly is the index? By inspecting property panel of the Count Loop command, we note that it has an Index Name, which we store as variable i, that counts the number of times we have iterated through this loop, and we can reference this variable inside the count loop. The Start Value and End Value properties define the range of the loop, and the Step value defaults to 1. All these values combine to instruct: initialize i as the Start Value, increment by the value of Step every iteration of the loop, until i is greater than the End Value, in which case the loop is over. By running this command, we can see that the values 0 to 10 are printed, with a total of 11 numbers.

FIGURE 74.png
Figure 74: Property of count loop

6.2.2 Condition loop

Let's look at the condition loop. Insert a Conditional Loop command from Basic Commands – Grammar to create a condition loop. Insert an Output Debug Information command in the loop body with the output content "Condition is True. Continue loop."

FIGURE 75.png
Figure 75: Condition loop

Condition Loop has the same properties as Conditional. They both only have the Condition property. The loop will continue only if the Condition evaluates to true. In order to make the loop run, we fill in True for the Condition.

FIGURE 76.png
Figure 76: Condition loop

When running this command, we find that the loop continues indefinitely, and the string "Condition is True. Continue loop." is repeatedly outputted. Here, we need to click the Stop button on the Laiye RPA Creator toolbar to interrupt the process. Keep in mind that a Condition Loop checks if the provided Condition is True every iteration and continues looping until the Condition is False. Since we provided a fixed Boolean value True for Condition, this value will always evaluate to True, and the loop will never stop.

There are two ways to resolve this problem. First, Laiye RPA provides a suite of commands that allows you to leave a loop, such as Continue Loop, Break Loop, Break and Return, and Exit Process. We’ll introduce them in the next section. Second—this is the more common solution—instead of entering a fixed value, you can enter an expression as the Condition. Initially the Condition can evaluate to True, but as the loop runs, the variables inside the expression change and eventually reach a state where the Condition evaluates to False. Then, the loop will be terminated.

For example, define a variable a and assign it an initial value of 1. Then, set the Condition of the loop a<5. Initially, this Condition is True (since a=1). However, let’s insert a command in the loop that increments a by 1. Thus, after a few iterations of the loop, a's value will be no longer smaller than 5, and the loop will terminate.

Whether you are using a Count Loop or a Condition Loop, the loop body is a command block. We can place zero, one, or many commands inside a command block, and these commands will be executed sequentially. You can even insert Conditional commands or other Count Loop or Condition Loop commands inside a command block. In other words, logic control commands can be nested. Keep this in mind.

6.3 Jump out of the Loop

As mentioned in the previous section, Laiye RPA provides a variety of commands for jumping out of the loop, including Continue Loop, Break Loop, Break and Return, and Exit Process. Some of these commands can be used not only for jumping out of the loop, but also for exiting Blocks and entire processes. Let’s go through them one by one.

The Continue Loop command is used inside a loop body to skip to the end of the current loop and continue looping (including checking if the condition still holds, executing the loop body, etc.).

FIGURE 77.png
Figure 77: Continue loop

The Break Loop command is used inside a loop body to break out of a loop, skipping to the next command after the loop command.

FIGURE 78.png
Figure 78: Break Loop

The Break and Return command can be used to break out of a loop and terminate the current Block, returning a given value (named as retValue by default).

FIGURE 79.png
Figure 79: Break and Return

Finally, the Exit Process command can be used to break out of everything and terminate the entire process.

FIGURE 80.png
Figure 80: Exit Process

In fact, the Break and Return and Exit Process commands can be used not only in a loop body, but anywhere inside a Block. Therefore, you can terminate the current Block or process wherever it suits your needs.