Introduction to Conditions

Comparison Operators

Introduction

Logical comparisons make it possible to find out if two values are the same, if they are different, if one is greater or smaller than the other, etc. A comparison can be performed on two variables, in which case their values would be tested. The comparison can be performed on a variable and a constant value. In both cases, the variable(s) and the value must be of the same type or they must be compatible.

The True and False Constants

Boolean algebra uses two constants to indicate its values. If a comparison is valid, it produces a value as True. If not, it produces a value of False.

Equality =

To compare two variables for equality, use the = operator. The formula to follow is:

`value1 = value2`

The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. The comparison can be performed on strings or numbers. The comparison can be performed as follows:

Inequality <>

To let you find out if two variables, or a variable and a constant, are not equal, the Visual Basic language provides the <> operator. Its formula is:

`value1 <> value2`

The comparison can be illustrated as follows:

The comparison can be performed on strings or numbers.

A Lower Value <

To find out whether one value is lower than another, use the < operator. The formula to follow is:

`value1 < value2`

If the value held by the left operand is lower than that of the right operand, the comparison produces a True result. The comparison can be resumed as follows:

The comparison can be performed on numbers.

Less Than Or Equal: <=

To combine the comparison for equality and lower value, you can use the <= operator. The formula to follow is:

`value1 <= value2`

If both operand hold the same value, or the left operand is lower than the right operand, the result is true. Otherwise it is false. This can be illustrated as follows:

The comparison can be performed on numbers.

Greater Value >

To find out whether one value is greater that another, use the > symbol. Its formula is:

`value1 > value2`

The comparison can follow a logic as follows:

The comparison can be performed on numbers.

Greater or Equal Value >=

The greater than or the equality operators can be combined to produce an operator as follows: >=. This is the "greater than or equal to" operator. It uses the following formula:

`value1 >= value2`

The comparison can be illustrated as follows:

The comparison can be performed on numbers.

Fundamentals of Boolean Values

The Boolean Data Type

A value is referred to as Boolean if it can be either true or false. Like a number or a string, a Boolean value can be stored in a variable. To declare such a variable, use the Boolean data type. Here is an example:

```<%
Dim employeeIsMarried As Boolean
%>```

To actually use a Boolean variable, you can assign a value to it. By default, if you declare a Boolean variable but do not initialized it, it receives a value of False. Otherwise, to initialize a Boolean variable, assign it a True or a False value.

To convert a value to Boolean, you can use CBool().

Initializing a Boolean Variable

To initialize or specify the value of a Boolean variable, assign it True or False. Here is an example:

```<%
Dim drinkingUnderAge As Boolean

drinkingUnderAge = True
%>```

A Boolean variable can also be initialized with a Boolean expression. Here is an example:

```<%
Dim age As Integer
Dim drinkingUnderAge As Boolean

age = 21

drinkingUnderAge = age >= 21
%>```

In this case, it is a good idea to put the Boolean expression in parentheses. Here is an example:

```<%
Dim age As Integer
Dim drinkingUnderAge As Boolean

age = 21

drinkingUnderAge = (age >= 21)
%>```

At any time and when you judge it necessary, you can change the value of the Boolean variable by assigning it a True or False value.

A Boolean Value as a Number

To In the Visual Basic language, a Boolean variable can also deal with numeric values. The False value is equivalent to 0. For example, instead of False, you can initialize a Boolean variable with 0. Any other numeric value, whether positive or negative, corresponds to True. Consider the followin example:

```<%@ Page Language="VB" %>

<!DOCTYPE html >

<head>
<title>Employment Application</title>
</head>
<body>
<div align="center">
<h3>Exercise</h3>
<%
Dim employeeIsMarried As Boolean

employeeIsMarried = 13708

Response.Write("Employee Is Married? " & employeeIsMarried)
%>
</div>
</body>
</html>```

The number can be decimal or hexadecimal:

Boolean as a Data Type

Passing a Boolean Variable as Argument

A Boolean variable can be passed to a procedure and/or a function can be made to return a Boolean value. To pass an argument as a Boolean value, in the parentheses of the procedure, type the name of the argument followed by the As Boolean expression. Here is an example:

```<script Language="VB" runat="server">
Sub CheckingEmployee(ByVal IsFullTime As Boolean)

End Sub
</script>```

In the same way, you can create a procedure or function with as many Boolean parameters as you need, and you can combine Boolean and non-Boolean parameters as you judge necessary. Then, in the body of the procedure, use (or don't use) the Boolean parameter(s).

Returning a Boolean Value

Just as done for the other data types, you can create a function that returns a Boolean value. When declaring the function, specify its name and the As Boolean expression on the right side of the closing parenthesis. Here is an example:

```<script Language="VB" runat="server">
Public Function IsDifferent() As Boolean

End Function
</script>```

Of course, the function can take arguments of any kind you judge necessary

In the body of the function, do whatever you judge necessary. Before exiting the function, you must return a value that evaluates to True or False.

A Boolean Member in a Class

A member variable of a class can be a Boolean type. Here is an example:

```<script runat="server">
Public Class House
public Bathrooms As Single
public HasCarGarage As Boolean
public MarketValue As Decimal
End Class
</script>```

Like parameters of the other types, you can create a method that uses a Boolean type as parameter.