ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

New WINDEV 2024 feature!
This content has been translated automatically.  Click here  to view the French version.
Help / WINDEV Tutorial / Tutorial - WLanguage basics
  • 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.
Durée de la leçon 20 mn
Overview
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.
If necessary, open the "WLanguage" project you created in the first lesson of this tutorial (see A project to discover WLanguage in lesson 1).
To view WLanguage events of the project:
  1. Right-click the "P" button next to the open element tabs. The context menu appears.
  2. Select "Element code".
  3. The code editor displays the different events associated with the project.
The IF statement
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:
  1. 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
    // Initialise le générateur de nombre aléatoire
    InitRandom()
    // Prend un nombre au hasard entre 100 et 4000
    NombreAléatoire = Random(100, 4000)
    Trace("Valeur du nombre aléatoire " + NombreAléatoire)
    // Vérifie si ce nombre est supérieur strictement à 2000
    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".
  2. Let's test this code: click Test project in the quick access buttons.
  3. 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".
  4. 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:
// Instruction conditionnelle monoligne
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.
    // Condition composée (mots-clés ET et OU)
    Condition1 is boolean = False
    Condition2 is boolean = True
    // - mot-clé ET
    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.
The SWITCH 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:
  1. 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.
    // Par défaut, la variable de type Date est initialisée avec la date du jour
    LaDateDuJour is Date

    // Teste le jour de la date
    SWITCH LaDateDuJour.Jour
    // 1er jour du mois
    CASE 1: Trace("Nous sommes le premier jour du mois.")
    // 15ème jour du mois
    CASE 15: Trace("Nous sommes le 15 du mois.")
    // Pour toutes les autres jours, affiche la date
    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.
  2. Let's test this code: click Test project in the quick access buttons.
  3. 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
    // Les mois 1, 2 et 3
    CASE 1, 2, 3: Trace("Nous sommes au premier trimestre de l'année.")
    // Les mois compris entre 4 et 6
    CASE 4 <= * <= 6: Trace("Nous sommes au deuxième trimestre de l'année.")
    // Les mois après le sixième mois
    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.
To sum up
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.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 2024
Comments
Click [Add] to post a comment

Last update: 11/18/2023

Send a report | Local help