Skip to main content

Appendix: basic knowledge of programming

When reading this article, we assume that you have the most basic experience in any programming language, and at least understand the basic concepts of data types, variables, and conditional judgments. If we don't even have these concepts, we will briefly introduce them in this chapter. Laiye Automation Platform requires very little programming foundation and is very easy to learn, so don't worry. It's enough to patiently read and understand the concepts in this chapter.

Of course, if you already have a programming foundation, you can completely avoid reading the contents of this chapter.

Let's start with a Excel table. The content in this table is completely fictitious. We only use it to explain the following important concepts.

Fictitious Excel table

Data

RPA's main work is usually dealing with various data. What is data? Let's imagine that there is an Excel table, in which many boxes have been filled in, and these contents are all data. Data is the information exchanged between computer and human.

In fact, data can also be subdivided into structured data and unstructured data. What is written neatly in a grid like this is obviously structured data. Most of the data we need to contact is also structured data, so it is enough to understand the concept of data with this table. Most of the images, sounds, videos and web pages are unstructured data, so I won't talk about them here.

Data type

In general programming languages, data is divided into several different types. The common data types in Laiye Automation Platform include numeric type, string type, boolean type, null type, compound type, etc. In the primary course, it is enough to learn several types except compound type. Compound type is learned in the intermediate course.

The numerical type is a number, which may be an integer or contain decimal places (generally called floating-point numbers in computers), such as "order quantity", "sales" and so on in the instance table;

String type is usually a string of text, usually with a pair of double quotation marks( " ) Or a pair of single quotation marks( ' ) Wrap it up to show the difference; Backslashes are used in strings to represent some special characters, such as: \t Represents a tab, \n Represents line feed, \' Represents a single quotation mark, \" Represents double quotation marks, \\ Represents the backslash itself, etc. The middle of the string can be wrapped directly, and the newline character will also be part of the string. You can also use three single quotation marks before and after( ''' ) To represent a string, which is called a long string. In a long string, you can write carriage return, single quotation marks and double quotation marks directly, without \n , \' or \" .

Booleans have only "true" and "false" values, which are often called "yes" and "no" Wait, the connotation is the same. The value of null type is always Null , Case insensitive.

How to distinguish these data types? Generally speaking, numerical types can be added, subtracted, multiplied and divided; Strings are usually concatenated without other operations; Boolean types usually have only logical operations such as "and", "or" and "not". For example, the data in the column "customer name" in the table is meaningless to add, subtract, multiply, divide and perform logical operations, so it should be of string type. When writing, add a pair of double quotation marks, such as "Jordan" .

The data in the "Order No." column in the table are essentially figures that can be added, subtracted, multiplied and divided, but their addition, subtraction, multiplication and division are meaningless. Therefore, it can be processed as a numerical type or a string type, and the designer can consider it as necessary.

Variable

In the above Excel table, each data is saved in a small grid. In addition, Excel has set a name for each small grid, for example, the grid occupied by the data of 261.54 is called D2. The data in this grid may change, for example, sales may change, but the name of this grid, D2, is unchanged. As long as we take the data in the D2 grid in Excel, we can get the latest value.

"Variable" is a common concept in programming. Like the grid in Excel, variables are also equivalent to a grid in which data can be stored. A variable also has a name. You can get the data saved in the variable by name or "assign" the value to the variable by name, that is, you can set the data saved in the variable in Settings.

The lattice naming in Excel is in the form of "letters + numbers", while the variable naming in the programming language is much more flexible. It can be a very long English word or several words connected by underscores. Besides the first character, the number 0-9 can also be used after it. Some programming languages, including those used by Laiye Automation Platform, can also use Chinese characters to name variables. It is generally recommended to set the variable name to a little longer, preferably meaningful words or phrases rather than "code" like D2. This is mainly to make us see more clearly when reading, and has no impact on the operation of the program. Laiye Automation Platform variable names are case insensitive.

Variables in Laiye Automation Platform are of dynamic type. There is no need to declare the type of variables when defining them, that is, the value and type of variables can be changed dynamically during the running process. This also conforms to the habits of general scripting languages such as python, Lua and JavaScript.

The way to define variable names is: Dim VariableName

While redefining the variable name, you can assign an initial value to the variable: Dim VariableName=Value

If you want to define multiple variables, you can define them as follows: Dim VariableName1=Value, VariableName2 or Dim VariableName1=Value1, VariableName2=Value2

Similarly, if you want to define a constant, you can define it as follows: Const ConstantName1=Value1, ConstantName2=Value2

Expression

Variables and variables, or variables and fixed data can be operated on, and the formula of their operation is called "expression". Since the value of a variable may change, the value of an expression may also change. After writing an expression in the programming language, as long as it runs to the line where the expression is located, it will calculate the value of the expression according to the data stored in the variable.

For example, x + 2 It's an expression when we're not sure x The value of this expression cannot be determined. If you run to this line and find that x The value of is 3, then x + 2 The value of is 5.

Of course, some operations are meaningless, such as dividing two strings, which is meaningless. However, since the value of the variable has not been determined when we write the expression, we may not be able to immediately determine that there is a problem with the expression. When it runs to this line, the computer finds that there is an error in the expression and can not continue to run. It usually reports an error and exits.

Conditional judgment

When writing a program, we usually write it line by line. When the program is running, it usually runs line by line from top to bottom. Of course, this operation mode is not flexible enough. We often want to judge a certain condition during operation, and then decide whether to execute a certain statement according to the condition. This is the conditional judgment statement.

Conditional judgment statements are written differently in different programming languages, but their general form is usually the same:

If <expression> Then
Statement1
Statement2
End of If Judgement

This means that when the "if" line is run, the value of the expression is calculated. The value of this expression should be Boolean. If not, it is usually automatically converted to Boolean. According to the value of this expression, the computer will decide whether to run the statements enclosed by "if" and "conditional judgment end", that is, statement 1 and statement 2 in the example. Only when the value of this expression is "true" will they be run, otherwise, they will be skipped.

We often encounter conditional judgment statements in programs, such as the following programs:

Send an email
If <Sending email is not successful> Then
Report fail message to user
End of If Judgement

As long as we correctly fill in the expression in the "if" line, so that it is true when "sending email failed" and false when "sending email succeeded", we can achieve our goal. On the contrary, usually the conditional judgment statement is not written well, which is also because the expression is not set well. In this example, usually the "send email" statement will assign a value to a variable to tell us whether the sending is successful. We just need to put this variable into the expression on the "if" line to work.

Loop

The forms of loop statements and conditional judgment statements are similar, usually:

Loop when <expression>
Statement1
Statement2
End of Loop

Similar to conditional judgment statements, when running to "loop when XXX", the value of the expression will be calculated first. If the value of the expression is "false", the loop will be skipped and the statements after "loop end" will be run directly. However, the biggest difference from the conditional judgment statement is that if the value of the expression is "true", it will jump back to the line "loop when XXX" again after running the following statements 1 and 2 to judge the value of the expression again.

In this way, you can use loop statements to let the computer do repetitive work. Such as the following procedure:

Send an email
Loop when <Sending email is not successful>
Retry to send the email
End of Loop

When the email is not sent successfully, the program will try again and again until the email is sent successfully. Of course, similar to conditional judgment statements, the most important thing in loop statements is how to correctly Settings this expression. If the Settings is not good and the expression is always "false", it is possible that the program will loop here all the time, and will not run further or end. This situation is called "dead loop".