About Conditional Statements in iLogic
iLogic provides conditional statements you can use in the Edit Rule dialog.
You place the statements in rules (small Visual Basic programs) that you define for your model. Conditional statements such as If-Then-Else allow your rules to perform actions according to certain criteria.
If-Then-Else statement in iLogic
In the If-Then-Else
statement, one set of actions executes if a condition is true, and another set of actions executes if the condition is false.
After the true or false statements execute, program control resumes with the next statement.
In iLogic, you can use the block or multi-line form to implement the If-Then-Else
structure.
For example:
If size = “small” Then
length = 6.0
Else
length = 12
End If
If the conditional expression is true, the statements between the keywords Then
and Else
are executed. The statements between the keywords Else
and End If
are bypassed. If the conditional expression is false
, the statements between the keywords Else
and End If
are executed. The statements between the keywords Then
and Else
are bypassed. In any case, program control resumes with the statement following the End If
statement.
If-Then statement in iLogic
In an If-Then-Else
statement, action is taken regardless of whether the condition is true
or false
. However, sometimes you perform an action or set of actions only if a condition is true
, but perform no action if the condition is false
.
You can omit the Else
portion of the block If
structure. For example:
If size = “small” Then
length = 6.0
End If
Null or empty sets of actions in iLogic
You can also use null
or empty
sets of actions in the block form of the If
statement. For example, you can state "if a condition is true, do nothing - else, do something," as in the following block:
If length > 20 Then
Else MessageBox.Show(" Invalid Size.Length must be at least 20 in. ", "Title")
End If
When using an empty set of actions, improve readability by including a comment where the true
statements would normally go:
If length > 20 Then ’do nothing
Else MessageBox.Show(" Invalid Size.Length must be at least 20 in ", "Title")
End If
Use of If statement as single-line statement
Although the block form of the If
statement enhances readability, you can also write the If
statement as a single line. In this format, the End If
statement is not used.
Examples include:
If length < 20 Then MessageBox.Show("Length is Too Short", "Title")
If mass < 1000 Then length =20 Else length =40
Use of Boolean variables in conditional expressions
A Boolean variable, which can hold the value True
or False
, can be used anywhere a relational expression can be used.
For example, the following two statements are equivalent:
If Feature.IsActive("Fillet1") = true Then . . .
If Feature.IsActive("Fillet1") Then . . .
In the second statement, the True
value is implied.
Similarly, the following two statements can be used interchangeably to test whether a Boolean variable is false:
If Feature.IsActive("Fillet1") = false Then . . .
If Not Feature.IsActive("Fillet1") Then . . .
These examples show the result of a function. Other examples include using a parameter name as a Boolean variable, or using a local variable to hold a Boolean value.
Case structure in conditional statements in iLogic
The case structure lets you choose between more than two alternatives.
Case structure can be implemented using an extended block If
structure or the Select Case
statement structure.
Extended block If statement (If-Then-ElseIf) in iLogic
The extended block If
statement uses one or more ElseIf
clauses that are nested between the first If
clause and the last Else
clause. The keyword ElseIf
is one word.
For example:
If material = "Steel" Then
density = 0.284
ElseIf material = "Copper" Then
density = 0.323
ElseIf material = "Aluminum" Then
density = 0.098
End If
In this format, the program executes the statements associated with the first true conditional expression found. Then, the program exits to the statement following the End If
statement. Use the final Else
statement to trap errors that occur when unexpected conditions are encountered that do not match the previous If
or ElseIf
conditions.
Using ElseIf
eliminates the need to code multiple End If
statements in a nested If
structure. For comparison, the previous example can also be written as:
If material = "Steel" Then
density = 0.284
Else
If material = "Copper" Then
density = 0.323
Else
If material = "Aluminum" Then
density = 0.098
End If
End If
End If
Select Case statement in iLogic
Select Case
is a powerful statement with several options.
For example:
Select Case material
Case “Steel”
density =0.284
Case “ Copper”
density = 0.323
Case “Aluminum”
density = 0.098
Case Else
density = 0
MessageBox.Show("not a valid material" , "Warning")
End Select
In this format, the Select Case
statement specifies an expression to test. Each subsequent Case
clause specifies one or more expressions to compare to the test expression. The first Case
clause containing an expression that matches the test expression has its associated actions executed. Program control then branches to the statement following the End Select
statement. The final Case Else
clause traps errors that occur when an unexpected value of the test expression does not match the expression list in any of the previous Case
clauses.
The test expression following Select Case
can be a more complex expression, such as:
Select Case radius +1
Select Case MinOfMany(x,y,z)
The expression list in a Case
clause can follow any of the following formats:
For example:
Select Case width
Case 1,2,3,4,5
MessageBox.Show("Small Plate", "Size")
Case 6 to 15
MessageBox.Show("Medium Plate", "Size")
Case Is > 15
MessageBox.Show("Large Plate", "Size")
End Select
The Select Case
statement does not require an association with one particular variable or expression. It can be used to evaluate any number of conditions, using the following format:
Select Case True
Case length >= 10
width = length -1
thickness = length/5
Case length < 10
width = length - .75
thickness = length/6
End Select