<Previous Lesson> <<Home>> < Next Lesson>
7.1 Conditional Operators
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 VB 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
Operator |
Meaning |
|---|---|
=
|
Equal to
|
>
|
More than
|
<
|
Less Than
|
>=
|
More than or equal
|
<=
|
Less than or equal
|
<>
|
Not Equal to
|
7.2 Logical Operators
In addition to conditional operators, there are a few logical operators that offer added power to the VB programs. They 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
|
7.3 Using If.....Then.....Else Statements with Operators
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
Example 7.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
wrong.Visible = False
correct.Visible = False
wrong.Visible = True
End If End Sub;
0 comments :
Post a Comment