2. The Visual Basic Language
Visual Basic Branching - If Statements
- Branching statements are used to cause certain actions within
a program if a certain condition is met.
- The simplest is the If/Then statement:
If Balance - Check < 0 Then Print "You are overdrawn"
Here, if and only if Balance - Check is less than zero, the statement
"You are overdrawn" is printed.
- You can also have If/Then/End If blocks to
allow multiple statements:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
End If
In this case, if Balance - Check is less than zero, two lines of
information are printed.
- Or, If/Then/Else/End If blocks:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
Else
Balance = Balance - Check
End If
Here, the same two lines are printed if you are overdrawn (Balance -
Check < 0), but, if you are not overdrawn (Else), your new
Balance is computed.
- Or, we can add the ElseIf statement:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
ElseIf Balance - Check = 0 Then
Print "Whew! You barely made it"
Balance = 0
Else
Balance = Balance - Check
End If
Now, one more condition is added. If your Balance equals the
Check amount (ElseIf Balance - Check = 0), a different
message appears.
- In using branching statements, make sure you consider all viable
possibilities in the If/Else/End If structure. Also, be aware that
each If and ElseIf in a block is tested sequentially. The first time
an If test is met, the code associated with that condition is executed
and the If block is exited. If a later condition is also True, it will
never be considered.
Counter Hit
This Homepage is special brought to you by CK Tan