<Previous Lesson> <<Home>> < Next Lesson>
In previous lessons, we have learned how to create Visual Basic 6 code that can accept input from the user and display the output without controlling the program flow. In this lesson, you will learn how to create Visual Basic 6 code that can make decision when it process input from the user, and control the program flow in the process. Decision making process is an important part of programming because it can help to solve practical problems intelligently so that it can provide useful output or feedback to the user. For example, we can write a program that can ask the computer to perform certain task until a certain condition is met.| To control the VB program flow, we can use various conditional operators. Basically, they resemble mathematical operators. Conditional operators are very powerful tools, they let the Visual Basic 6 program compare data values and then decide what action to take, whether to execute a program or terminate the program and more. These operators are shown in Table 7.1. Table 7.1: Conditional Operators
|
In addition to conditional operators, there are a few logical operators which offer added power to the VB programs. There are shown in Table 7.2.
Table 7.2:Logical Operators
|
Operator |
Meaning |
|
And |
Both sides must be true |
|
or |
One side or other must be true |
|
Xor |
One side or other must be true but not both |
|
Not |
Negates truth |
* You can also compare strings with the above operators. However, there are certain rules to follows: Upper case letters are less than lowercase letters, "A"<"B"<"C"<"D".......<"Z" and number are less than letters.
To effectively control the VB program flow, we shall
use If...Then...Else statement together with the conditional operators and
logical operators.
The general format for the if...then...else statement is
If conditions Then
VB expressions
Else
VB expressions
End If
* Any If...Then.. Else statement must end with End If. Sometime it is not necessary to use Else.
|
Example7.1 : Private Sub OK_Click() firstnum=Val(usernum1.Text) secondnum=Val(usernum2.Text) If total=firstnum+secondnum And Val(sum.Text)<>0 Then correct.Visible = True End Sub |
Example 7.2
In this example, the program will check whether the password entered by the user matches the password assigned by the Form_Load procedure. If the two passwords match, then the textbox will disappear and the label will display “Login Successful”; otherwise the label will display "Login Fail! Please enter your password again" and the user has to key in the password again.
Dim password As String
Private Sub Command1_Click()
If Text1.Text = password Then
Text1.Visible = False
Label1.Visible = True
Label1.Caption = "Login Successful"
Else
Label1.Visible = True
Label1.Caption = "Login Fail! Please enter your password again"
Text1.Text = ""
End If
End Sub
Private Sub Form_Load()
password = "1234"
End Sub
We need to use nested If...Then... Else statements if a condition is depending on other conditions that have to be fulfilled first. A nested If statements are enclosed within other If statements. There can be many levels of nested If .. Then ..Else statements but will examine only the two-level nested statements. The format for a two level nested If ..Then...else statement is as follows:
If condition A Then
If Condition B Then
VB6 statement
Else
VB6 statement
End If
End If
* Any If...Then.. Else statement must end with End If. For two-level nested If, you must have two End If
Example 7.3
In this example, both conditions i.e. user name and password must be fulfilled before login can be sucessful.
Private Sub Command1_Click()
Dim userpassword, userName As String
userpassword = "123"
userName = "Liew"
If Text1.Text = userName ThenIf Text2.Text = userpassword Then
MsgBox ("Login Successful")
Else
MsgBox ("Wrong password, login failed, please try again!")
End IfElse
MsgBox ("Wrong user name, login failed, please try again!")
End If
Example 7.4
In this example, suppose an airline company wish to recruit air stewards and air stewardesses, the height requirement might be at least 170 cm for male candidates and 160 for female candidates. The following code heps to filter the candidates based on gender and height.
If Option_Male.Value = True Then
If Val(txtHeight.Text) >= 170 Then
MsgBox ("Congratulation, welcome to join our team")
Else
MsgBox ("Sorry, your application is not sucessful,thank you.")
End If
Else
If Val(txtHeight.Text) >= 160 Then
MsgBox ("Congratulation, welcome to join our team")
Else
MsgBox ("Sorry, your application is not sucessful,thank you.")
End If
End If
View more examples that involve the usage of If......Then.... Else
Polling System Choice Selection Password Cracker Dice Reversi Tic Tac Toe Drag and Drop Slot Machine Library System
<Previous Lesson> <<Home>> < Next Lesson>
Copyright ® 2008 Dr.Liew Voon Kiong . All rights reserved |Contact: admin@vbtutor.net