If...Then...Else

    This handy statement allows you to compare two or more conditions.  It would appear like this:

Dim Counter As Integer
Do
Counter = Counter + 1
If Counter = 10 Then Exit Do
Loop

    You see, in this statement the Do Loop(See Do Loops Tutorial) kept looping and adding 1 to the variable Counter.   After it added one to Counter it checked to see if Counter equaled 10.  That is what the If part means.  If Counter equals(=) 10.  The Then statement tells the program what to do if Counter does equal 10.  Exit Do exits the loop.  You could replace Counter, 10 and Exit Do with whatever you would like.

    Mabey you are thinking "Well, what if I need to do a bunch of different things after the Then statement?"  If this is the fact, then the End If statement is for you.  You would use it like the this:

If Counter = 10 Then
(Put Code Here)
End If

    There is another part of the If statement that will save you a lot of time.  It is the Else statement.  The Else statement is used in the following code example where a user would have entered something into a textbox called Text1.  This If statement is meant to see if they did not enter anything.  If they didn't then it ends the program.  If there is something entered then it gives whatever that is to the variable Name.

Dim Name As String
If Text1.Text = "" Then
End
Else
Name = Text1.Text
End If

    You see here that the Else statement will require you to have an End If.  Well, that should otherwise be self-explainatory.  The Else statement is always carried out if the first condition is not met.  If the first statement is met then whatever happens after the Else statement is not carried out.

Back

Copyright ©2000 by Kyle Baker.  Please read our disclaimer.