3. Conditions are logical expressions that evaluate to a
True/False value and they usually contain comparison
operators— equals (=), different (<>), less than (<), greater
than (>), less than or equal to (<=), and so on — and logical
operators: And, Or, X or, and Not. Here are a few examples
of valid conditions:
4. If (age1 < age2) And (age1 > 12) Then ...
If score1 = score2 Then ...
5. FORMS OF IF STATEMENT
1. if … statement then 1 condition
2. if … statement has Else part
3. if statement has one or more Else if parts
6. If … then statement
The If. . .Then statement tests an expression, which
is known as a condition. If the condition is True, the
program executes the statement(s) that follow.
The If. . .Then statement can have a single-line or a
multiple-line syntax. To execute one statement
conditionally, use the single-line syntax as follows:
If condition Then
statement is TRUE
End if
7. Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If TextBox1.Text = "Hello!" Then Label1.Text =
"World"
End Sub
8. Another example:
Dim a As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
a = TextBox1.Text
If a >= 18 Then label1.text ("You can now vote
online.")
End Sub
9. 2. if … statement has Else part
If the condition is evaluates correct, then the
statement are executed.
If the condition is evaluates false, then the
statements are executed.
Syntax:
If condition Then
statement is True
Else
statement is False
End if
10. Dim a As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
a = TextBox1.Text
If a >= 18 Then Label1.Text = "You can now
vote online."
Else Label1.Text = "You can not vote“
End Sub
11. Another Example:
(Using Boolean/Logical Operation)
If TextBox1.Text = “Apple" And / Or
TextBox2.Text = “Banana"
Then Label1.Text = "Fruit“
Else Label1.Text = “Junk Food"
12. 3. if statement has one or more Elseif parts
One or more statements and condition.
Syntax:
If Condition1 then
statements to be if condition is true
Else if condition2 then
statements to be executed is False but Condition2 is True
Else if condition3 then
statements to be executed if Con1 and Con2 are False but
Condition3 is True
Else
statements to be executed if all of the above conditions are False.
End Sub
13. Dim Grade as Integer
Grade = txtgrade.Text
If Grade <= 74 Then MsgBox("You Failed!") Else If
Grade <= 80 Then MsgBox("Needs Improvement!") Else If
Grade <= 85 Then MsgBox("Average!") Else If Grade <= 90
Then MsgBox("Satisfactory!") Else If Grade <= 95 Then
MsgBox("Very Satisfactory!") Else MsgBox("Excellent!")
End Sub
14. Note:
Aside from Message box, you may also use the
controls such as label and textbox, if you want to
show the executed statements or condition.