1. Lesson 3 of 8 Visual Basic Programming
1 of 4
Lesson 3: Conditional Logic
Author: Kasun Ranga Wijeweera
Email: krw19870829@gmail.com
Date: 2020 April 29
Look at the code segment given below.
Dim x, y As Integer
x = 5
y = 5
If x = y Then MessageBox.Show (“Equal”)
Execution of the above code segment displays a message box
with text “Equal”.
The equals sign shown in red color works as the Equality
Operator. If the value of x equals to the value of y then the code
fragment x = y returns True. Otherwise the code fragment returns
False.
If…Then statement was used in the code segment above. A
<<condition>> should be written between If and Then parts. The
code fragment in the right side of the Then part is executed only if the
<<condition>> returns True.
Now look at the code segment given below.
Dim x, y As Integer
x = 5
2. Lesson 3 of 8 Visual Basic Programming
2 of 4
y = 6
If x = y Then MessageBox.Show (“Equal”)
Execution of the above code segment does not display the
message box as the <<condition>> returns False.
Only a single line of code can be included after Then part when
using If…Then statement. Therefore, If…Then…End If statement
should be used in order to have more than a single line of code after
Then part. Now look at the code segment given below.
Dim x, y As Integer
x = 5
y = 5
If x = y Then
MessageBox.Show (“Equal”)
MessageBox.Show (“x is equal to y”)
End If
Execution of the above code segment displays two message
boxes each with texts “Equal” and “x is equal to y” respectively.
Look at the code segment given below that demonstrates the usage of
If…Then…Else statement.
Dim x, y As Integer
x = 5
y = 5
3. Lesson 3 of 8 Visual Basic Programming
3 of 4
If x = y Then MessageBox.Show (“Y”) Else MessageBox.Show (“N”)
Execution of the above code displays “Y” in a message box
since the condition is true. Execution of the code given below
displays “N” in a message box since the condition is false.
Dim x, y As Integer
x = 5
y = 6
If x = y Then MessageBox.Show (“Y”) Else MessageBox.Show (“N”)
If…Then…Else…End If statement should be used when there
are more than a single line of code to execute if the condition is either
true or false. Look at the code segment given below.
Dim x, y As Integer
x = 5
y = 5
If x = y Then
MessageBox.Show (“Equal”)
MessageBox.Show (“x is equal to y”)
Else
MessageBox.Show (“Not equal”)
MessageBox.Show (“x is not equal to y”)
End If
Execution of the above code displays two message boxes each
with texts “Equal” and “x is equal to y” respectively as the condition
is true. Now look at the code segment given below.
4. Lesson 3 of 8 Visual Basic Programming
4 of 4
Dim x, y As Integer
x = 5
y = 6
If x = y Then
MessageBox.Show (“Equal”)
MessageBox.Show (“x is equal to y”)
Else
MessageBox.Show (“Not equal”)
MessageBox.Show (“x is not equal to y”)
End If
Execution of the above code segment displays two message
boxes with texts “Not equal” and “x is not equal to y” respectively as
the condition is false.
If…Then…ElseIf…Else…End If statement can be used when
there are multiple conditions. Look at the code segment given below.
Dim x As Integer = 18.76
If x < 10 Then
MessageBox.Show (“x < 10”)
ElseIf x < 20 Then
MessageBox.Show (“10 <= x < 20”)
ElseIf x < 30 Then
MessageBox.Show (“20 <= x < 30”)
Else
MessageBox.Show (“x >= 30”)
End If