|
|
|
|
|
- Lesson 4 - Conditional statements
- Overview
- Practical example
- The IF statement
- The SWITCH statement
- To sum up
Tutorial - WLanguage basicsLesson 4 - Conditional statements We will cover the following topics: - The IF statement.
- The SWITCH statement.
20 mn 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: - Right-click the "P" button next to the open element tabs. The context menu appears.
- Select "Element code".
- The code editor displays the different events associated with the project.
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: - 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.
RandomNumber is int
InitRandom()
RandomNumber = Random(100, 4000)
Trace("Random number value " + RandomNumber)
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". - Let's test this code: click in the quick access buttons.
- 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".
- 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:
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.
Condition1 is boolean = False
Condition2 is boolean = True
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>
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. 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: - 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.
CurrentDate is Date
SWITCH CurrentDate.Day
CASE 1: Trace("Today is the first day of the month.")
CASE 15: Trace("Today is the 15th day of the month.")
OTHER CASE: Trace("Today is: " + CurrentDate.ToString())
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: <Name of control or variable>.<Property name> In our example, the Day property is used on the CurrentDate variable to get the day of the date. - Let's test this code: click in the quick access buttons.
- 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:
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
CASE 1, 2, 3: Trace("We are in the first quarter of the year.")
CASE 4 <= * <= 6: Trace("We are in the second quarter of the year.")
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. 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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|