You are on page 1of 25

Page 1

Corporative education

Logic and Startup

ADVPL Programming

All rights reserved.

Budget Planning and Control

Matrix - Av. Braz Leme, 1.717 - 02511-000 - So Paulo - SP - Brazil.

Tel .: 55 (11) 3981 - 7001 www.microsiga.com.br

Page 2

All rights reserved.

Logic and Boot to ADVPL Programming

Data processing

Input, processing (algorithm) and output

Programming language

Source and executable program

Variables

The Context of Variables within a Program

Local Variables

Static Variables

Private Variables
5

Variables and field names

Public Variables

Common Operators

Mathematical Operators

String Operators

Relational Operators

Logical Operators

Assignment Operators

Simple Assignment

Online Assignment

Compound assignment

Increase / Decrement Operators

Special Operators

10

Operator Precedence Order

11

Change of Precedence

11
Expressions

12

Instructions

12

Differences between Command and Function

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

Initiation of Programs in ADVPL (Algorithm)


17

summary

Page 3

All rights reserved.

Logic and Boot to ADVPL Programming

Data processing

The problems that will be solved through programming are based on the basic concept of

Data processing. The information is received by the program and processed,

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).

Input, processing (algorithm) and output

Note: All program, regardless of its purpose, it works that way. That is: entrance,

processing and output.

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

manipulate the input data, resulting in the output data.

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?

A programming language. Programming language is nothing more than a program that

allows the programmer to create programs.

There are two categories of programming language:

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"

codes are in English, meaning it is much easier to understand.

Examples of programming language: C, C ++, Visual basic, Advpl and etc.

Source and executable program

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

All rights reserved.

Logic and Boot to ADVPL Programming

Variables

Variables are "placeholders in the computer's memory to store data during the

processing of the program ".

When declaring, "create" the variable, it is necessary to define, what will be the data types
that the same

will manipulate.

For example: numeric, character (string, alphanumeric), logical or date.

It is also necessary to inform if the variable will be: local, private, public, static or global.

The Context of Variables within a Program

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

of a variable is made at the time of its declaration.

Location number: = 10

This line of code declares a variable called nNumber indicating that its scope is local.

The scope identifiers are:

LOCAL

STATIC
PRIVATE

PUBLIC

Local Variables

Local variables are pertaining only to the scope of the function where they were declared.
Should be

explicitly declared with LOCAL identifier, as in the example:

Function Father ()

Local nVar: = 10, aMatrix: = {0,1,2,3}

<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

All rights reserved.

Logic and Boot to ADVPL Programming

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

body of any routine, its scope is any program file.

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

performed). At this point it is automatically destroyed.

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

All rights reserved.

Logic and Boot to ADVPL Programming

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

to the execution of the Father function.

Unlike other scope identifiers, when a variable is declared public

without being initialized, the assumed value is false (.F.) and not nil (nil).

Variables and field names

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

nickname (->) and one of two reference identifiers, MEMVAR or FIELD.

cRes: = MEMBER-> NAME

This command line identifies that the value assigned to the variable cRes must be the value of
the variable of

called NAME.

cRes: = FIELD-> NAME

In this case, the value assigned to the cRes variable will be the value of the NAME field in the
file or table

open in the current area.

Page 7

All rights reserved.

Logic and Boot to ADVPL Programming

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.

cRes: = CLIENTS-> NAME

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

assigned to a variable or analyzed for decision making. For example:

Location n Salary: = 1000, nDiscount: = 0.10

Local nInput, nSalLiquido

nIncrease: = nSalary * 1.20

nSalLiquido: = nAmount * (1-nDiscount)

In this example some expressions are used to calculate the net salary after an increase. The

operand of an expression can be a variable, a constant, a file field or a

Function.

Mathematical Operators

The operators used in AdvPl for mathematical calculations are:

Addition

Subtraction

Multiplication

Division

Exponentiation

Module (Rest of Division)

** or ^

String Operators

The operators used in AdvPl for character handling are:

+ Concatenation of strings (union)

Concatenation of strings with elimination of the final whites of the strings


intermediary

$ Substrings Comparison (contained in)

Page 8

All rights reserved.

Logic and Boot to ADVPL Programming

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

==

Exactly the same comparison (for

characters)

(I.e.

Comparison Minor or Equal

>=

Greater or Equal Comparison

<> or # or

(I.e.

Different Comparison

.And.

It is logical
.Or.

Logical OR

.Not. or!

NOT logical

Simple Assignment

:=

Online Assignment

+=

Addition and Online Assignment

-=

Online Subtraction and Assignment

*=

Multiplication and Online Assignment

/=

Online Division and Assignment

** = or ^ =

Online Exponentiation and Assignment

%=

Module (remainder of the division) and Online Assignment

Simple Assignment

The equality sign is used to assign value to a memory variable.

nVariable = 10

Page 9

All rights reserved.

Logic and Boot to ADVPL Programming

Increase / Decrement Operators

The AdvPl language has operators to perform increment or decrement of variables. It is


understood
by increment increase the value of a numerical variable by 1 and it is understood by
decrement decrease

the value of the variable in 1. The operators are:

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

more than one variable at a time.

nVar1: = nVar2: = nVar3: = 0

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

receives the contents of nVar2 by the end.

With the on-line assignment operator, you can override the individual initializations for each
variable

by a startup only:

Local nVar1: = 0, nVar2: = 0, nVar3: = 0

per

Location nVar1: = nVar2: = nVar3: = 0

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

and assignment. With them you can save typing:

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

++ Post or Fixed Increase

Post or Pre-Fixed Decrease

Page 10

10

All rights reserved.

Logic and Boot to ADVPL Programming

Decrement / increment operators can be placed either before (pre-set) or after

(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

right inside an expression.

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

lines is the variable nB containing 21 and the variable nA containing 11.

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

{}

Definition of Matrix, Constant, or Code Block

-> Nickname Identifier

&

Macrosubstitution

Passing of parameters by reference

||

Parameter passing by value

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

the arguments of a function. Brackets are used to specify a specific element of

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,

A: = {10,20,30} creates a matrix called A with three elements.

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

field name that will be accessed.

Page 11

11

All rights reserved.

Logic and Boot to ADVPL Programming

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

procedure is taken as a reference and not as a value.

The symbol || is used to indicate that during the transition from a variable to a function or

procedure is taken as a value and not as a reference.

Operator Precedence Order

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 same level of priority.

The order of precedence, or execution priority level, of the operators in AdvPl is:

Preset Increment / Decrement Operators

String Operators

Mathematical Operators

Relational Operators

Logical Operators
Assignment Operators

Post-Fixed Increment / Decrement 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

right. For mathematical operators however, there is a precedence to follow:

Exponentiation

Multiplication and Division

Addition and Subtraction

Consider the example:

Location nResult: = 2 + 10/2 + 5 * 3 + 2 ^ 3

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

All rights reserved.

Logic and Boot to ADVPL Programming

Location nResult: = (2 + 10) / (2 + 5) * 3 + 2 ^ 3

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

parentheses more intero toward the outermost.

Expressions

The term expression means a given or a combination of two or more data of any kind. Let's go

four types of data:

Character type: Formed by any set of characters contained in ASCII table, always

represented in quotation marks.

Numeric type: Comprised of negative values, positive or zero, specifically used to

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,

30 or 31) and the existence of the month (1 to 12).

Type memo: Supports up to 64 KB of text.

Instructions

Instruction is everything that is ordered to the system, and it is expected that it will perform
immediately

an operation.

Differences between Command and Function

The instructions are divided into two modes: command and function.

The concepts below are very important for understanding the differences between

two.

Command: is an instruction that performs a task;

Function: is a statement that can receive one or more arguments. Performs a processing and

returns an expression.

Page 13

13

All rights reserved.


Logic and Boot to ADVPL Programming

Database

We can say that the database is a set of information

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:

inclusions, consultations, changes; and exclusions.

To solve this "problem", we could create a database with two tables. One for

store information only from suppliers, such as: Name, address, contact,

supplier and etc ...

The other table would be to store product information such as:

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

database, so that the data generated or reported by the program is stored.

Control structures

Control structures are fundamental in any programming language. It is through

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 ....

The main ones are: If, While, For, etc ...

If

Else (If not)

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

All rights reserved.

Logic and Boot to ADVPL Programming

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

User Function ExercIf2 ()

cEst = "MA"

If cEst = "RJ"

Alert ("RJ State, Icms of 3%")

Elseif (cEst = "SP")

Alert ("State SP, Icms of 5%")

Elseif (cEst = "MG")

Alert ("State MG, Icms of 6%")

Elseif (cEst = "MA")

Alert ("MA State, 2% Icms")

Else

Alert ("Invalid State")

Endif

Return ()

While / EndDo
While

The code that is below the While will run as long as the condition within the

true.

Example

User Function TesteWhile ()

Local nCnt: = 1

Location aX

While nCnt <= 10

For

For (To) Next

The Code that is inside the For will be executed by a determined number of times.

Page 15

15

All rights reserved.

Logic and Boot to ADVPL Programming

Example 1

User Function TFor ()

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

used both to increment the counter and vice versa.

Example 2

User Function TFor2 ()

nI = 0
For nI: = 1 to 5 Step 2

MsgAlert (nI)

Next

Return ()

Example 3

User Function TFor2 ()

nI = 0

For nI: = 1 to 5 Step 2

MsgAlert (nI)

Next

Return ()

Page 16

16

All rights reserved.

Logic and Boot to ADVPL Programming

Example 4

User Function TFor3 ()

nI = 0

For nI: = 5 to 1 Step -2

MsgAlert (nI)

Next

Return ()

Indentation

Every program must be very well documented. In this way, it facilitates the understanding for

analyze the code and even make adjustments to it.

It is also very important that the program is indented. This way, it is much easier to analyze

the same.

See this example without indentation and then with.

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

SRT - Convert number in character.

SQRT - Calculate the square root of the number or reported as variable parameter.

ALLTRIM - Shoot all the blanks.

Page 17

17

All rights reserved.

Logic and Boot to ADVPL Programming

Drawing Screens

Example

Defines MSDialog oDlg Title "Product Query" From 0,0 To 200,500 Pixel

@ 020,192 SAY TIME ()

@ 030,130 SAY "MICROSIGA"

@ 040,100 SAY "PRODUCT CONSULTATION"

@ 050,010 SAY "PRODUCT CODE" // "Label that will appear on the screen

@ 050.080 GET cCODIGO PICTURE "@!" VALID ValCodProd ()

@ oDlg: nHeight / 2-30, oDlg: nClientWidth / 2-70 Button oBtnOk Prompt "& Ok" Size 30.15
Pixel Action

Confirm () Message "Click here to Confirm" Of oDlg

@ oDlg: nHeight / 2-30, oDlg: nClientWidth / 2-35 Button oBtnCancel Prompt "& Cancel" Size
30.15 Pixel

Action oDlg: End () Cancel Message "Click here to cancel" Of oDlg


Activate MSDialog oDlg Centered

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.

Initiation of Programs in ADVPL (Algorithm)

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

Of course I'm not generalizing.

However, most forget the principal, which is the work of analysis.

We can not forget that every computer program, regardless of: purpose, price

and function work with the famous EP S.

Input, Processing, and Output. If the input data is not

processing will be poor and reports will only show this to us.

Therefore, the work of analysis is fundamental.

For these reasons, I recommend that before we start typing, we will work with the

algorithm.

Use what we call Portuguese to solve these exercises.

Only after the Advpl.

Page 18

18

All rights reserved.

Logic and Boot to ADVPL Programming

Exercises

Exercise 01 - Create a function to solve the following problem.

Develop a function that calculates the average of three notes.

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 04 - Develop a function that calculates the multiplication table number 2.

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

"Feed" the SZ6. Do not forget to use algorithms.

Annotations

Registration number: XBAP10270707

You might also like