The conditional statements are very important to any programming language. In VB 6, the conditional statement change the direction of the program using If...Then ...Else
construct.
The syntax for If...Then...Else
is given below.
If condition Then
[Statements]
Else If condition 1 Then
...
[statements]
...
Else If condition n Then
...
[Statements]
...
Else
...
[Statements]
...
End If
If you look at the above syntax , the conditions are very important for If statements. If the condition 1
is false then another condition is checked and so on. The condition is met (True), then that statement block is executed. If no condition is met, then the last Else
statement is executed.
Example Program: If…Then…Else
Consider the following example code, the program has a variable called day
. If the day
is 1, it is Monday, else it is Tuesday, and so on.
Private Sub Text1_Change()
Dim day As Integer
day = Val(Text1.Text)
If day = 1 Then
Text2.Text = "Monday"
ElseIf day = 2 Then
Text2.Text = "Tuesday"
ElseIf day = 3 Then
Text2.Text = "Wednesday"
ElseIf day = 4 Then
Text2.Text = "Thrusday"
ElseIf day = 5 Then
Text2.Text = "Friday"
ElseIf day = 6 Then
Text2.Text = "Saturday"
ElseIf day = 7 Then
Text2.Text = "Sunday"
Else
Text2.Text = "Enter Number Less Than Equal To 7"
End If
End Sub
If the number for variable day is greater than 7 , then default message is displayed – "Enter number less than equal to 7"
.
Output – If…Then…Else
The output of the above program is given below.