Visual Basic Simple & Block If Statement

Like all other programming languages Visual Basic makes use of the common control structures: Sequence, Selection, and Iteration.

A common implementation of the selection structure is the If statement. With an If statement you may compare the value of a variable, property, constant or expression to another variable, property, constant, or expression.

In Visual Basic

You can use simple selection

If condition Then statement

OR

A block If statement.     The word Then is required

If condition Then
         statement
         statement
         statement
End If          'End If is two words

For example

If txtinput.Text = "" Then
         MessageBox.Show(
"you must enter a value in the TextBox")
End If

The If statement uses a relational operator to compare two operands. The two operands should be of the same data type. You should only compare numbers to numbers and strings to strings.
 
The standard relational operators are

 
=             Equal to
>             Greater than
<             Less than
< >          Not equal to
>=           Greater than or Equal to
<=           Less than or Equal to

When comparing numeric values the result of comparisons are generally what you would expect.

5 is < 10

.01 is > .001

and 1000 is = 1000

When comparing two string type operands the comparison is based on the ASCII coding scheme where

 
A condition is an expression. Like all expressions conditions are evaluated and return a single value. In Visual Basic 2010 if a condition is True it returns a -1, If it is False it returns a 0 (zero).
 
In an If statement if the expression (which is often but not always a condition) evaluates to anything other than 0 it is considered True.
 
Therefore the code fragment

If 6 Then
        MsgBox(
“this is true”)
End If

Is syntactically correct and will display “this is true”
 
This form of the If statement can be used with Boolean variables. A Boolean variable is declared as type Boolean and is intended to reflect the absence or presence of a condition. They are used as flags. Boolean variable maybe  True or False
 
 
An Example:

Dim blnFlag As Boolean
:
:
:
If blnFlag Then       ‘blnFlag is either True or False
       MsgBox(
”the flag is on”)
Else
       MsgBox(
“the flag is off”)
End If