PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
New WINDEV 25 feature!
  • This lesson will teach you the following concepts
  • Overview
  • The IF and SWITCH statements
  • The IF statement
  • The SWITCH statement
  • Practical example: Using the IF and SWITCH statements
  • Project used
  • Creating the window
  • Creating the window controls for the conditional IF statement
  • Conditional IF statement
  • Creating the window controls for the conditional SWITCH statement
  • SWITCH statement
Lesson 2.2. The conditional statements
This lesson will teach you the following concepts
  • The IF statement.
  • The SWITCH statement.
  • Practical example.
Lesson duration

Estimated time: 1 h
Previous LessonTable of contentsNext Lesson
Overview
WLanguage is a set of statements used to handle data.
The conditional statements are used to compare variables and/or values between themselves in order to perform different processes. Several conditional statements are available:
  • IF... THEN... ELSE... END
  • SWITCH...
The 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
WLanguage 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
See IF statement for more details.

The SWITCH statement

This statement is used to evaluate an expression and to run a process for each possible expression value.
The SWITCH statement can be used as follows:
SWITCH <Expression>
CASE Value 1:
Process 1...
CASE Value 2:
Process 2...
...
CASE Value N:
Process N...

OTHER CASE
Process ...
END
WLanguage code 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

Remark

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:
<Name of control or variable>..<Property name>

You can also use the following syntax:
<Name of control or variable>.<Property name>
In our example, ..Day is used on the D variable to get the day of the date.
The online help of a function or property can be displayed at any time by pressing F1. See "How to access the online help ?" for more details.
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 an expression value. 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...
    OTHER CASE
    Process ...
    END
See SWITCH statement for more details.
Practical example: Using the IF and SWITCH statements
To use the conditional statements, we are going to create the following window:
Test of conditional statements
Two operations can be performed:
  • If the user clicks the check box, the "Close" button is displayed.
  • If the user clicks the "Test" button, the box checked in the check box is displayed.

Project used

To handle the different concepts of this lesson, we are going to create different windows. These windows will be created in the "WD My first windows" project.
  • Open the "WD My First Windows" project if necessary.
    To do so, in WINDEV's home page (Ctrl + <), click "Tutorial" and select "My first windows (Exercise)".

Creating the window

  • Create a new blank window:
    1. Click Creating an element among the quick access buttons. The element creation window appears: click "Window" then "Window". The window creation wizard starts.
    2. Select "Blank" and the "Phoenix" skin template.
    3. Validate. The window is automatically created in the editor. The window for saving an element is displayed.
    4. Specify the window title: "Conditional statements". The window name ("WIN_Conditional_statements") is automatically proposed.
    5. Click "OK" to validate the information shown in the save window.

Creating the window controls for the conditional IF statement

Two controls must be created:
  • a Check Box control used to display (or not) the "Close" button.
  • a "Close" Button control to close the window.
  • To create the Check Box control:
    1. On the "Creation" pane, in the "Usual controls" group, click "Check Box" and click where you want to create the control (e.g., at the top of the window).
    2. Click the control that was just created: the "Option 1" caption becomes editable.
    3. Type the option caption: "Make the closing button visible".
    4. Press Enter to validate the input.
  • The option caption is truncated in the editor. To display the control properly:
    1. Select the control.
    2. In the popup menu (right click), select "Adapt the size".
    3. The control is immediately enlarged and the caption is entirely displayed.
  • To create the "Close" Button control:
    1. On the "Creation" pane, in the "Usual controls" group, click Creating a Button control.
    2. Click the location where the control will be created (for example, on the right of Check Box control).
    3. Click the control that was just created. The text displayed in the control becomes editable. Type the caption: "Close".
    4. Press Enter to validate the input.
  • This Button control is used to close the window. We are going to type the corresponding WLanguage code:
    1. Select the "Close" Button control.
    2. Press F2: the code editor displays the events associated with the control.
    3. Write the following WLanguage code in the "Click BTN_Close" event:
      Close()
      Close is used to close the current window.
    4. Close the code window (click X at the top right corner of code editor).
  • The "Close" Button control must be invisible when the window is opened. This information corresponds to its "initial state". We are going to modify this state in the control description window:
    1. Select the "Close" Button control.
    2. Open the popup menu of the control and select "Description".
    3. In the "UI" tab, change the initial status of the control by clicking "Visible". This option is now unchecked.
    4. Validate the control description window.
    5. The "Close" Button control is still visible in the editor. Its state was changed in execution only.
    6. Save the modifications by clicking Save an element among the quick access buttons (on the left of the ribbon) or by pressing Ctrl + S.

Conditional IF statement

In our example, a click on the check box must trigger the display of Close button.
  • We will enter the WLanguage code associated with the Check Box control:
    1. Select the Check Box control.
    2. Press F2.
    3. Write the following WLanguage code in the event "Whenever modifying CBOX_NoName1":
      IF CBOX_NoName1 = True THEN
      BTN_Close..Visible = True
      ELSE
      BTN_Close..Visible = False
      END
      Let's take a look at this WLanguage code:
      • CBOX_NoName1 and BTN_Close respectively correspond to the names of Check Box and Button controls.

        Tip

        To handle the current control in one of the events associated with it, you can use the keyword MySelf.
        In our example, you also have the ability to write:
        IF MySelf = True THEN
      • The Check Box control is a 2-state control: checked/unchecked. It corresponds in programming to a Boolean variable. If the control is checked, its value is set to True ; if the control is unchecked, its value is set to False.
      • This code tests the value of the Check Box control.
        • If the Check Box control is checked, BTN_Close is made visible via the ..Visible WLanguage property.
        • If the Check Box control is unchecked, the Button control BTN_Close becomes invisible.
    4. Close the code window (click X at the top right corner of code editor).
  • Let's now run the window test:
    1. Click Running a window test among the quick access buttons (or press F9). The window is run.
      The 'Close' Button control invisible
    2. In the window in execution, click the check box. The "Close" button becomes visible.
    3. Click the "Close" button: the window is closed and the window editor is displayed.

Creating the window controls for the conditional SWITCH statement

Two controls must be created:
  • a Radio Button control used to select the value to check.
  • a Button control used to run the test.
  • To create the Radio Button control:
    1. On the "Creation" pane, in the "Usual controls" group, expand "Radio Button" (click the arrow). The list of default Radio Button controls appears.
    2. In the search box at the top of the list of controls, enter "Title". Click the first proposed control (with "M.").
    3. Click inside the window to create the control (below the Check Box control created beforehand for example).
  • To create the "Test" Button control:
    1. On the "Creation" pane, in the "Usual controls" group, click Creating a Button control.
    2. Click the location where the control will be created (on the right of Radio Button control, for example).
    3. Click the control that was just created. The text displayed in the control becomes editable. Type the caption: "Test".
    4. Press Enter to validate the input.

SWITCH statement

In our example, the "Test" button is used to check the selected value and display a message.
  • We are going to enter the WLanguage code associated with the "Test" Button control:
    1. Select the "Test" Button control.
    2. Press F2.
    3. Write the following WLanguage code in the "Click BTN_Test" event:
      SWITCH RADIO_Abbreviated_Titles
    4. When the Enter key is pressed to go to the next line, the code editor automatically displays the different SWITCH capabilities:
      Entering a condition (code editor)
    5. Write the following WLanguage code:
      SWITCH RADIO_Abbreviated_Titles
      // Mr
      CASE 1
      Info("You've selected Mr")
      // Mrs
      CASE 2
      Info("You've selected Mrs")
      // Miss
      CASE 3
      Info("You've selected Miss")
      // Other
      CASE 4
      Info("You've selected Other")
      END
      Let's take a look at this WLanguage code:
      • RADIO_Abbreviated_Titles corresponds to the name of the Radio Button control.
      • The Radio Button control is a control proposing several options (4 in our case). A single option can be checked at a time. The Radio Button control corresponds in programming to an Integer variable. Each option is associated with a value. If this option is checked, the Radio Button control takes for value the identifier of checked option.
      • This code tests the value of the Radio Button control. A message is displayed according to the value of Radio Button control.
    6. Close the code window (click X at the top right corner of code editor).
  • Let's now run the window test:
    1. Click Running the test of the window among the quick access buttons (or press F9).
    2. In the window whose test is run, select an option and click "Test": the message corresponding to the selected option is displayed.
      Window test
    3. Close the window.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 25
Comments
Click [Add] to post a comment