Thursday, December 11, 2008

USING THE IF CONTROL STRUCTURE WITH THE COMPARISON OPERATORS

To effectively control the VB program flow, we shall use the If control structuret together with the conditional operators and logical operators. There are basically three types of If control structure, namely If....Then statement, If....Then... Else statement and If....Then....ElseIf statement.

1. If....Then Statement
This is the simplest control structure which ask the computer to perform a certain action specified by the VB expression if the condition is true. However, when the condition is false, no action will be performed. The general format for the if...then.. statement is

If condition Then

VB expression

End If

Example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumber As Integer
myNumber = TextBox1.Text
If myNumber > 100 Then
Label2.Text = " You win a lucky prize"
End If
End Sub

*) When the you run the program and enter a number that is greater than 100, you will see the "You win a lucky prize" statement. On the other hand, if the number entered is less than or equal to 100, you don't see any display.

2. If....Then...Else Statement
Using jus If....Then statement is not very useful in programming and it does not provides choices for the users. In order to provide a choice, we can use the If....Then...Else Statement. This control structure will ask the computer to perform a certain action specified by the VB expression if the condition is true. And when the condition is false ,an alternative action will be executed. The general format for the if...then.. Else statement is

If condition Then

VB expression

Else

VB expression

End If

Example 1:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumber As Integer
myNumber = TextBox1.Text
If myNumber > 100 Then
Label2.Text = " Congratulation! You win a lucky prize"
Else
Label2.Text = " Sorry, You dif not win any prize"
End If
End Sub

* When the you run the program and enter a number that is greater than 100, you will see the "Congratulation! You win a lucky prize" statement. On the other hand, if the number entered is less than or equal to 100, you will see the "Sorry, You dif not win any prize" statement

 
Example 2:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myNumber, MyAge As Integer
myNumber = TextBox1.Text
MyAge = TextBox2.Text

If myNumber > 100 And myAge > 60 Then
Label2.Text = " Congratulation! You win a lucky prize"
Else
Label2.Text = " Sorry, You dif not win any prize"
End If

End Sub

*) This program use the logical And operator beside the conditional operators. This means that the both conditions must be fulfilled in order for the conditions to be true, otherwise the second block of code will be executed. In this example, the number entered must be more than 100 and the age must be more than 60 in order to win a lucky prize, any one of the above conditions not fulfilled will disqualify the user from winning a prize.

3. If....Then...ElseIf Statement
If there are more than two alternative choices, using jus If....Then....Else statement will not be enough. In order to provide more choices, we can use the If....Then...ElseIf Statement. executed. The general format for the if...then.. Else statement is

If condition Then

VB expression

ElseIf condition Then

VB expression

ElseIf condition Then

VB expression

.

.

Else

VB expression

End If


Example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Mark As Integer

Dim Grade as String

Mark = TextBox1.Text
If myNumber >=80 Then
Grade="A"

ElseIf Mark>=60 and Mark<80 then

Grade="B"

ElseIf Mark>=40 and Mark<60 then

Grade="C"

Else

Grade="D"

End If
End Sub

No comments:

Post a Comment