|
|
|
|
- Lesson 4 - Conditional statements
- Overview
- Practical example
- The IF statement
- The SWITCH statement
- To sum up
Tutorial - WLanguage basics
Lesson 4 - Conditional statements We will cover the following topics: - The IF statement.
- The SWITCH statement.
 20 mn WLanguage is a set of statements used to handle data. Conditional statements are used to compare variables and/or values in order to perform different processes. Several conditional statements are available: - IF... THEN... ELSE... END
- SWITCH...
Practical example To complete the exercises in this lesson, we will work on the "WLanguage" project that we used in the previous lesson. The principle remains the same: the example code will be entered directly in the "Initialization" event of the project. To view WLanguage events of the project: - Right-click the "P" button next to the open element tabs. The context menu appears.
- Select "Element code".
- The code editor displays the different events associated with the project.
This statement is used to execute one process or another depending on the result of an expression. If the expression evaluates to true, a process is executed. If it evaluates to false, another process can be executed. The IF statement can be used as follows: SI <Expression à vérifier> ALORS Traitement à exécuter dans le cas où l'expression est vérifiée SINON Traitement à exécuter dans le cas contraire FIN We will test this statement with a simple WLanguage code example: - If necessary, clear the "Initialization" event of the project and copy the following code.
This code selects a number at random and displays a message according to the value.
NombreAléatoire is int
InitRandom()
NombreAléatoire = Random(100, 4000)
Trace("Valeur du nombre aléatoire " + NombreAléatoire)
IF NombreAléatoire > 2000 THEN
Trace("Nombre supérieur à 2000")
ELSE
Trace("Nombre inférieur ou égal à 2000")
END
In this code, the expression to be evaluated is "RandomNumber>2000". - Let's test this code: click
in the quick access buttons. - If necessary, open the "Debugger trace" pane to see the result: on the "Home" tab, in the "Environment" group, expand "Panes", select "Panes", and then select "Debugger trace".
- The "Debugger trace" pane will show the message that corresponds to your case (as the number is random, the message may change with each test!).
Remark: The IF statement can also be written in a single line using the "? ... ELSE" statement (here, the statement spans over 2 lines because it doesn't fit in the page!): <Expression à vérifier>? <Traitement si expression vérifiée> SINON <Traitement dans le cas contraire> It is an abbreviated form of IF ... THEN ... ELSE: it takes up less space, but it may not be as easy to understand. In our example, the code becomes:
Trace(NombreAléatoire > 2000? "Nombre supérieur à 2000" ELSE "Nombre inférieur ou égal à 2000")
In an IF statement, you can also cumulate conditions using the AND and OR keywords. Here are some simple examples you can test right away. - Compound condition with the AND keyword: Determines if two conditions are met. Both conditions are evaluated.
Condition1 is boolean = False
Condition2 is boolean = True
IF Condition1 AND Condition2 THEN
Trace("Condition1 ET Condition2: <vrai>")
ELSE
Trace("Condition1 ET Condition2: <faux>")
END
When you run the project test again, the "Debugger trace" pane will display:
Condition1 ET Condition2: <faux> - Compound condition with the OR keyword: Tests if either of the conditions are met. Again, both conditions are evaluated.
Condition1 is boolean = False
Condition2 is boolean = True
IF Condition1 OR Condition2 THEN
Trace("Condition1 OU Condition2: <vrai>")
ELSE
Trace("Condition1 OU Condition2: <faux>")
END
When you run the project test again, the "Debugger trace" pane will display:
Condition1 OU Condition2: <vrai>
 The second condition can be ignored if the first condition evaluates to false: simply use the _AND_ keyword. Similarly, using the _OR_ keyword, if the first part of the expression is true, the rest of the expression is not evaluated.
For more details, see IF statement. This statement is used to evaluate an expression and to execute a process for each possible expression value. The SWITCH statement can be used as follows: SELON <Expression> CAS Valeur 1: Traitement 1 CAS Valeur 2: Traitement 2 ... CAS Valeur N: Traitement N
AUTRES CAS Traitement FIN We will write some WLanguage code to try this statement: - If necessary, clear the "Initialization" event of the project and copy the following code. This code retrieves the current date and displays a different message depending on its value. A specific message is displayed for the 1st and 15th of the month. In the other cases, the date of the corresponding day is displayed.
LaDateDuJour is Date
SWITCH LaDateDuJour.Jour
CASE 1: Trace("Nous sommes le premier jour du mois.")
CASE 15: Trace("Nous sommes le 15 du mois.")
OTHER CASE: Trace("Nous sommes le: " + LaDateDuJour.VersChaîne())
END
 Several functions and properties are available in WLanguage. Functions can take parameters and return results. Properties are directly applied to the controls or variables via the following syntax: <Nom du champ ou de la variable>.<Nom de la propriété> In our example, the Day property is used on the CurrentDate variable to get the day of the date. - Let's test this code: click
in the quick access buttons. - The "Debugger trace" pane will show the message that corresponds to your case (the message may vary depending on the actual date!). For example, if the test is run on November 28, 2021, the following message will appear:
Nous sommes le: 28/11/2021
Remarks: - If the line "CASE 1:..." is executed, the other lines of code corresponding to the possible values are not executed.
- Several values can be grouped in the same case. The different values are separated by a comma. For example, to test the month of a date, we can write the following WLanguage code:
SWITCH LaDateDuJour.Mois
CASE 1, 2, 3: Trace("Nous sommes au premier trimestre de l'année.")
CASE 4 <= * <= 6: Trace("Nous sommes au deuxième trimestre de l'année.")
CASE > 6: Trace("Nous sommes au second semestre de l'année.")
END
For a project test run on November 28, 2021, the "Debugger trace" pane will display:
Nous sommes le: 28/11/2021 Nous sommes au second semestre de l'année.
- In our examples, the process corresponding to an expression value is written in a single line of code, directly after the "CASE X:" statement. You can also execute multiple lines of code. In this case, the lines of code are located under the "CASE X:" statement. The following syntax must be used:
SELON <Expression> CAS Valeur 1: Traitement 1 - Ligne de code 1 Traitement 1 - Ligne de code 2 CAS Valeur N: Traitement N - Ligne de code 1 Traitement N - Ligne de code 2 AUTRES CAS Traitement FIN
For more details, see SWITCH statement. In this lesson, you have discovered two conditional statements available in WLanguage: - the IF statement,
- the SWITCH statement.
In the next lesson, we will cover how to use loops in WLanguage.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|