Monday, March 9, 2009

If/Then

If condition
Then
... executable statements ...
Else
... executable statements ...
End If;
If condition
Then
... executable statements ...
ElsIf condition
Then
... executable statements ...
Else
... executable statements ...
Else
... executable statements ...
End If;
If A = B Then X = 10
ElsIf A = C Then X = 20 /* Consider using a */
ElsIf A = D Then X = 30 /* case statement */
End If;

Nested Ifs

If condition
Then
If condition
Then
... statements ...
Else
If condition
Then
... statements ...
ElsIf condition
Then
... statements ...
End If;
End If;
End If;
An advantage of these structures is that the inner statements are evaluated only when true. Defer costly execution to inner If statements. Assume determining if C is equal to D is time consuming:
Don't code:
If A=B and C=D Then...
Code:
If A=B
Then If C = D
Then...

Note: Null is neither True nor False.

No comments:

Post a Comment