Use conditional program branching structures
- Last UpdatedJul 23, 2024
- 1 minute read
You can dynamically control the execution path of a script based on certain conditions being met. The InTouch HMI supports IF-THEN-ELSE control structures for this purpose.
The basic syntax for an IF-THEN-ELSE control structure is as follows:
Syntax
IF Condition THEN
... statements and/or another IF-THEN-ELSE structure
[ELSE
... statements and/or another IF-THEN-ELSE structure]
ENDIF;
Remember the following rules when working with IF-THEN-ELSE structures:
-
IF-THEN-ELSE structures can be nested, both in the THEN section and in the ELSE section.
-
For every IF statement, there must be a closing ENDIF statement. An ENDIF statement always applies to the nearest prior IF statement on the same nesting level.
-
Condition must be a valid discrete expression. The THEN section is executed if Condition is TRUE. The ELSE section is executed if Condition is FALSE.
-
The ELSE section is optional.
-
Some other programming languages allow you to check multiple conditions on the same hierarchy level of an IF-THEN-ELSE structure and have one general ELSE section that is executed if all of the conditions evaluate to FALSE. (The If-ElseIf-Else structure in Visual Basic is an example of this.) This is not possible in the InTouch HMI. For every condition to check, you must open a new IF-THEN-ELSE structure. Therefore, to have a single section of code to act as the ELSE code for all conditions, you must place it in the ELSE section of the IF-THEN-ELSE structure at the last nesting level.