You are on page 1of 8

Sixth Lecture

6-1Conditional Operators
To control the Visual Basic program flow, we can use various conditional operators.
Basically, they resemble mathematical operators. Conditional operators are very powerful
tools which let the Visual Basic program compare data values and then decide what action
to take, whether to execute or terminate the program etc. These operators are shown in
Table (5-2).
Table 5-2: Comparison Operators (Conditional Operators)
Operator Meaning
= Equal to
> More (Greater) than
< Less than
>= More (Greater)than or equal to
<= Less than or equal to
<> Not equal to
The following table shows some conditional expressions and their results .You’ll
work with conditional expressions several time in this Course.
Conditional Expression Result
10 <> 20 True (10 is not equal 20)
Score <20 True if Score is less than 20; otherwise, false
Score = Label 1.Text True if the Text property of the Label 1 object contains the
same value as the Score variable; otherwise, false
TextBox1 ="Bill" True if the word "Bill" in the TextBox1 object; otherwise, false
6-2 Logical Operators
In addition to conditional operators, there are a few logical operators that offer
added power to the Visual Basic programs. They are shown in Table (5-3).
Table (5-3)
Operators 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
The following table lists some examples of the logical operators at work. In the
expressions, it is assumed that the Vehicle string variable contains the value "Bill", and the
integer variable Price contains the value 200.
Logical expression Result
Vehicle="Bill" And Price <300 True (both conditions are True)
Vehicle="Car" Or Price <500 True (one condition is True)
Vehicle="Bill" Xor Price <300 False (both conditions are True)
Not Price <100 True (condition is False)
P a g e 17 | 24
6-3Conditions Statement
Conditional structures allow applications written in Visual Basic to respond to
different situations depending upon the result of a test condition. The condition can be a
comparison or any expression that evaluates to a numeric value.

6-3-1 If Then Statement


In general, you use an If...Thenstatement when your program must make an
"either/or" decision. When your program must select from more than one alternative, use

an If...Then...Elsestatement, an If...Then...Elseifstatement, or a Select Casestatement.


You choose the structure that is most efficient for your needs. These other
statements are covered in subsequent topics If...Thenstatements evaluate whether a
condition is true or false and direct the program’s flow accordingly. If...Thenstatements
can use either single-line or block form syntax.

Syntax 1
If Condition Then Statements
EX.11: What will appear when the following program is executing?
Private Sub Command1_Click()
X=5
Y=7
If X <> Y Then X = 2 * X
Print "X="; X, "Y="; Y
End Sub

If...Then...Else statements are an elaboration on the If...Thenconcept.


AnIf...Then...Else block allows you to define two blocks of code and have your
programexecute one based upon the result of a condition. If more than one of the
conditionsin a conditional structure is true, only the code statements enclosed by the first
truecondition are executed.

Syntax 2
If Condition Then Statements1 Else Statements2
Note:Can using the function IIF instead of IF … Then ….
IIF (Condition, Statements1, Statements2)
Switch (Condition1, Statements1,Condition2, Statements2)
P a g e 18 | 24
An If...Then...Else statement includes a condition that evaluates to True orFalse,
one or more statements that execute depending on the result of the testcondition, and an
End If statement in the case of a block If...Then...Else statement.

EX.12: What will appear when the following program is executing?


Private Sub Command1_Click()
x = 5: y = 10: z = 30
If x <> 2 Then
y=y+x
z=z+x
x=0
Else
y=y-x
z=z-x
x=1
Print "X="; x
Print "Y="; y
Print "Z="; z
End If
End Sub
Syntax 3
IfConditionThenStatements…End If
Syntax 4
If Condition ThenStatements…ElseIf Condition n ThenStatements…ElseStatements…End If
EX.13: Using Operators with IIf.
Private Sub Command1_Click()
Dim X As Integer
X = Val(Text1.Text)
Label1 = IIf(X >= 50, "Good", "Bad")
Label1 = Switch(X >= 50, "Good", X<50, "Bad")
End Sub
EX.14: Using Operators with If … Then … Else.
Private Sub Command1_Click()
sAge = Val(Text1.Text)
If sAge > 24 And sAge <= 45 Then
Label1 = "Acceptable"
Else
Label1 = "Unacceptable"
End If
End Sub
P a g e 19 | 24
EX.15: Using Operators with If … Then … Else.
Private Sub Command1_Click()
FirstNum = Val(Text1.Text)
SecondNum = Val(Text2.Text)
Total = Firstnum + SecondNum
If Total = Val(Text3.Text) And Val(Text3.Text) <> 0 Then
Label1.Caption = "Yes, you are Correct"
Else
Label1.Caption = "Sorry, you are wrong"
End If
End Sub
EX.16 Using If … Then … Else.
Private Sub Command1_Click()
x = 8: y = 8
If x > 10 Then
x=x+7
Else
If x < 5 Then
x = x + 13
Else
If y < 10 Then
y=y+x
Else
y=y*2
End If
End If
End If
Print x, y
End Sub
EX.17: Using If … Then … ElseIf … Else.
Private Sub Command1_Click()
x = 3: y = 10
If x > 5 Then
x=x+7
ElseIf x < 5 Then
x = x + 13
ElseIf y < 10 Then
y=y+x
Else
y=y*2
End If
Print x, y
End Sub
P a g e 20 | 24
6-3-2 Select Case Statement
Allow your program to choose from more than two alternatives. However, the
Select Case structure can be more efficient because it evaluates the test expression
onlyonce. The result of the expression is then compared against multiple values
todetermine which code block is invoked.The following example shows the code syntax
for the Select Case structure.One of several groups of statements is run, depending on the
value of an expression.

Syntax
Select Case variable
Case value 1
Statements executed if value 1 matches variable
Case value 2
Statements executed if value 2 matches variable
Case value n
Statements executed if value n matches variable
Case Else
Statements executed if value no matches is found
End Select
Ex.18: Using Operatorswith Select Case.
Private Sub Command1_Click()
Dim age As Integer
age = Val(Text1.Text)
Select Case age
Case is >=16
Label1 <= "you can drive now"
Case 18
Label1 = "you can vote now"
Case 21
Label1 = "you can travel"
Case 65
Label1 = "time to retire and have fun"
Case Else
Label1 = "you’re a great age!, Enjoy it"
End Select
End Sub

P a g e 21 | 24
EX.19:Using ListBox and Select Case.
Private Sub Form_Load()
List1.AddItem "Green"
List1.AddItem "Yallow"
List1.AddItem "Blue"
List1.AddItem "Red"
List1.AddItem "White"
List1.AddItem "Black"
End Sub
Private Sub List1_Click()
Dim iColor As Integer
Dim sBackground As String
iColor = List1.ListIndex
Select Case iColor
Case 0
sBackground = vbGreen
Case 1
sBackground = vbYellow
Case 2
sBackground = vbBlue
Case 3
sBackground = vbRed
Case 4
sBackground = vbWhite
Case 5
sBackground = vbBlack
End Select
Form1.BackColor = sBackground
End Sub
EX.20:
Private Sub Command1_Click()Label3 = "Very Good"
Dim degree As Single Case 90 To 100
If IsNumeric(Text1.Text) ThenCase 90 To 100
degree = Val(Text1.Text) Label3 = "Excellent"
Select Case degreeCase Is > 100
Case 0 To 49MsgBox "Higher degree must be <=100"
Label3 = "Fail"Case Else
Case 50 To 59End Select
Label3 = "Pass"Else
Case 60 To 69MsgBox "Your degree is wrong"
Label3 = "Pass-High"End If
Case 70 To 79End Sub
Label3 = "Good"
Case 80 To 89
P a g e 22 | 24
6-3-3 GoTo Statement
Another branching statement, and perhaps the most hated statement
inprogramming, is the GoTo statement. However, we will need this to do Run-Timeerror
trapping. The format is GoTo Label, where Label is a labeled line.Labeled lines are
formed by typing the Label followed by a colon.

Syntax
Sub…
A:
Statement…
GoTo Beta
Statement…
GoTo A
Statement…
Beta:
End Sub
GoTo Example:
Line10:
.

GoTo Line10
When the code reaches the GoTo statement, program control transfers to the
linelabeled Line10.

Ex.21: Using GoTo Statement and Numeric Data Type as Integer


Private Sub Command1_Click()
Dim x, y, Max As Integer
x = Val(Text1.Text)
y = Val(Text2.Text)
Max = x
If x > y Then GoTo 10
Max = y
10 Text3.Text = Max
End Sub

P a g e 23 | 24
6-3-4With End With Statement
Syntax
With Object
. Property = Setting
End With
EX.22: Using With Statement
Label1.Caption = “Your”
Label1.BackColor = vbGreen
Label1.ForeColor = vbBlue
With Label1
.Caption = “Bibo”
.BackColor = vbGreen
.ForeColor = vbBlue
End With
6-3-5 Choose Statement
Syntax
Value = Choose (Number, Value1, Value2, Value n)
EX.23: Using With and Choose Statement
Private Sub Command1_Click()
With Label1
.ForeColor = vbRed
.BackColor = vbWhite
End With
T = Text1.Text
Label1 = Choose(T, 20, 40, 60, 80)
End Sub
H.W: Write a program using the Select Case statement to inform a person about his/her
eight status based on the body mass index (BMI) where BMI=bodyweight in kilograms
dividedby the square of the height in meters. The weight status is usually shown in the
table below:
BMI BMI Weight Status

Below 18.5 Underweight

18.5 – 24.9 Normal

25.0 – 29.9 Overweight

30.0 And Above Obese

P a g e 24 | 24

You might also like