PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
New WINDEV Mobile 25 feature!
  • This lesson will teach you the following concepts
  • The variables
  • What is a variable?
  • Declaring and assigning a variable
  • The types of variables
  • The scope of variables
  • Simple operations on the variables
  • Tips
  • The conditional IF and SWITCH statements
  • The IF statement
  • The SWITCH statement
  • The loops
  • The FOR statement
  • The LOOP statement
  • The WHILE statement
  • The procedures
  • Types of procedures
  • Creating and calling a procedure
  • Parameters of a procedure
  • Result of a procedure
Lesson 4.2. The WLanguage basics
This lesson will teach you the following concepts
  • The different types of variables.
  • Main WLanguage statements.
  • The main WLanguage operators.
  • Procedures and functions.
  • Processing strings, numeric values and currencies.
Lesson duration

Estimated time: 1 hour
Previous LessonTable of contentsNext Lesson
The variables

What is a variable?

In a programming language, a variable is used to store data. These memory sections contain strings, numbers, etc.
The variables are used to perform calculations, to perform comparisons or to store information that will be used later.
Declaring and initializing a variable
A variable is represented by:
  • a name: Name given to the variable so that it can be used by the language.
  • a type: Nature of data stored in the variable (see The types of variables).
  • a value: Information stored in the variable.
  • a scope: Limit for using the variable in the program (see The scope of variables). The scope is mainly defined by the location where the variable is declared.

Declaring and assigning a variable

The variable must be declared (which means created) before it can be used.
  • Example of simple declaration:
    Price is currency
    • Price represents the variable name.
    • is is used to declare the variable. The everyday language is used in WLanguage.
    • currency corresponds to the variable type.
  • Example of multiple declaration:
    LastName, FirstName are strings
    • LastName, FirstName represent the names of variables.
    • are is used to declare a set of variables.
    • strings represents the type of variables.
When the variable is declared, you have the ability to assign it (or to give it a value). For example:
// Assign a currency variable
Price = 1256.67
// Assign a string variable
LastName = "Doe"
The = operator is used to perform this assignment.

Remark

In WLanguage, the " character (double quote) is the character used to delimit a character string. In the above example, the doubles quotes are used to assign the Doe value to the LastName variable.
The variable content can be read and handled: all you have to do is use the name given to the variable to access it.
The following example is used to read and display the content of Price variable on the screen:
Info(Price)

The types of variables

The variable type is used to specify the kind of information that will be stored in the variable. The most common types are:
  • boolean (True or False),
  • string ("Doe"),
  • integer (1234),
  • currency (12,32),
  • real (7,766666),
  • etc.

Important

Use the type corresponding to the information that must be stored. Therefore, you will optimize memory and avoid calculation or process errors when using variables in WLanguage functions.
Most of these types of variables will be used in this tutorial.
For more details, see the help about the desired type (see The different types of variables).

Remark

Other types are available, such as arrays, structures, dates, times, ...
Advanced variables are also available. These advanced types gather all the characteristics of the element being used in a single variable.
Advanced types can be used to handle XML documents, emails, XLS files, ...

The scope of variables

The variables can be declared anywhere in the code. However, according to the position of its declaration, the variable cannot be used to perform processes or calculations. We talk of variable scope.
Two types of scope are available:
  • Global.
  • Local.
Global scope
Global means that the variable has an extended visibility in the code. The variable is visible outside the location where it was declared. Several levels are available:
  • Project and Set of procedures,
  • Window, Mobile Window, Page, Report.
A variable declared at project level has the greatest visibility in the program. The variable is visible anywhere, in all program processes. However, you should not declare too many variables with this scope: indeed, the memory occupied by the variable is always reserved even if the variable is not used. Using a large number of global variables is not recommended in the program architecture. To pass variables to a process, we recommend that you use parameters (see "Parameters of a procedure" for more details).
A variable declared in a set of procedures and a variable declared in the project have the same visibility. The advantage of declaring a variable at the level of a Set is to group (or classify) the variables by theme (set) in order to make the project initialization process more readable.
A variable declared at Window, Mobile Window, Page or Report level limits the scope of the variable to all the processes of the element (Window, Mobile Window, Page or Report) and its controls. This makes it possible to encapsulate and limit the uses.
Local scope
Local means that the variable has a limited visibility in the code. The variable is visible in the process where it was declared. This makes it possible to restrict the use of variable to the process.
Summary scope diagram
Scope of variables

A variable is global when it is declared:
  • in the initialization code of project (or in the declaration code of set of procedures). The variable is global to the project.
  • In the declaration code of global variables of window, page or report. The variable is global to the element (window, page or report) where it was declared.
In all other cases, a variable is local to the process where it is declared.

Simple operations on the variables

Several mathematical operators can be used to perform calculations on variables:
  • + to perform an addition.
  • - to perform a subtraction.
  • * to perform a multiplication.
  • / to perform a division.
Other operators can be used to perform calculations:
  • ++ to increment from 1 (add 1 to the variable).
  • - - to decrement from 1 (subtract 1 from the variable).
  • += to assign by adding a value.
  • - = to assign by subtracting a value.
Examples:
// Declaration of variables
Cnt is int
V1 is int
Res is numeric
 
// Assignment
Cnt = 10
V1 = 3
 
// Use of operators
Cnt = Cnt + 3   // Cnt is equal to 13
Cnt ++ // Cnt is equal to 14
Cnt -= 8 // Cnt is equal to 6
Cnt = Cnt * V1 // Cnt is equal to 18
Res = Cnt / 5 // Res is equal to 3.6
Comparison operators are also available:
  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to
  • <> different from
  • = equal to
Other operators are available. See The different types of operators for more details.

Tips

  • It is very convenient to name the variables with long names (and to avoid short names such as i, j, k, ...). When reading the program again, you will be able to easily remember the variable purpose.
  • To define the name of variables, all Unicode characters (including the accented characters) are accepted. Meaning improved readability! Caution: some characters are not allowed: space, =, dot, comma, ...
  • It is very important to give the proper type to the variable according to its use. For example, to store several digits, you may have to:
    • use a numeric variable if this variable must be used for calculations.
    • use a string variable if this variable must be used to store digits without performing calculations (to store the social security number for example).
The conditional IF and SWITCH statements

The IF statement

This statement is used to run an action or another one according to the result of an expression. If the expression is checked, a process is run ; if the expression is not checked, another process can be started.
The IF statement can be used as follows:
IF <Expression to check> THEN
  Process to run if the expression is checked
ELSE
  Process to run otherwise
END
Code example: The following code selects a number at random and displays a message according to the value.
Tot is currency
// Selects a number at random between 100 and 4000
Tot = Random(100, 4000)
IF Tot>2000 THEN
Info("The amount is greater than 2000")
ELSE
Info("The amount is less than or equal to 2000")
END

In this case, the expression to check corresponds to "Tot>2000".
Remark: Several code lines can be run during the process corresponding to a condition. In this case, the following syntax must be used:
IF <Expression to check> THEN
Code line 1
Code line N
ELSE
Code line 1
Code line N
END

The SWITCH statement

This statement is used to evaluate an expression and to run a process for each possible expression value.
The SWITCH statement is used according to the syntax below:
SWITCH <Expression>
CASE Value 1:
Process 1...
CASE Value 2:
Process 2...
...
CASE Value N:
Process N...

OTHER CASE
Process ...
END
Example: The following code retrieves today's date and displays a different message according to its value. A specific message is displayed for the 1st and for the 15th of the month. In the other cases, today's date is displayed.
D is Date
D = Today()
SWITCH D..Day // Checks the day of the date
CASE 1: Info("We are the first day of the month")
CASE 15: Info("We are the 15th of the month")
OTHER CASE: Info("We are the: " + DateToString(D))
END
Remarks:
  • If the code line "CASE 1:..." is run, the other code lines corresponding to the possible values are not run.
  • Several values can be grouped in the same case. The different values are separated by a comma. For example:
    Sub is int = 2
    SWITCH Sub
           CASE 1,2: Info("Case 1 or 2")
           CASE 3: Info("Case 3")        
           OTHER CASE: Info("Other case")        
    END
  • Several code lines can be run during the process corresponding to a condition. In this case, the following syntax must be used:
    SWITCH <Expression>
    CASE Value 1:
    Process 1 - Code line 1...
    Process 1 - Code line 2...
    CASE Value N:
    Process N - Code line 1...
    Process N - Code line 2...
    END
The loops
The loop statements are used to run a process in a recurring way. A specific loop statement is used according to the number of occurrences. Several statements can be used to perform loops:
  • FOR...
  • LOOP...
  • WHILE...

The FOR statement

The FOR statement is used when the number of occurrences to process is known. This statement is used to manage the number of occurrences via a variable in which the passages performed in the loop will be counted.
The syntax of FOR statement is as follows:
FOR Subscript = Start Value TO End Value
Process to run
END
For example, the following code runs the process 2000 times:
FOR Cnt = 1 TO 2000
// Process to run
END
Remark: An increment step of subscript can be defined via the STEP keyword. For example, the following code runs the process 2000 times and the Cnt variable decreases by 10:
FOR Cnt = 2000 TO 1 STEP -10
// Process to run
END

The LOOP statement

The LOOP statement is used to perform loops when the number of occurrences to process is unknown. In this case, a test must be used to exit from the loop.
The syntax of LOOP statement is as follows:
LOOP
Process to run
IF <Expression> THEN BREAK
END
For example:
Counter is int
Counter = 10
LOOP
// Process to run
Counter = Counter - 1
IF Counter = 0 THEN BREAK
END

Tip

The LOOP statement and the FOR statement can have the same behavior: all you have to do is use the syntax with exit according to the number of iterations:
LOOP (<Number of iterations>)
...
END
Example:
LOOP(10)
// Process to run
END

The WHILE statement

The WHILE statement and the LOOP statement operate according to the same principle. The difference is that the test of exit condition is performed BEFORE running the loop code. This test is used to compare a variable. This variable starts from a start value and it is modified in the loop until it reaches the value that triggers the exit from the loop.
The syntax of WHILE statement is as follows:
<Initialize the variable to its start value>
WHILE <Compare the variable to its end value>
Process to run
<Modify the variable>
END
For example:
Counter is int
Counter = 0
WHILE Counter<10
// Process to run
Counter = Counter + 1
END
The procedures
A procedure is used to associate an identifier with a code section in order to re-use it.
In this lesson, we are going to present the different types of procedures available in WLanguage, their creation mode, how to call them, pass parameters and retrieve a result.

Types of procedures

Three types of procedures are available:
  • Global procedure: can be used in all project processes (declared in a set of procedures).
  • Local procedure in a Window, Page or Mobile Window: can be used in all processes that depend on the object in which this procedure was declared.
  • Internal procedure in a process: can only be used in the process where it was declared.

Remark

Scope of procedures
The procedures comply with the scope rules presented for the variables (see "The scope of variables").

Creating and calling a procedure

To create a global procedure, you must:
  1. Create (if necessary) a set of procedures (via the "Project explorer" pane, "Procedures" folder). Give a name to the set of procedures.
  2. Create a global procedure in the set of procedures (via the "Project explorer" pane, "Procedures, Set name" folder). Give a name to the procedure.
  3. Type the code of global procedure. The procedure code has the following format:
    PROCEDURE <Name of global procedure>()
To create a local procedure, you must:
  1. Select the element associated with the procedure (window, page, etc.).
  2. Create a local procedure (via the "Project explorer" pane, expand the element name, "Local procedures" folder).
  3. Give a name to the procedure.
  4. Type the code of local procedure. The procedure code has the following format:
    PROCEDURE <Name of local procedure>()
To create an internal procedure, type the following code in the required process:
INTERNAL PROCEDURE <Procedure name>()
<Code of internal procedure>
END
To call a procedure, use the procedure name (with the possible parameters that will be passed to it).
<Procedure name>(<Parameter 1>, ..., <Parameter N>)
See Procedure/Function for more details.

Parameters of a procedure

What is a parameter?
A parameter is a value sent to a procedure during the call to the procedure.
The following example is used to call the Multiply10 procedure and to pass the value that will be handled in the procedure as parameter:
Multiply10(50)
You can pass 0 or several values as parameters to a procedure. These values can be of any type (as with variables).
The parameter is specified in the procedure declaration in the format of a variable. For example, for the Multiply10 procedure, the procedure code is:
PROCEDURE Multiply10(P)
P=P*10
P is the parameter expected by the procedure.

Remark

To specify the parameter role in the procedure, you have the ability to typecast the parameter in the procedure declaration.
For example, to use numeric values only, you have the ability to declare:
PROCEDURE Multiply10(P is numeric)
In the following example, the Multiplication procedure expects two Integer parameters and returns the multiplication result.
The procedure code is as follows:
PROCEDURE Multiplication(Nb1 is int, Nb2 is int)
MyResult is int
MyResult = Nb1 * Nb2
RESULT MyResult
The code used to call the procedure is as follows:
res is int
res = Multiplication(10, 50)
// Res is equal to 500
How to use the parameters?
By default, passing parameters in WLanguage is performed by reference (or by address). The parameter in the procedure represents (references) the variable passed during the call.
Therefore, when a procedure statement modifies the parameter value, the value of the variable corresponding to this parameter is modified.
Example:
  • The procedure code is as follows:
    PROCEDURE Test_address(P1)
    P1 = P1 * 2
  • The code used to call the procedure is as follows:
    T is int
    T = 12 // T is equal to 12 before the call
    Test_address(T)
    // T is equal to 24 after the call
To avoid modifying the value of the variable corresponding to the parameter, the parameters must be passed by value. Passing parameters by value allows you to handle a copy of parameter value. If the procedure code modifies the variable value, the value of the variable corresponding to the parameter is not modified.
To force a parameter to be passed by value to a procedure, the LOCAL keyword must be used in front of the parameter name in the procedure declaration. This keyword indicates that the following parameter will not be modified by the procedure.
Example:
  • The procedure code is as follows:
    PROCEDURE Test_value(LOCAL P1)
    // Local indicates that the parameter will be passed by value
    P1 = P1 * 2
  • The code used to call the procedure is as follows:
    T is int
    T = 12 // T is equal to 12
    Test_value(T)
    // T does not change

Remark

In the same procedure, some parameters can be passed by reference while other parameters can be passed by value. All you have to do is used the LOCAL keyword in front of each parameter passed by value.
Mandatory or optional parameters?
The parameters received in the procedure can be mandatory or optional parameters. A mandatory parameter must be filled during the call to the procedure while an optional parameter can be omitted: in this case, it will take the default value defined when declaring the procedure parameters.

Remark

When declaring a procedure, the optional parameters are the last parameters (they are always specified after all mandatory parameters).
In the following example, the Multiplication procedure uses an optional parameter, Nb2. This optional parameter is indicated after the mandatory parameters, by specifying its default value. In this example, the default value of optional parameter is set to 10.
PROCEDURE Multiplication(Nb1 is int, Nb2 is int=10)
MyResult is int
MyResult = Nb1 * Nb2
RESULT MyResult
The code used to call the procedure is as follows:
res is int
res = Multiplication(6)
// Res is equal to 60
In this example, the second parameter was not specified. Therefore, its default value will be used.

Result of a procedure

The procedures can return one or more results. The result can be typecasted. The RESULT keyword must be used to return a value.
See RESULT statement for more details.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 25
Comments
Click [Add] to post a comment