Skip to main content

Appendix: Programming Basics

Throughout this guide, we assume that you have some basic knowledge of computer programming, including data types, variables, and conditionals. If you have no prior experience, this chapter serves as a crash course to programming basics. Laiye RPA demands very little programming knowledge, and all the necessary concepts should be easy to understand. So don't worry! Take your time with this chapter, and you will be all set.

If you already have prior experiences in programming, feel free to skip this appendix.

Let us take a look at this Excel table. The content in this table is completely arbitrary, but we will use it to explain some important concepts.

**Figure 120: The example Excel table**

Data

The main job of RPA is usually to process various types of data. What exactly is data?Imagine that you have an Excel table, and its cells are already filled with various entries. These entries are all data. Data is the information exchanged between computers and humans.

Data can be classified into structured and unstructured data. The data nicely lined up in the gridded system of this Excel table is clearly structured, and most of the data we will have to deal with is all structured. Therefore, we will simply use this table to explain our concepts. On the other hand, images, audios, videos, and webpages are mostly unstructured data, so we will not go into details here.

Data Type

Generally speaking, in programming languages, data can be of many different types. Common data types in Laiye RPA include numeric types, string types, Boolean types, null type, and compound types. We will leave the compound types for next level.

A numeric type is just a number. It could be an integer or a number with a decimal point (known as a floating-point number, or simply a float). In the example Excel table, this could be the numbers under the "Amount" column or the "Sales" column.

A string type is usually a string of characters enclosed with a pair of double quotes ("example") or a pair of single quotes ('example') to indicate that this is a string. Special characters in a string are indicated using backslashes. For example, \t represents a tab character, \n represents a new line, \' represents a single quote, \" represents a double quote, and \ represents a backslash. You can also use three single quotes ('''example''') to enclose a long string. In a long string, you can directly enter new lines, write single quotation marks, and write double quotation marks, instead of using the backslash substitutes \n, \', or \".

A Boolean type can have one of two values: "True" or "False". This often corresponds colloquially to "Yes" and "No", and the meaning is the same. A null type always has the value Null (case-insensitive), representing an empty value.

How do we distinguish between the various data types? In general, we can add, subtract, multiply, and divide numeric values. Boolean values only support logical operations like "And, Or, and Not". For example, the "Customer Name" column in the example table makes clearly does not support these arithmetic or logical operations, and therefore it must be a string. Its values must be enclosed by quotation marks, like "John".

The values in the "Order Number" column are numbers and can technically support arithmetic operations. However, adding or subtracting order numbers makes no sense, so we can either process them as numbers or as strings, and it's up to the process developer to make this decision.

Variable

In the example Excel table, each data point is stored in a cell, and Excel has given a name for each cell. For example, the cell that has value 261.54 is called D2. The data in this cell could change, but the name of this cell remains the same: D2. As long as we read from cell D2, we will always get the latest value.

A "variable" is a common concept in programming. It is like a cell in an Excel table, capable of storing data. A variable has a name, and through this name we can access the data stored in the variable or assign some new data to store in the variable.

The cells in an Excel table are all named in the format "Letter + Number", but variable naming in programming languages is much more flexible. A legal variable name consists of English letters, numbers (except for the first character of a name), and underscores. Some programming languages, like Laiye RPA's Bot Script, also support characters from other languages (like Chinese). We recommend you give variables self-explanatory names that describe what they represent, instead of mysterious codes like D2. This helps us and others understand what the code is doing, though it has no bearing on how the program actually runs. Laiye RPA's variable names are case-insensitive.

Laiye RPA's variables are dynamically typed. This means that you do not need to declare the type of a variable when creating it, and the value and type of a variable can change during the runtime of a process. This is the same as other scripting languages like Python, Lua, and JavaScript.

The syntax for defining a variable is Dim variable name. You can optionally assign an initial value to a variable by using the syntax Dim variable name = initial value. You can also declare multiple variables with syntaxes like Dim variable name 1 = value 1, variable name 2 or Dim variable name 1 = value 1, variable name 2 = value 2. For example, Dim example = 1. Likewise, you can define constants (which are just variables whose values cannot change throughout the runtime of a process) with the syntax Const name 1 = value 1, name 2 = value 2.

Expression

You can create calculations involving multiple variables and fixed data, and this calculation is called an "expression". Since the value of a variable can change over time, the value of an expression can also change. After writing an expression, its values are only computed when the process reaches the line of code where the expression is located, and the computation uses the values of the variable when the process reaches the line of code.

For example, x + 2 is an expression. We cannot calculate the value of this expression if we do not know the value of x. If the process reaches x + 2 and finds that the value of x is 3, then x + 2 evaluates to 5.

Of course, some expressions have meaningless calculations. For example, dividing a string is meaningless, but when we are writing the expression, it is unclear what the value of each variable is, so we cannot immediately determine that this expression is problematic. We can only realize this mistake during runtime, and so Laiye RPA will throw runtime exceptions and exit from its process.

Conditional

When writing a program, we usually write one line of code after another, expecting the program to run each line one after another, from top to bottom. However, this is often not flexible enough. For example, we might want to determine whether a certain condition is true during runtime and then decide whether to execute a certain statement based on this condition. This is a conditional statement.

Conditionals are written differently in different programming languages, but the general form is the same

If <expression> then
Statement1
Statement2
End Conditional

When the program reaches the line "If", it evaluates the expression, which should yield a Boolean value. If the expression evaluates to a non-Boolean, it is often converted to a Boolean value. Based on the condition's value, the program decides whether to run the statements between the "If" line and the "End Conditional" line. In this example, this includes Statement1 and Statement2. Only if the value evaluates to true will the program execute the statements. Otherwise, it will skip through them.

We often encounter conditionals when writing programs. Take the following example:

Send email If the email is not sent successfully then Report the failure to users End Conditional

The conditional expression should evaluate to "True" when the email is not sent successfully, and it would cause our program to report the error. Oftentimes, when a conditional is behaving incorrectly, it is because the conditional expression is not evaluated correctly. In this example, the Command to send an email would typically assign to a variable to show whether the email is sent successfully, and we can simply use this variable as the conditional expression.

Loop

The syntax of a loop is similar to that of a conditional.

Loop when <expression>
Statement 1
Statement 2
End Loop

Similar to a conditional, a loop first evaluates the expression when the program reaches the line "Loop when \<expression\>". If the expression evaluates to "False", then the program will skip to the statement after "End Loop" without executing the lines in between. If the expression evaluates to "True", then it will first execute the enclosed lines (Statement 1 and Statement 2 in this example) and loop back to the line Loop when \<expression\>, repeating this process.

This allows us to use a loop to instruct our computer to complete repetitive tasks. For example,

Send email Loop when the email is not sent successfully Try to send email again End Loop

the above code tries to resend the email indefinitely until the email is sent successfully. Similar to a conditional, the most important part of a loop is setting up its expression. If we make a mistake and cause the condition to always evaluate to "False", then this results in an infinite loop that the program cannot get out of. You must manually interrupt the program in this case.