Professional Documents
Culture Documents
Corporative education
ADVPL Programming
Page 2
Data processing
Programming language
Variables
Local Variables
Static Variables
Private Variables
5
Public Variables
Common Operators
Mathematical Operators
String Operators
Relational Operators
Logical Operators
Assignment Operators
Simple Assignment
Online Assignment
Compound assignment
Special Operators
10
11
Change of Precedence
11
Expressions
12
Instructions
12
12
Database
13
Control structures
13
Example 1
13
Example 2
14
Example
14
Example 1
15
Example 2
15
Example 3
15
Indentation
16
Functions
16
Example 4
16
Drawing Screens
17
summary
Page 3
Data processing
The problems that will be solved through programming are based on the basic concept of
a response at the end. The first analysis that we must do to find the solution is to identify
which
information is really necessary (input data) and what are the answers that the solution asks for
(output data).
Note: All program, regardless of its purpose, it works that way. That is: entrance,
The first work of the programmer is, based on the proposed problem, to divide the
information that will be
manipulated by the program in input and output information and to determine in general
terms which
will be the procedures to arrive at the solution of the problem. The definition of these
procedures is what
Algorithm, and processing is the execution of these procedures, which transform and
Among the forms of algorithm representation, we can cite the conventional flowchart.
Programming language
For a program to be developed, it is necessary for the programmer to use "one program",
which program?
Low level language. In this category, the programmer works with "complex" codes,
"Machine language".
High level language. In this category, the programmer works with "simpler"
When the programmer types his programs, we can call the same program source.
Note: Only the programmer, analyst has access to the source program. Except for exceptions.
The end user will have access to the executable program. That is, the features of the system
itself.
Page 4
Variables
Variables are "placeholders in the computer's memory to store data during the
When declaring, "create" the variable, it is necessary to define, what will be the data types
that the same
will manipulate.
It is also necessary to inform if the variable will be: local, private, public, static or global.
The variables declared in a program or function are visible according to the scope where they
are
defined. As also the scope depends on the lifetime of the variables. The scope definition
Location number: = 10
This line of code declares a variable called nNumber indicating that its scope is local.
LOCAL
STATIC
PRIVATE
PUBLIC
Local Variables
Local variables are pertaining only to the scope of the function where they were declared.
Should be
Function Father ()
<commands>
Daughter()
<more commands>
Return (.T.)
In this example, the variable nVar was declared as local and assigned a value of 10. When the
Daughter function
is executed, nVar still exists but can not be accessed. When the execution of the Parent
function ends, the
nVar variable is destroyed. Any variable with the same name in the program that called the
function Father
is not affected.
Local variables are created automatically each time the function where they are declared is
enabled.
They continue to exist and maintain their value until the end of the function activation (that is,
until the function
return the control to the code that executed it). If a function is called recursively (for example,
calls itself), each call in recursion creates a new set of local variables.
Page 5
The visibility of local variables is identical to the scope of your declaration. That is, the variable
is visible in
anywhere in the source code in which it was declared. If a function is called recursively, only
the local variables created in the most recent activation are visible.
Static Variables
Static variables work basically like local variables, but retain their value through
execution. Static variables must be explicitly declared in the code with the STATIC identifier.
The scope of static variables depends on where they are declared. If they are declared inside
the body
of a function or procedure, its scope will be limited to that routine. If they are declared outside
the
In this example, the variable nVar is declared static and initialized with the value 10:
Function Father ()
Static nVar: = 10
<commands>
Daughter()
<more commands>
.Return (.T.)
When the Daughter function is executed, nVar still exists but can not be accessed. Different
from variables
declared as LOCAL or PRIVATE, nVar continues to exist and maintains its current value when
running
of the Father function ends. However, it can only be accessed by subsequent executions of the
function
Father.
Private Variables
The statement is optional for private variables. But they can be explicitly stated
PRIVATE identifier.
Once created, a private variable still exists and will dim its value until the program or
function where it was created ends (that is, until the function where it was created returns to
the code the
In simpler terms, a private variable is visible inside the creation function and all functions
calls, unless a called function creates its own private variable with
name.
For example:
Function Father ()
Private nVar: = 10
<commands>
Daughter()
<more commands>
Return (.T.)
In this example, the variable nVar is created private and initialized with a value of 10. When
the Daughter function
is executed, nVar still exists and, unlike a local variable, can be accessed by the Daughter
function.
Page 6
When the Father function is finished, nVar will be destroyed and any previous nVar statement
will become
accessible again.
Public Variables
You can dynamically create public variables in the code with the PUBLIC identifier. The
variables
continue to exist and maintain their value until the end of implementation.
You can create a private variable with the same name as an existing public variable. However,
you can not create a public variable with the same name as an existing private variable.
Once created, a public variable is visible throughout the program where it was declared until it
is
hidden by a private variable created with the same name. The newly created private variable
hides the
existing public variable, and it will become inaccessible until the new private variable is
destroyed. Per
example:
Function Father ()
Public nVar: = 10
<commands>
Daughter()
<more commands>
Return (.T.)
In this example, nVar is created as public and initialized with a value of 10. When the Daughter
function is executed,
nVar still exists and can be accessed. Unlike local or private variables, nVar still exists after the
without being initialized, the assumed value is false (.F.) and not nil (nil).
Often a variable can have the same name as a field in a file or table opened in the
time. In this case, AdvPl will privilege the field. So a reference to a name that identifies
both a variable and a field, will result in the contents of the field.
To specify what the referenced element should be, you must use the
This command line identifies that the value assigned to the variable cRes must be the value of
the variable of
called NAME.
In this case, the value assigned to the cRes variable will be the value of the NAME field in the
file or table
Page 7
The FIELD identifier can be replaced by the last name of an open file or table, to avoid
need to select the area before accessing the contents of finished field.
Common Operators
In the documentation on variables there is a brief demonstration of how to assign values to a
variable
in the simplest way. AdvPl significantly extends the use of variables through the use of
expressions and functions. An expression is a set of operators and operating whose result can
be
In this example some expressions are used to calculate the net salary after an increase. The
Function.
Mathematical Operators
Addition
Subtraction
Multiplication
Division
Exponentiation
** or ^
String Operators
Page 8
Relational Operators
The operators used in AdvPl for operations and relational assessments are:
Logical Operators
The operators used in AdvPl for operations and logical evaluations are:
Assignment Operators
The operators used in AdvPl for assigning values to memory variables are:
<
Comparison Minor
>
Major Comparison
Equal Comparison
==
characters)
(I.e.
>=
<> or # or
(I.e.
Different Comparison
.And.
It is logical
.Or.
Logical OR
.Not. or!
NOT logical
Simple Assignment
:=
Online Assignment
+=
-=
*=
/=
** = or ^ =
%=
Simple Assignment
nVariable = 10
Page 9
Online Assignment
The online assignment operator is characterized by two points and the equal sign. It has the
same
function of the equality signal alone, but applies the assignment to the variables. With it can
be attributed
When several variables are initialized on the same line, the assignment starts from the right to
the
left, that is, nVar3 receives the value zero initially, nVar2 receives the contents of nVar3 and
nVar1
With the on-line assignment operator, you can override the individual initializations for each
variable
by a startup only:
per
The online assignment operator can also be used to replace field values in a
database.
Compound assignment
Compound assignment operators are a feature of the AdvPl language for calculation
expressions
Operator
Example
Equivalent to
+=
X+=Y
X=X+Y
-=
X-=Y
X = X-Y
*=
X*=Y
X=X*Y
/=
X/=Y
X=X/Y
** = or ^ =
X ** = Y
X = X ** Y
%=
X% = Y
X = X% Y
Page 10
10
(postfixed) variable name. Within an expression, the order of the operator is very important,
being able to change the result of the expression. The incremental operators are executed
from the left to
Location nA: = 10
Location nB: = nA ++ + nA
The value of variable nB results in 21, since the first reference to nA (before ++) contained the
value 10 that
was considered and immediately increased by 1. In the second reference to nA, it already had
the value
11. What was done was the sum of 10 plus 11, equal to 21. The final result after the execution
of these two
However:
Location nA: = 10
Location nB: = ++ nA + nA
Results in 22, as the incremental operator increased the value of the first nA before its value
was
considered.
Memory Variables
Control Structures
Special Operators
In addition to the common operators, AdvPl has some other operators or identifiers. These are
your
purposes:
()
Grouping or Function
[]
Matrix Element
{}
&
Macrosubstitution
||
Parentheses are used to group elements into an expression by changing the order of
precedence
of the expression evaluation (according to mathematical rules for example). They also serve to
wrap
an array. For example, A [3,2], refers to the element of matrix A in row 3, column 2.
Keys are used to specify literal arrays or blocks of code. For example,
The -> symbol identifies a field of a file by differentiating it from a variable. For example, FUNC-
> name refers to the FUNC file name field. Even if there is a variable named name, it is the
Page 11
11
The & symbol identifies an expression evaluation through macro and is seen in detail in the
documentation
on macrosubstitution.
The @ symbol is used to indicate that during the transition from a variable to a function or
The symbol || is used to indicate that during the transition from a variable to a function or
Depending on the type of operator, there is an order of precedence for the evaluation of the
operands. In
principle, all operations with operators, are performed from left to right if they have
The order of precedence, or execution priority level, of the operators in AdvPl is:
String Operators
Mathematical Operators
Relational Operators
Logical Operators
Assignment Operators
In complex expressions with different types of operators, the evaluation will follow this
sequence. Case
there is more than one operator of the same type (ie of the same level), the evaluation is from
left to
Exponentiation
The result of this expression is 30, since the exponentiation 2 ^ 3 (= 8) is first calculated, then
calculated the multiplications and divisions 10/2 (= 5) and 5 * 3 (= 15), and finally the additions
resulting in
2 + 5 + 15 + 8 (= 30).
Change of Precedence
The use of parentheses within an expression changes the order of precedence of the
operators.
Operating in parentheses are analyzed before those outside the parentheses. If there are
more than one set of non-nested parentheses, the leftmost group will be evaluated first and
and so on.
Page 12
12
In the above example, the exponentiation 2 ^ 3 (= 8) will be calculated first. Then 2 + 10 (= 12)
will be
calculated, 2 + 5 (= 7) calculated, and finally the division and multiplication will be effected,
which results in
12/7 * 3 + 8 (= 13.14).
If there are several nested parentheses, that is, placed inside one another, the evaluation will
occur
Expressions
The term expression means a given or a combination of two or more data of any kind. Let's go
Character type: Formed by any set of characters contained in ASCII table, always
math operations.
Logical type: There are only two possible data: true (. .T) or false (. .F), always interspersed
with
Score.
Type date: accepted only consistent dates, ie, the date is checked for the duration of the
month (28, 29,
Instructions
Instruction is everything that is ordered to the system, and it is expected that it will perform
immediately
an operation.
The instructions are divided into two modes: command and function.
The concepts below are very important for understanding the differences between
two.
Function: is a statement that can receive one or more arguments. Performs a processing and
returns an expression.
Page 13
13
Database
organized. For example. Imagine what a building materials company wants to have, the
portfolio of
suppliers and products they provide, in an organized and easily accessible way to:
To solve this "problem", we could create a database with two tables. One for
store information only from suppliers, such as: Name, address, contact,
Date of product validity, product name (description), unit, unit price, etc.
The following would create a relationship between the tables and this way, your I need to
know which
products a supplier provides me, with the database would be much simpler and faster.
Note: When the programmer creates a program, the same need to "bind" the same, a
Control structures
that are controlled: how many times a given routine should be executed, which path the
program should follow, following conditions established by the program and etc ....
If
Endif
Whenever If is used, EndIf must be used. It is possible to have several If within the other.
However, do not forget, if you open three If, you should use EndIf three times.
Note: Create and use a configuration (environment) in the Go and also do not forget to open
or create
a project in it.
Example 1
#Include "Rwmake.ch"
User Function ExercIf ()
nN1: = 3
nN2: = 8
If nN1> nN2
Page 14
14
MsgAlert ("The number" + AllTrim (Str (nN1) + "and greater than" + AllTrim (Str (nN2)))
Else
MsgAlert ("The number" + AllTrim (Str (nN2) + "is greater than" + AllTrim (Str (nN1)))
EndIf
Return ()
Example 2
Using Elseif
cEst = "MA"
If cEst = "RJ"
Else
Endif
Return ()
While / EndDo
While
The code that is below the While will run as long as the condition within the
true.
Example
Local nCnt: = 1
Location aX
For
The Code that is inside the For will be executed by a determined number of times.
Page 15
15
Example 1
nI = 0
For nI: = 1 to 5
MsgAlert (nI)
Next
Return ()
Note: For In the structure, when the flow is executed for the second time, the initial variable,
which in
our example is nI, two, three, three, and so on do not need to use nI ++.
However, if it is necessary that the increment is not one at a time Step can be used. Step can
be
Example 2
nI = 0
For nI: = 1 to 5 Step 2
MsgAlert (nI)
Next
Return ()
Example 3
nI = 0
MsgAlert (nI)
Next
Return ()
Page 16
16
Example 4
nI = 0
MsgAlert (nI)
Next
Return ()
Indentation
Every program must be very well documented. In this way, it facilitates the understanding for
It is also very important that the program is indented. This way, it is much easier to analyze
the same.
If nN1> nN2
MsgAlert ("The number" + AllTrim (Str (nN1) + "and greater than" + AllTrim (Str (nN2)))
Else
MsgAlert ("The number" + AllTrim (Str (nN2) + "is greater than" + AllTrim (Str (nN1)))
EndIf
If nN1> nN2
MsgAlert ("The number" + AllTrim (Str (nN1) + "and greater than" + AllTrim (Str (nN2)))
Else
MsgAlert ("The number" + AllTrim (Str (nN2) + "is greater than" + AllTrim (Str (nN1)))
EndIf
Functions
Some functions
SQRT - Calculate the square root of the number or reported as variable parameter.
Page 17
17
Drawing Screens
Example
Defines MSDialog oDlg Title "Product Query" From 0,0 To 200,500 Pixel
@ 050,010 SAY "PRODUCT CODE" // "Label that will appear on the screen
@ oDlg: nHeight / 2-30, oDlg: nClientWidth / 2-70 Button oBtnOk Prompt "& Ok" Size 30.15
Pixel Action
@ oDlg: nHeight / 2-30, oDlg: nClientWidth / 2-35 Button oBtnCancel Prompt "& Cancel" Size
30.15 Pixel
Enter this code in the ide. Compile and execute the same.
Note: First, you must set the screen with the Define MsDialog.
When all the elements of the window are the same, it is necessary to use the ActiveDate
MsDialog, which
the screen.
Note: One of the great mistakes of professionals as a whole, not only the technology is: think
knows a lot and ends up neglecting the procedures that caution tells to follow.
For example, it is common to professional sit at the computer and start typing without
stopping. IT IS
We can not forget that every computer program, regardless of: purpose, price
processing will be poor and reports will only show this to us.
For these reasons, I recommend that before we start typing, we will work with the
algorithm.
Page 18
18
Exercises
Exercise 02 - Develop a function that calculates the square root of the number 49.
Exercise 03 - Now, use the previous function as a base, and calculate the square root of the
number entered
by the user.
Exercise 05 - Develop a function that calculates the multiplication table of the desired number
(informed).
Exercise 06 - now develop four programs: inclusion, consultation, Change and Delete.
The main objective of these exercises is to put into practice what was presented in theory, and
Annotations