If the conditional statement is long, you can write the If...Then expression one line and the statement on the next line but then you must indicate the end of the conditional statement with End If. It is also a good idea (to make your code easy to read) to indent the statement. The formula to follow is:

You can also write the statement on its own line even
if the statement is short enough to fit on the same line with the If...Then expression. If the statement contains, or must use, more that one line of code, you must create the statements in the line next to the If...Then expression. Here is an example:

An If...Then...Else conditional statement can process only two statements. If you need to process more
conditions, you can add an ElseIf condition. The formula to follow is:

If Condition1 Then
statement1
ElseIf condition2 Then
statement2
End If

The first condition, condition1, would be checked. If
condition1 is true, then statement1 would execute. If condition1
is false, then condition2 would be checked. If condition2
is true, then statement2 would execute. Any other result would be
ignored.

If you need to process more than two conditions, you can
add more ElseIf sections. The formula to follow would be:

If condition1 Then
statement1
ElseIf condition2 Then
statement2
ElseIf condition3 Then
Statement3
. . .
ElseIf condition-n Then
Statement-n
End If

The Visual Basic language provides a comparison operator
used to compared two objects of classes. The operator is named Is. The formula to use it is:

comparison-result = object1Isobject2

The Is operator compares the references of two
objects, not the values of the objects. If the references point to the same
object, the comparison produces a True result. Otherwise, the comparison is
False.

Something Is Not

To let you find out if two objects are not the same, the Visual Basic language provides
the IsNot comparison operator. The formula to use it is:

comparison-result = object1IsNotobject2

The IsNot operator compares the references of two
objects. If the objects are the same, the comparison produces True result; otherwise,
it produces
False.

If Something Is Nothing

Remember that when an object is created, if the New keyword
is not used to allocate memory for it, the object is said to be Nothing.
Also remember that, if an object was already difined, if for some or any reason you
want to undefine it, you can assign Nothing to it. Here is an
example we saw in Lesson 07:

At any time, to find out whether an object is
currently defined or it is not holding a clear value, use the Is operator to compare it to the Nothing object.
Here are examples:

<%@ Page Language="VB" %>
<!DOCTYPE html>
<html>
<head>
<script runat="server">
Class Employee
End Class
</script>
<title>Department Store - Employee Record</title>
</head>
<body>
<h4>Department Store - Employee Record</h4>
<%
Dim staff As New Employee
Response.Write("Dim staff As New Employee")
If staff Is Nothing Then
Response.Write("<br>The staff member is currently not defined.")
Else
Response.Write("<br>The staff member has a defined object.")
End If
staff = Nothing
Response.Write("<br><br>staff = Nothing")
If staff Is Nothing Then
Response.Write("<br>The staff member is currently not defined.")
Else
Response.Write("<br>The staff member has a defined object.")
End If
%>
</body>
</html>

This would produce:

The If Operator for Nothing

To provide a simple way to find out if a value or an object
is Nothing, the If operator provides the following formula:

If(object/value is nothing, object/value-result)

If the first argument is nothing, the second argument is
produced.

Going To a Statement

In the flow of your code, you can jump from one statement or line to another. This is made possible with the GoTo keyword. Before using it, first create or insert a name on a particular section of your procedure or function. The name, also called a label, is made of one word and it can be anything. That name is followed by a colon ":". Here
is an example:

<script language="VB" runat="server">
Sub btnCalculateClick(ByVal sender As Object, ByVal e As EventArgs)
' Do some thing(s) here
RegularTime:
' Do some other thing(s) here
End Sub

After creating the label, if a condition calls to jump to the label, type GoTo followed by the label. In the same way, you can create as many labels as you
want. The name of each label must be unique in the same procedure. Here is an example:

As you should be aware by now, Boolean algebra stands by two
values, True and False, that are opposite each other. In fact, Visual Basic provides an
operator that can be used to reverse a Boolean value or to negate a Boolean
expression. This is the role of the Not operator. The formula to use it is:

NotExpression

To use the operator, type Not followed by a
logical expression. The expression can be a simple Boolean value. Here is
an example:

In this case, the Not operator is used to change the
logical value of the variable. When a Boolean variable has been "notted", its logical value has
changed. If the logical value was True, it would be changed to False
and vice versa. Therefore, you can inverse the logical value of a Boolean variable
by "notting" or not "notting" it. Here is an example:

<script runat="server">
Sub btnFindClick(ByVal sender As Object, ByVal e As EventArgs)
Dim chemicalSymbol As String
If Not txtChemicalSymbol.Text = "" Then
chemicalSymbol = txtChemicalSymbol.Text
End If
End Sub
</script>

To make the code easier to read, it is a good idea to pu the
negative expression in parentheses

When performing its assignment, a function can encounter
different situations, some of which would need to be checked for
a true value, a false value or a negation. Therefore, a conditional statement can
assist a procedure or function to decide what action to take or what value to
produce.

Conditional Returns

A function can return only one value but you can make it
produce a result
that depends on some condition. Here are two examples:

In our examples, we first declared a variable that was going
to hold the value to return. This is not always necessary. If you already have
the value or the expression to return, you can precede it with the Return. You
can repeat this for each section where the value or expression can/must be
returned. Here are examples:

The Visual Basic language contains many functions that can assist you in dealing with conditional statements. Also many classes have built-in methods that produce a Boolean value. For example, the class of every object used in Visual Basic is equipped with a method named Equals. It can be used to find out if one object or value is equal to another.

If a Value is Numeric

Remember that any value that a visitor enters in a text box is primarily considered as text. Before using or converting a value that is supposed to be numeric, to let you checke whether it is a number, Visual Basic provides a function named IsNumeric. This function takes one argument as the value to check. If the argument is an integer or a floating-point number, the function returns True. If not, it returns False. Here is an example:

When you declare a variable, especially when you create an object (declaring a variable from a class), you should initialize, but you can declare a variable without immediately initializing. This technique creates the risk of using a variable that has not yet been initializing, which can cause errors sometimes. To let you find out if a variable has been previously initialized or whether the variable currently holds a significant value, the Visual Basic library provides a function named IsNothing. This function takes one argument as the object or the expression to check. The IsNothing() function produces a True result if the argument holds a value. Otherwise, it returns False.

If a Variable is a Reference Type

As you probably know already, when you declare a variable from a class, the variable is stored in an area of computer referred to as the heap. Such a variable is referred to as a reference type. At any time, to let you check whether a variable is a reference type, Visual Basic provides a function named IsReference. This function takes one argument that is the object or an expression to check.

The Condition-Based Function

To assist you with checking a condition and its alternative,
the Visual Basic language provides a function named IIf. This function operates like an If...Then...Else conditional statement. It takes three required arguments and returns a result of a type of your choice (actually an Object). It can be presented as follows:

Public Function IIf(ByVal Expression As Boolean,
ByVal TruePart As Object,
ByVal FalsePart As Object) As Object

The condition to check, a Boolean expression, is passed as the first argument:

If that Expression is true, the function returns the value of the second argument, the TruePart. The third argument or last argument is ignored

If the Expression is false, the first argument is ignored and the function returns the value of the second argument