Program Flow Logic
  • 30 Jan 2024
  • PDF

Program Flow Logic

  • PDF

Article summary

Allows conditional execution, based on the evaluation of an expression.

Syntax 1: If condition Then thenpart  [ Else elsepart]

Syntax 1 Remarks: The single-line form is often useful for short, simple conditional tests.  Syntax 1 has these parts:

Part

Description

If

Begins the simple If...Then control structure.

Condition

One of two types of expressions:

A numeric or string expression that evaluates true (nonzero) or false (0 and Null) .

Then

Identifies actions to be taken if condition is satisfied.

Thenpart

Statements or branches performed when condition is true

Else

Identifies actions taken if condition is not satisfied.  If the Else clause is not present, control passes to the next statement in the program.

Elsepart

Statements or branches performed when condition is false

Syntax 2

If condition1   Then                 [ statementblock-1]
[ ElseIf condition2 Then                 [ statementblock-2] ]
[ Else                 [ statementblock-n] ]
End If

Syntax 2 Remarks: The block form of If...Then...Else provides more structure and flexibility than the single-line form and is usually easier to read, maintain, and debug.  Syntax 2 has these parts:

Part

Description

If

Keyword that begins the block If...Then decision control structure.

Condition1

Same as condition used in the single-line form shown above.

Then

Keyword used to identify the actions to be taken if a condition is satisfied.

Statementblock-1

One or more Visual Basic statements executed if condition1 is true

ElseIf

Keyword indicating that alternative conditions must be evaluated if condition1 is not satisfied.

Condition2

Same as condition used in the single-line form shown above.

Statementblock-2

One or more Visual Basic statements executed if condition2 is true

Else

Keyword used to identify the actions taken if none of the previous conditions are satisfied.

Statementblock-n

One or more Visual Basic statements executed if condition1 and condition2 are both false.

End If

Keyword that ends the block form of the If...Then.

 In executing a block If, Visual Basic tests condition1: the first numeric expression.  If the expression is true, the statements following Then are executed.

If the first expression is, Visual Basic begins evaluating each ElseIf condition in turn.  When Visual Basic finds a true condition, the statements immediately following the associated Then are executed.  If none of the ElseIf conditions is true, the statements following the Else are executed.  After executing the statements following Then or Else, the program continues with the statement following End If.

The Else and ElseIf clauses are both optional. There is no limit to the number of ElseIf clauses in a block If, but none can appear after an Else clause.  Any of the statement blocks can contain nested block If statements. The block If must end with an End If statement.

Example: This example calculates the ratio of two input tags, depending on which is larger.  If they are equal, the ELSE statement is executed.

                If GD(1) > GD(2) Then

                                Result = GD(2)/GD(1)

                ElseIf GD(2) > GD(1) Then

                                Result = GD(1)/GD(2)

                Else

                                Result = 1

                End If


Was this article helpful?