0% found this document useful (0 votes)
65 views5 pages

Visual Basic Activities - If Then Else

This document contains 4 examples that demonstrate using If/Else and Select Case statements to evaluate conditions and output corresponding messages or values. Example 1 shows using If/Else to check if a number is greater than or equal to 100 to win a prize. Example 2 adds a second condition to check age as well. Example 3 uses ElseIf to evaluate a text box value and assign a letter grade. Example 4 demonstrates Select Case for assigning grade descriptions based on an examination mark.

Uploaded by

Tresita B. Rios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views5 pages

Visual Basic Activities - If Then Else

This document contains 4 examples that demonstrate using If/Else and Select Case statements to evaluate conditions and output corresponding messages or values. Example 1 shows using If/Else to check if a number is greater than or equal to 100 to win a prize. Example 2 adds a second condition to check age as well. Example 3 uses ElseIf to evaluate a text box value and assign a letter grade. Example 4 demonstrates Select Case for assigning grade descriptions based on an examination mark.

Uploaded by

Tresita B. Rios
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Example 13.

2
We modified the code in Example 13.1 by deleting the second If statement and use the
Else keyword instead. When you run the program and enter a number that is greater
than 100, the me=ssage “Congratulation! You win a lucky prize” will be
shown.Otherwise, you will see the "Sorry, You did not win any prize" message, as
shown in Figure 13.3

The code
Private Sub OK_Click(sender As Object, e As EventArgs)
Handles OK.Click
Dim myNumber As Integer
myNumber = TxtNum.Text
If myNumber >= 50 And myNumber < 100 Then
MsgBox( "Congratulation! You win a lucky prize")
Else
MsgBox( "Sorry, You did not win a lucky prize")
End If
End Sub

Example 13.3
This program involves using two variables and two conditions. Both conditions must be
met otherwise the second block of code will be executed. In this example, the number
entered must at least 100 and the age must be at least 60 in order to win a lucky prize,
any one of the above conditions not fulfilled will disqualify the user from winning a prize.
You need to add another text box for the user to enter his or her age. The output is as
shown in Figure 13.4

Private Sub OK_Click(sender As Object, e As EventArgs)


Handles OK.Click
Dim myNumber, MyAge As Integer
myNumber = TxtNum.Text
MyAge = TxtAge.Text

If myNumber >= 100 And MyAge >=60 Then


MsgBox(" Congratulation! You win a lucky prize")
Else
MsgBox("Sorry, You did not win any prize")
End If
End Sub
Example 13.4
This program can compute the grade for the mark entered by the user. We use several
ElseIf statements and the logical operator And to achieve the purpose. The outputs are
as shown in Figure 13.5 and Figure 13.6

Private Sub OK_Click(sender As Object, e As EventArgs)


Handles OK.Click
Dim Mark As Integer
Dim Grade As String
Mark = TxtMark.Text
If Mark >= 80 And Mark <= 100 Then
Grade = "A"
ElseIf Mark >= 60 And Mark < 80 Then
Grade = "B"
ElseIf Mark >= 40 And Mark < 60
Grade = "C"
ElseIf Mark >= 0 And Mark < 40
Grade = "D"
Else
Grade = "Out of Range"
End If
MsgBox("You Grade is " & Grade)
End Sub

14.2 The usage of Select Case is shown in the


following examples
Example 14.1: Examination Grades
In this example, the program will display a message associated with the grade entered
by the user.

The Code
Private Sub BtnShow_Click(sender As Object, e As EventArgs)
Handles BtnShow.Click
Dim grade As String
grade = TxtGrade.Text
Select Case grade
Case "A"
MsgBox("High Distinction")
Case "A-"
MsgBox("Distinction")
Case "B"
MsgBox("Credit")
Case "C"
MsgBox("Pass")
Case Else
MsgBox("Fail")
End Select
End Sub
The Output

Example 14.2
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles
Button1.Click’Examination Marks
Dim mark As Single
mark = mrk.Text
Select Case mark
Case Is >= 85
MsgBox( "Excellence")
Case Is >= 70
MsgBox( "Good")
Case Is >= 60
MsgBox( "Above Average")
Case Is >= 50
MsgBox( "Average")
Case Else
MsgBox( "Need to work harder")
End Select
End Sub
Example 14.3
Example 14.2 can be rewritten as follows:

Private Sub Button1_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button1.Click

‘Examination Marks
Dim mark As Single
mark = Textbox1.Text
Select Case mark
Case 0 to 49
MsgBox( "Need to work harder")
Case 50 to 59
MsgBox( "Average" )
Case 60 to 69
MsgBox( "Above Average")
Case 70 to 84
MsgBox( "Good")
Case 85 to 100
MsgBox("Excellence")
Case Else
MsgBox( "Wrong entry, please reenter the mark")
End Select
End Sub
Example 14.4
Grades in high school are usually presented with a single capital letter such as A, B, C,
D or E. The grades can be computed as follow:

Private Sub Button1_Click(ByVal sender As System.Object,


ByVal e As System.EventArgs) Handles Button1.Click
‘Examination Marks
Dim mark As Single
mark = TextBox1.Text
Select Case mark
Case 0 To 49
LblGrade.Text = "E"
Case 50 To 59
LblGrade.Text = "D"
Case 60 To 69
LblGrade.Text = "C"
Case 70 To 79
LblGrade.Text = "B"
Case 80 To 100
LblGrade.Text = "A"
Case Else
LblGrade.Text = "Error, please re-enter the mark"
End Select
End Sub
The output

Figure 14.3

You might also like