IF STATEMENT IN VISUAL BASIC 2010


IF STATEMENT

The if statement is another conditional operator. We have already met the example below in the above example.

   If TextBox1.Text = “” Then

            MsgBox(“enter marks in student marks box”)

        End If

 

Looking at the code.

 

If TextBox1.Text = “” Then

 



This statement tells vb to check if there is anything in textbox1

If there is nothing then

MsgBox(“enter marks in student marks box”)

A message box should pop up telling the user to enter marks in the textbox labeled student marks. If there is something entered then the code that follows would be executed. By the way you can try that by running the program and clicking the equal’s button when textbox1 is empty.

Remember that “” (two double quotes) means empty.

The syntax of if statement is as follows

If [Variable to be tested] [condition] Then

Statements to be executed

ElseIf [Variable to be tested] [condition] Then

Statements to be executed

ElseIf [Variable to be tested] [condition] Then

Statements to be executed

ElseIf [Variable to be tested] [condition] Then

Statements to be executed

ElseIf [Variable to be tested] [condition] Then

Statements to be executed

 Else

Statements to be executed

End If

Conditional operators.

= this means that something is equal to.

< this means that something is less than and not equal to

> this means that something is more than and not equal to

<= less than and equal to.

>= greater than and equal to.

 

EXERCISE

Create another project with a user interface like the one for select case.

IF1

Double click  the equals button and enter the following code.

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

Dim marks As Integer

        Dim grade As String

        If TextBox1.Text = “” Then

            MsgBox(“enter marks in student marks box”)

        Else

            marks = TextBox1.Text

            If marks < 0 Then

                grade = “ERROR”

            ElseIf marks <= 20 Then

                grade = “E”

            ElseIf marks <= 40 Then

                grade = “D”

            ElseIf marks <= 60 Then

                grade = “C”

            ElseIf marks <= 80 Then

                grade = “B”

            ElseIf marks <= 100 Then

                grade = “A”

            Else

                grade = “ERROR”

            End If

        End If

        TextBox2.Text = grade

 

    End Sub

Your code window should now look as follows.

IF2

Try and run the program. It should work just like the other program for select case.

Let us now look at the code.

 

Dim marks As Integer

Dim grade As String

We are declaring to variables marks of type integer and grade of type string

 

 If TextBox1.Text = “” Then

            MsgBox(“enter marks in student marks box”)

 

We are testing if there is anything in the textbox1 and if there is nothing the user is prompted to enter marks using a message box.

  Else

we are telling vb if there is something in textbox1 to execute the code that follow.

marks = TextBox1.Text

we are telling vb that to pass what is in textbox1 to the variable marks.

If marks < 0 Then

We are then starting to test this variable

We are telling vb to check if it is less than 0: remember not equal to 0

grade = “ERROR”

we are telling vb to assign ERROR to variable grade if the above condition is met.

ElseIf marks <= 20 Then

 

we are telling vb to test for another condition if the earlier one was not met.

grade = “E”

we are telling vb to assign E to variable grade if this condition is met.

ElseIf marks <= 40 Then

We are telling vb to perform another test if the earlier conditions have not been met.

grade = “D”

we are telling vb to assign D to variable grade if this condition is now met. The same idea is repeated for the other else ifs.

 ElseIf marks <= 60 Then

                grade = “C”

    ElseIf marks <= 80 Then

                grade = “B”

            ElseIf marks <= 100 Then

                grade = “A”

 

            Else

if all the other conditions are not true then this one is executed.

grade = “ERROR”

End If

 

This marks the end of if statement oh sorry! The inner if.

 

End If

This marks the end of the other if that was the first one.

TextBox2.Text = grade

We are then passing the variable grade to textbox2.

NOTE

  1. The if else if statements are executed sequentially and once a condition is met the other elseif statements are not executed.
  2. it is possible to use nested if’s as shown in the above example.(more than one if together) in this case remember the end if is always for the nearer if.