Conditional operators in visual basic 2010



Conditional operators

These are always in our lives in daily basis and a choice as to be made between several options. Consider a situation where an employee continuously  misses on the job for several days. The employer may decide to suck the employee depending on the number of  days that the employee as failed to attend the job.

We have to major conditional statements.

  1. Select case
  2. If statement.

SELECT CASE

This takes the format

Select Case {condition to be tested} no brackets.

            Case 1

               

            Case 2

                

               

            Case Else

               

        End Select

Note: the cases can be as many as possible

Example

Create a new project and give it any name.

Create a user interface as shown below.

case1

A label was used to label the textbox at the top with number of days.

Double click the action taken button and enter the following code

Dim days As Integer

        days = Val(TextBox1.Text)

        Select Case days

            Case 0

                MsgBox(“you are a good servant”)

  Case 1

                MsgBox(“you have a warning letter”)

            Case 2

                MsgBox(“you have a  second warning letter”)

            Case Else

                MsgBox(“you are fired”)

        End Select

Your code window should now look like this.

case2

Run your program and enter a number in the textbox and click the action button.

As an example below.

case3

The results would be.

case4

Try using other values that are specified in the code eg 0, 2, 3. and note the results.

You can also write a code that grades the marks of students in a similar way.

Exercise.

Design a similar project like the one above and add another textbox to the form as shown below.

case5

The student marks is supposed to be entered in student marks and the grade displayed once the equals button is clicked.

Enter the code below

Dim marks As Integer

        Dim grade As String

        If TextBox1.Text = “” Then

            MsgBox(“enter marks in student marks box”)

        End If

        marks = Val(TextBox1.Text)

        Select Case marks

            Case 0 To 20

 

                grade = “E”

 

            Case 21 To 40

 

                grade = “D”

 

            Case 41 To 60

 

                grade = “C”

            Case 61 To 80

 

                grade = “B”

 

            Case 81 To 100

 

                grade = “A”

 

            Case Else

                grade = “ERROR”

        End Select

        TextBox2.Text = grade

 

    End Sub

Your code window should now be like this one.

case6

Try out your program press f5 from the keyboard and enter any value in the student marks box. Then click equals button. Did it work? Yes it worked.



Let us now analyze this code.

Dim marks As Integer

We are declaring a variable marks of type integer

Dim grade As String

We are declaring a variable grade of type string.

For now we are going to skip the if statement.

If TextBox1.Text = “” Then

MsgBox(“enter marks in student marks box”)

End If

So we move to

marks = Val(TextBox1.Text)

we are telling vb that the value of marks is in textbox1.

Select Case marks

We want now to test for certain conditions.

Case 0 To 20

            grade = “E”

 

We are telling vb if the marks are 0 to 20 then the grade is E as indicated in the next statement

Case 21 To 40

            grade = “D”

We are telling vb if the marks are 21 to 40 then the grade is D as indicated in the next statement.

Then we continue testing all the other values as shown below.

 

Case 41 To 60

                grade = “C”

Case 61 To 80

                grade = “B”

 

Case 81 To 100

                grade = “A”

Case Else

                grade = “ERROR”

       End Select

 

End select ends our testing.

Then we are telling vb to display the value of grade in the textbox2 as indicated below

TextBox2.Text = grade

End Sub

Means the end of the our procedure.