Automation, Control & Plant Intelligence - Articles, Analysis, Reviews, Interviews & Views
We can use the If-Then-Else statement to run a specific statement or a block of statements, depending on the value of a condition. If-Then-Else statements can be nested to as many levels as you
need.
TIP: However, for readability, you should use Select
Case statement rather than multiple levels of nested If...Then...Else
statements.
Use it when only one condition is
True, you can use the single-line syntax of the IF-THEN-ELSE
statement.
This following example shows the
single-line syntax, omitting the
Else:
Sub myDate()myDate = #1/1/99#If myDate < Now Then myDate = NowEnd Sub
To run more than one line of code, you must use the multiple-line syntax.
This syntax includes the End If statement, as shown in the following example:
Sub AlertUser(value as Long)IF value = 0 THENMsgbox “Hello Automation Guru”ValveOutput = 43Pump3 = TrueEND IFEnd Sub
You can use an IF-THEN-ELSE
statement to define two blocks of executable statements:
one block runs if the condition is
True,
the 2nd block runs if the
condition is False.
Sub AlertUser(value as Long)If value = 1 ThenValve1Output = 43Pump3 = TrueElseSystemDown = 1SetAlarm21 = TrueEnd IfEnd Sub
You can add ElseIf statements
to an IF-THEN-ELSE statement to test a second condition if the first
condition is False.
For example, the following function
procedure computes a damper output based on off set.
The statement following the
Else statement runs if the conditions in all of the If and
ElseIf statements are False.
Function UpdateValve(RealValue, offSet)If performance = 1 ThenRealValue = offSet * 0.1ElseIf performance = 2 ThenRealValue = offSet * 0.09ElseIf performance = 3 ThenRealValue = offSet * 0.07ElseRealValue = 0End IfEnd Function