Skip to main content

Logical control

As mentioned earlier , A process block usually contains multiple commands. In the previous example, multiple commands in a process block are executed one by one in sequence. For example, a process block completes the function of writing Excel data, and executes four commands in sequence, namely "open Excel", "read cells", "save Excel" and "close Excel". Generally, we call this sequential process structure sequential structure. However, the actual RPA scenario is far more complex than this. This chapter introduces the slightly more complex process structure and how to use logic control in Laiye Automation Platform to realize these complex process structures.

Conditional branch

The process structure introduced first is called conditional branching. What is conditional branching? As the name implies, it refers to branching according to certain conditions when the process structure runs to a certain step: when the conditions are met, it follows one of the branches; When the conditions are not met, follow another branch.

Let's look at the specific command usage. In the command list of "Laiye Automation Creator", select "Basic Commands" Expand and select "Syntax and Grammar" Expand and find "If Condition" , With this command, you can create a conditional branch.

Conditional branch command

In the command assembly area, you can clearly see the effect of this command. When the conditions are met, the branch reads: Judgment according to conditions , Then the following line reads: If Conditions Then , Its function is very obvious: when it runs here, it will judge the current condition. When the condition is established, it will run the next command. We try to insert the command to be run when the "condition holds" below. For example, we can insert a command of "output debugging information", whose output content is If the condition is true, this message will be output . Note that when inserting this command, there is a solid blue line and a dashed line indicating the insertion position. At this time, hold down the left mouse button and move the mouse left and right to the position of the dashed line to adjust the insertion position. The "output debug information" command must be inserted into If Conditions Then The lower right of (called "indent") indicates that it is subordinate to the previous command (as shown in figure a below). Click If Conditions Then The minus sign on the left will fold the commands subordinate to it, indicating that they are subordinate). Instead of inserting it into If Conditions Then As shown in Figure B below), so that there is no dependency between the two. The command "output debug information" will not run until the condition is judged.

Dependency of conditional branch command

Sometimes, conditional branch only needs to run the specified command when the conditions are met; Sometimes, other specified commands need to be run when the conditions are not met. At this time, you need to find the command "otherwise perform subsequent operations" from the command list of "Laiye Automation Creator" and drag it into the assembly area. Move the mouse left and right to make its insertion position different from the above If Conditions Then The left side is aligned, which means that the two are juxtaposed. The assembly area will display: Otherwise , You can also insert a subordinate below Otherwise The "output debugging information" command of the If the condition is not true, this message will be output . In the same way, pay attention to adjusting its left and right positions to make it and Otherwise Commands have dependencies. After completion, the command assembly area is roughly as shown in the following figure:

Conditional branch command - add output debug information

We tried to run it, but the expected content was not output, and we saw a warning message (represented by orange and yellow):

Conditional branch command - run result

Why did this happen? At this time, the most important attribute of the conditional branch command is "judgment expression", which we have not filled in at all. Open the attribute area of the "conditional branch" command. In the "judgment expression" attribute, Laiye Automation Platform helps us fill in one by default Conditions But this just gives us a hint. We need to Conditions Replace this sentence with a real conditional expression, such as x > 1 . If it is not replaced, it will Conditions As a variable (because this is indeed a legal variable name), and judge whether its value is True . If the reader is still unfamiliar with the concepts of variables and expressions, please refer to the Basic knowledge of programming .

Conditional branch command - conditional expression

For the convenience of demonstration, we click here Conditions Click the "close" button on the right to clear it. Then fill in a True , It means that the condition here is always "true".

Conditional branch command - conditional expression is true

At this time, it can be seen that in the command assembly area, the branch when the conditions are met reads: If True Then , This indicates that the judgment expression attribute has taken effect. Run this command to get correct results and output debugging information: If the condition is true, this message will be output .

It should be noted that the two branches of the conditional branch command are two command blocks. In the command block, you can place one command or multiple commands in sequence as needed, or you can leave none empty. The command "otherwise perform subsequent operation" is optional. It can be placed or not, and can be selected according to actual needs.

Loop Structure

Let's introduce another important process structure, which is called Loop Structure. What is called Loop Structure? As the name implies, it refers to the circular execution of process according to certain rules. According to the different loop rules, it can be divided into Counting Loop and conditional loop. Traversing arrays and traversing dictionaries are actually two special Counting Loop. However, since we haven't talked about arrays and dictionaries yet, traversing arrays and traversing dictionaries will be discussed later.

Counting Loop

Let's look at the Counting Loop first. In the command list of "Laiye Automation Creator", select "Basic Commands" Expand and select "Syntax and Grammar" Expand and find "For Loop" , With this command, a Counting Loop can be established. After adding this command to the command assembly area, we will add a "output debugging information" command in its subordinate scope, which will add "index name" i And sequentially output as debugging information.

Counting Loop

This leads to the concept of "index name". When we open the attribute list box of this command, we can see that the command has four attribute: "index name" is the value used for counting. Here, a variable is used i express, i It can also be used in the loop body (in the above example, we will i Sequentially displayed); "Initial value" and "end value" calibrate the range of the cycle, "step" is 1 by default, and can be modified to other values. The three values together mean: i Starting from "initial value", the value of "step" will be automatically increased every cycle, and the cycle will not end until it is greater than "end value". When we run this command, we can see that 0 to 10 are printed out, and the loop is executed 11 times in total.

Attribute of counting cycle

Condition loop

Let's look at conditional loops. In the command list of "Laiye Automation Creator", select "Basic Commands" Expand and select "Syntax and Grammar" Expand and find “While Loop” , With this command, you can create a conditional loop. After adding the command to the command assembly area, we will add a command of "output debugging information" in its subordinate scope. This command outputs "The condition is true to continue the loop" .

condition loop

The attribute area of the conditional loop command is the same as the conditional branch command. It has and only has one attribute: judgment expression. If the "judgment expression" is true, the loop will execute. To make the loop execute, we fill in the "judgment expression" True .

Attribute of conditional loop

When we execute this command, we find that the string will be output all the time "The condition is true to continue the loop" , Without automatically stopping. We need to click the "stop" button on the "Laiye Automation Creator" toolbar to forcibly stop the execution of the process. This is because the so-called "conditional loop" means that when certain conditions are met, a sentence block will be executed in a loop. Accordingly, if the condition is not satisfied, the statement block will not be executed. In the example just now, in order to make the loop execute, we filled in a fixed value in the "judgment expression" attribute True , and this value is always "true" and will not change with the loop, so the "judgment expression" is always true and the loop will run endlessly.

So how to solve this problem? In the first method, Laiye Automation Platform provides a variety of commands to Jump Out of the Loop, including "Continue to Loop", "Jump Out of the Loop", "Jump Out and Return" and "Quit Process". These methods will be described in the next section; The second method, which is also more common, is to fill in an expression in the "judgment expression". At first, the value of this expression is true. As the loop goes on, the value of the expression changes constantly. When the loop reaches a certain state, the expression is no longer true, and the loop ends.

Let's take an example: first, define a variable a , And give this variable an initial value of 1 ; Then fill in "judgment expression" a < 5 , At first, this expression was true (because at this time a be equal to 1 ), The cycle starts to execute; Then, in the loop a The value of plus 1 ; After several cycles, a The value of is no longer less than 5 , The loop then exits.

It should also be noted that the loop body of both "Counting Loop" and "conditional loop" is a command block. One command can be placed in the command block, or multiple commands can be placed in sequence. The commands in the command block can also be "conditional branch" commands or "Counting Loop" or "conditional cycle", that is, logic control commands can be nested.

Loop jump

As we said in the previous section, Laiye Automation Platform provides a variety of commands to jump out of the Jump 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 to jump out of the loop, but also to exit process blocks and process. Let's explain them separately.

The first is the command "Continue to Loop". The so-called "Continue to Loop" means that during the execution of the loop body, it is no longer executed This time After terminating this cycle, it jumps back to the beginning of the cycle body and continues to execute the next cycle.

Continue to Loop

The second is the "Jump Out of the Loop" command. The so-called "Jump Out of the Loop" means that in the process of executing the loop body, the loop command is no longer executed, but directly jump out of the loop body and continue to execute the command after the loop statement.

Jump Out of the Loop

The second is the "Jump Out and Return" command. The so-called "Jump Out and Return" means that in the process of executing the loop body, the loop command is no longer executed, but directly jumps out of the process block and returns retValue This value.

Jump Out and Return

Finally, there is the command "Quit Process". The so-called "Quit Process" means that in the process of executing the loop body, the loop command is no longer executed, but the Quit Process is directly executed and the execution of the process is suspended.

Quit Process

It should be noted that the "Jump Out and Return" command and the "Quit Process" command can be used not only in the loop body, but also in the conditional branch and sequence structure. That is to say, at any position of the process block, the "Jump Out and Return" command and the "Quit Process" command can be used to jump out of the process block and the Quit Process at any time if necessary.