Branching
[ Using VB ]
Branching is a way to transfer control of a program to somewhere else, or to choose one path out of many different possibilities. Branching performs a test, and performs different tasks depending on the results of that test.
The simplest form of branching is a single 'If Then' branch. It is structured like this:
The condition to be tested can use the following tests.
= - Tests if two values are equal.
> - Tests if item 1 is greater than item 2.
< - Tests if item 1 is less than item 2.
<> - Tests if two values aren't equal.
>= - Tests if item 1 is greater than or equal to item 2.
<= - Tests if item 1 is less than or equal to item 2.
Examples:
intTotal = 1
If intTotal <> 1 Then
MsgBox "intTotal isn't equal to 1"
'Won't execute because
intTotal = 1
End If
intTotal = 1
If intTotal = 1 Then
MsgBox "intTotal is equal to 1"
'Will execute because
intTotal = 1
End If
The 'If Then Else' statement is the same as the 'If Then' statement with one exception, the 'Else' part. In the 'If Then' statement code only executes if the statement is true, but with the 'Else' statement there is code that will execute when condition returns false.
If condition Then
... 'Do something if condition is true
Else
... 'Do something if condition is false
End If
Examples:
intTotal = 1
If intTotal <> 1 Then
MsgBox "intTotal isn't equal to 1"
'Won't execute because
intTotal = 1
Else
MsgBox "intTotal is equal to 1"
'Will execute because
intTotal = 1
End If
The 'If Then ElseIf' branching statement can perform multiple different tests, but will only go on to the next test if the previous test was false, so once the code within one branch executes, the program continues from the end of the 'If Then ElseIf' statement.
If condition Then
... 'Do something if condition is true
ElseIf condition1 Then
... 'Do something if condition1 is true
Else 'optional useful if you want 'Default' code
... 'Do something if none of the conditions are false
End If
Example:
intRandNum = Rnd Mod 6
If intRandNum = 0 Then
MsgBox "intRandNum is 0"
ElseIf intRandNum = 1 Then
MsgBox "intRandNum is 1"
ElseIf intRandNum = 2 Then
MsgBox "intRandNum is 2"
ElseIf intRandNum = 3 Then
MsgBox "intRandNum is 3"
ElseIf intRandNum = 4 Then
MsgBox "intRandNum is 4"
ElseIf intRandNum = 5 Then
MsgBox "intRandNum is 5"
End If
'Select Case' is similar to the 'If Then Else' with the exception that you can only test the value of one variable within a 'Select Case' statement.
Select Case testexpression
Case expression
Case Else 'default expression
End Select
For an example, take the previous 'If Then ElseIf' statement, here is how it would be coded in a 'Select Case' statement.
intRandNum = Rnd Mod 6
Select Case intRandNum
Case Is = 0
MsgBox "intRandNum is 0"
Case Is = 1
MsgBox "intRandNum is 1"
Case Is = 2
MsgBox "intRandNum is 2"
Case Is = 3
MsgBox "intRandNum is 3"
Case Is = 4
MsgBox "intRandNum is 4"
Case Is = 5
MsgBox "intRandNum is 5"
End Select
The choose function takes an expression, and many different values, and returns the value that corresponds to the value of the expression.
Choose( index, choice1, choice2 )
If the value of index is 1 then it returns choice 1, and if the value of the index is 2, it returns choice 2, and so on.
Example:
strMonth = Choose( intMonth, "January", "Febuary", "March", "April", ... )
In this example if the value of intMonth is equal to 1, then it returns the string "January", this is a good way to assign certain strings to a variable depending on the value of an integer value.
The if function takes three parameters, an expression, and two values. If the expression tests out as true, then it returns the true value, if it is false, then it returns the false value.
IIf( expression, truepart, falsepart )
Example:
test = IIf( value = True, "True", "False" ) 'If the value is true, then it returns the string 'True'
The Switch function, like the IIf function, evaluates an expression, and returns a value based on the test. The Switch function however takes multiple expressions, and returns the value corresponding to the first expression that returns true.
Switch( expr-1, value-1, expr-2, value-2 )
Example:
strMonth = Switch( intMonth = 1, "January", intMonth = 2, "Febuary", intMonth = 3, "March" )