PC SOFT

ONLINE HELP
FOR WINDEV, WEBDEV AND WINDEV MOBILE

Home | Sign in | English US
  • 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

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
Code sample: 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".
Note: 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 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
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
Notes:
  • 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
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 the home window, click "Tutorial" and select the first project "My first windows (Exercise)".
    Tip: if the home window is not displayed, on the "Home" pane, in the "Online help" group, expand "Tutorial" and select "My first windows (Exercise)".

Creating the window

  • Create a new blank window:
    1. Click Creation among the quick access buttons. The window for creating a new element is displayed: click "Window" then "Window". The wizard for window creation starts.
    2. Select "Blank" and the "Elegant" skin template.
    3. Validate. The window is automatically created in the editor. The backup window of created element is displayed.
    4. Specify the window title: "Conditional statements". The window name ("WIN_Conditional_statements") is automatically proposed.
    5. Click the green button to validate the information displayed in the backup 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 used to close the window.
  • To create the Check Box control:
    1. On the "Creation" pane, in the "Usual controls" group, click "Check box" then click the position where the control will be created in the window (at the top of window for example).
    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 mouse click), select "Resize".
    3. The control is immediately enlarged and the caption is entirely displayed.
  • To create the "Close" button:
    1. On the "Creation" pane, in the "Usual controls" group, click Button control.
    2. Click the location where the button will be created (on the right of Check Box control for example).
    3. Click the button that was just created. The text displayed in the button becomes editable. Type the caption: "Close".
    4. Press Enter to validate the input.
  • This button is used to close the window. We are going to type the corresponding WLanguage code:
    1. Select the "Close" button.
    2. Press the F2 key: the code editor displays the processes associated with the button.
    3. Type the following code in the "Click BTN_Close" process:
      Close()
      Close is used to close the current window.
  • The "Close" button must be invisible when opening the window. This information corresponds to its "initial status". We are going to modify this status in the description window of control:
    1. Select the "Close" button.
    2. Display the popup menu of control and select "Description".
    3. In the "GUI" tab, modify the initial status of control by clicking "Visible". This option is now unchecked.
    4. Validate the description window of control.
    5. The "Close" button is still visible in the editor. Its status was changed in execution only.
    6. Save the modifications by clicking Save among the quick access button (on the left of 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 are going to type the WLanguage code associated with the check box:
    1. Select the Check Box control.
    2. Press the F2 key.
    3. Type the following code in the "Whenever modifying CBOX_NoName1" process:
      IF CBOX_NoName1 = True THEN
      BTN_Close..Visible = True
      ELSE
      BTN_Close..Visible = False
      END
      Let's study this 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 processes associated with it, you have the ability to use the MySelf keyword.
        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 checks the value of check box.
        • If the check box is checked, the BTN_Close button becomes visible via the WLanguage ..Visible property.
        • If the check box is unchecked, the BTN_Close button becomes invisible.

Note

Several functions and properties are available in WLanguage. The functions can accept parameters and they return results. The properties are directly applied to the controls via the following syntax:
<Control name>..<Property name>
In our example, ..Visible is used on the BTN_Close control.
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.
  • Let's now run the window test:
    1. Click Window GO among the quick access buttons (or press F9).
    2. In the window in execution, click the check box. The "Close" button becomes visible.
      Close button 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 Radio Button controls proposed by default is displayed.
    2. In the list of controls, in the "Pre-filled" category, click the second 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:
    1. On the "Creation" pane, in the "Usual controls" group, click Button control.
    2. Click the location where the button will be created (on the right of Radio Button control for example).
    3. Click the button that was just created. The text displayed in the button 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 to display a message.
  • We are going to type the WLanguage code associated with the Test button:
    1. Select Test button.
    2. Press the F2 key.
    3. Type the following code in the "Click BTN_Test" process:
      SWITCH RADIO_Abbreviated_Titles
    4. When pressing the Enter key to go to the next line, the code editor automatically displays the different SWITCH capabilities:
      Code editor
    5. Type the following code:
      SWITCH RADIO_Abbreviated_Titles
      // M.
      CASE 1
      Info("You've selected M.")
      // 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 study this code:
      • RADIO_Titles corresponds to the name of 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 menu 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 checks the value of radio button. A message is displayed according to the value of radio button.
  • Let's now run the window test:
    1. Click Window Go 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 required version
  • Version 23
Comments
Check box properties
As I'm working through the tutorial, I found out, that obviously the "caption" property of the check box control is not the correct description. The "caption" property is (as a standard) invisible and must be made visible using the "style" tab (last tab in the left column of tabs) and then by using the "Position" Box, by choosing "Overlap" e.g.

What you mean in the tutorial text is the text of the option itself, found in the "content" tab (5th in the left column) and then by entering the text in the text line of the option itself.

Please correct this in your tutorial text.
Thanks a lot for this great environment!
Franz HÖRMANN
Jan. 10 2019