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", "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.
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 Branching" Command under "Basic Commands" – "Syntax and Lexical".
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 Info" Command that outputs "This message is printed if the condition is true." The branch that executes when the conditional is not satisfied reads otherwise , and the area below indicates that we can insert Commands there as well. Let's insert an "Output Debug Info" Command that outputs "This message is printed if the condition is false." Now, the assembly area should look like Figure 107.
Let's run this process, and we find it fails.
How could this happen? It's because we haven't filled in the most important Property for the Command "Conditional Branching" — "Condition Expression". In the Property "Condition Expression" bar, there is "Condition_establishment" filled by default to remind users to change it into an effective one.
The Property "Condition Expression" actually takes a Boolean value, which can be either "True" or "False". We can obtain this value through variables, constants, or expressions, and all these concepts will be explained later in detail. For better understanding, we fill in "True" here in the text version.
Now, we can find the Command reads " If TRUE then", indicating the Property comes into effect. Run this Command, and " This message is printed if the condition is true" will be printed as expected.
Please note that two branches of the Command "Conditional Branching" have two separate Blocks, where we can insert zero or more Commands in sequence. When using the Command "Conditional Branching", it's quite common that we leave the branch "otherwise" empty.
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.
Let's look at the counter loop first. In the command area of Laiye RPA Creator, insert the "Counter Loop" Command under "Basic Commands" – "Syntax and Lexical" to create a counter loop. Insert an "Output Debug Info" Command in the loop body and configure it to output the "index" i.
What exactly is the index? By inspecting Property panel of the "Counter 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 counter loop. The "Starting Value" and "Ending 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 "Starting Value", increment by the value of "Step" every iteration of the loop, until i is greater than the "Ending 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.
Let's look at the conditional loop. Insert a "Conditional Loop" Command from "Basic Commands" – "Syntax and Lexical" to create a conditional loop. Insert an "Output Debug Info" Command in the loop body with the output content "Judgement Expression is True. Continue loop."
"Conditional Loop" has the same Properties as "Conditional Branching". They both only have the Property "Judgement Expression". The loop will continue only if the Condition evaluates to true. In order to make the loop run, we fill in "True" for the Property "Judgement Expression".
When running this Command, we find that the loop continues indefinitely, and the string "Judgement Expression 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 Conditional Loop checks if the provided Judgement Expression is True every iteration and continues looping until the Judgement Expression is False. Since we provided a fixed Boolean value True for Judgement Expression, 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 to Loop", "Jump out of the Loop", "Jump out and Return", and "Quit 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 Judgement Expression. Initially the Judgement Expression can evaluate to True, but as the loop runs, the variables inside the expression change and eventually reach a state where it 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 "Judgement Expression" as a\<5. Initially, it is True (since a=1 at this time). 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 "Counter Loop" or a "Conditional 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 other "Conditional Loop" and "Counter Loop" Commands inside a command block. In other words, logic control Commands can be nested. Keep this in mind.
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 to Loop", "Jump Out of the Loop", "Jump Out and Return", and "Quit 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 to 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.).
The "Jump Out of the Loop" Command is used inside a loop body to break out of a loop, skipping to the next Command after the loop Command.
The "Jump Out and Return" Command can be used to break out of a loop and terminate the current Block, returning a given value (named as retValue ).
Finally, the "Quit Process" Command can be used to break out of everything and terminate the entire process.
In fact, the "Jump Out and Return" and "Quit 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.