New WEBDEV 28 feature!
• In this lesson you will learn the following concepts
• Overview
• Practical example
• The IF statement
• The SWITCH statement
Appendix 5. Conditional statements
In this lesson you will learn the following concepts
• The IF statement.
• The SWITCH statement.
 Estimated time: 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 "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.
 Note You can also complete the actions described in this lesson using WDScript, the WLanguage script editor. To open WDScript, go to the "Tools" tab, "Utilities" group, and click "WDScript".
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:
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.
RandomNumber is int
// Resets the random number generator
InitRandom()
// Selects a number at random between 100 and 4000
RandomNumber = Random(100, 4000)
Trace("Random number value " + RandomNumber)
// Check if this number is strictly greater than 2000
IF RandomNumber > 2000 THEN
Trace("Number greater than 2000")
ELSE
Trace("Number less than or equal to 2000")
END
In this code, the expression to be evaluated is "RandomNumber>2000".
2. Let's test this code: click 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 to evaluate>? <Process if expression evaluates to true>
ELSE <Process otherwise>
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(RandomNumber > 2000? "Number greater than 2000" ELSE "Number 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)
Condition1 is boolean = False
Condition2 is boolean = True
// - AND keyword
IF Condition1 AND Condition2 THEN
Trace("Condition1 AND Condition2: <true>")
ELSE
Trace("Condition1 AND Condition2: <false>")
END

When you run the project test again, the "Debugger trace" pane will display:
Condition1 AND Condition2: <false>
• 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 OR Condition2: <true>")
ELSE
Trace("Condition1 OR Condition2: <false>")
END

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

 Note The second condition can be ignored if the first condition evaluates to false: simply use the _AND_ keyword. For example: Condition1 is boolean = FalseCondition2 is boolean = True IF Condition1 _AND_ Condition2 THEN Trace("Condition1 _AND_ Condition2: ")ELSE Trace("Condition1 _AND_ Condition2: ")END When you run the project test again, the "Debugger trace" pane will display: Condition1 _AND_ Condition2: 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
CurrentDate is Date

// Checks the day of the date
SWITCH CurrentDate.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: " + CurrentDate.ToString())
END

 Note 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: . 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 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:
Today is: 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 CurrentDate.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, 2021, the "Debugger trace" pane will display:
Today is: 28/11/2021
We are in the second half of the year.
• 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:
SWITCH <Expression>
CASE Value 1:
Process 1 - Line of code 1
Process 1 - Line of code 2
CASE Value N:
Process N - Line of code 1
Process N - Line of code 2
OTHER CASE
Process
END
For more details, see SWITCH statement.