PC SOFT

ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Home | Sign in | English EN
New WINDEV Mobile 26 feature!
  • In this lesson you will learn the following concepts
  • Overview
  • Practical example
  • The IF statement
  • The SWITCH statement
Lesson 3.5. Conditional statements
In this lesson you will learn the following concepts
  • The IF statement.
  • The SWITCH statement.
Lesson duration

Estimated time: 20 mn
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...

Practical example

To perform the actions 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 "Initializing" event of the project.

  • 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 to the result of an expression. If the expression evaluates to true, a process is executed. If if evaluates to false, another process can be executed.
The IF statement can be used as follows:
IF <Expression to evaluate> THEN
  Process to execute if the expression evaluates to true
ELSE
  Process to execute otherwise
END

  • We will test this statement with a simple WLanguage code example:
    1. If necessary, clear the "Initializing" event of the project and copy the following code.
      This code selects a number at random and displays a message according to the value.
      gnRandomNumber is int
      // Initializes the generator of random numbers
      InitRandom()
      // Selects a number at random between 100 and 4000
      gnRandomNumber = Random(100, 4000)
      Trace("Random number value " + gnRandomNumber)
      // Check if this number is strictly greater than 2000
      IF gnRandomNumber > 2000 THEN
      Trace("The number is greater than 2000")
      ELSE
      Trace("The number is less than or equal to 2000")
      END
      In this code, the expression to be evaluated is "gnRandomNumber>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" pane, in the "Environment" group, expand "Panes" and 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 statement? ... ELSE:
<Expression to evaluate>? <Process if expression evaluates to true>
ELSE <Process otherwise>
Here, the statement spans over 2 lines because it doesn't fit in the page! 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:
// Single-line conditional statement
Trace(gnRandomNumber > 2000? "The number is greater than 2000"
ELSE "The number is less than or equal to 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.
      // Compound condition (AND and OR keywords)
      gbCondition1 is boolean = False
      gbCondition1 is boolean = True
      // - AND keyword
      IF gbCondition1 AND gbCondition2 THEN
      Trace("gbCondition1 and gbCondition2: <true>")
      ELSE
      Trace("gbCondition1 and gbCondition2: <false>")
      END

      When you run the project test again, the "Debugger trace" pane will display:
      gbCondition1 AND gbCondition2: <false>
    • Compound condition with the OR keyword: Tests if either of the conditions are met. Again, both conditions are evaluated.
      gbCondition1 is boolean = False
      gbCondition1 is boolean = True
       
      IF gbCondition1 OR gbCondition2 THEN
      Trace("gbCondition1 OR gbCondition2: <true>")
      ELSE
      Trace("gbCondition1 OR gbCondition2: <false>")
      END

      When you run the project test again, the "Debugger trace" pane will display:
      gbCondition1 OR gbCondition2: <true>

      Remark

      The second condition can be ignored if the first condition evaluates to false: simply use the _AND_ keyword. For example:
      gbCondition1 is boolean = False
      gbCondition1 is boolean = True
       
      IF gbCondition1 _AND_ gbCondition2 THEN
      Trace("gbCondition1 _AND_ gbCondition2: <true>")
      ELSE
      Trace("gbCondition1 _AND_ gbCondition2: <false>")
      END

      When you run the project test again, the "Debugger trace" pane will display:
      gbCondition1 _AND_ gbCondition2: <false>
      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:
SWITCH <Expression>
CASE Value 1:
Process 1
CASE Value 2:
Process 2
...
CASE Value N:
Process N

OTHER CASE
Process
END

  • We will write some WLanguage code to try this statement:
    1. If necessary, clear the "Initializing" 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.
      // By default, the Date variable is initialized with the current date
      gdCurrentDate is Date
       
      // Checks the day of the date
      SWITCH gdCurrentDate.Day
      // 1st day of the month
      CASE 1: Trace("Today is the first day of the month.")
      // 15th day of the month
      CASE 15: Trace("Today is the 15th day of the month.")
      // For all other days, display the date
      OTHER CASE: Trace("Today is: " + gdCurrentDate.ToString())
      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>
      In our example, the Day property is used on the gdCurrentDate variable to get the day that corresponds to 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, for a test run on November 28, 2020:
      Today is: 28/11/2020
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 gdCurrentDate.Month
    // Months 1, 2 and 3
    CASE 1, 2, 3: Trace("We are in the first quarter of the year.")
    // Months 4 to 6
    CASE 4 <= * <= 6: Trace("We are in the second quarter of the year.")
    // Months after the sixth month
    CASE > 6: Trace ("We are in the second half of the year.")
    END

    For a project test run on November 28, 2020, the "Debugger trace" pane will display:
    Today is: 28/11/2020
    We are in the second half of the year.
  • Several lines of code can be executed 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.
Previous LessonTable of contentsNext Lesson
Minimum version required
  • Version 26
This page is also available for…
Comments
Click [Add] to post a comment