Home

Introduction to Conditions

 

Introduction to Boolean Values

 

Introduction

A Boolean value is one that can have either a True or a False value. In the Visual Basic language, a Boolean value can also be a numeric value, either 0 or a positive/negative value. Boolean values are usually valuable when making comparisons.

The Boolean Data Type

Like a number or a string, a Boolean value can be stored in a variable. To declare such a variable, use the Boolean keyword. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim EmployeeIsMarried As Boolean
%>

</body>
</html>

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:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim EmployeeIsMarried As Boolean

    Response.Write("Employee Is Married? " & EmployeeIsMarried)
%>

</body>
</html>

This would produce:

Boolean Value

To initialize a Boolean variable, assign it a True or a False value. 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:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim EmployeeIsMarried As Boolean

    EmployeeIsMarried = -792730
    Response.Write("Employee Is Married? " & EmployeeIsMarried)
%>

</body>
</html>

The number can be decimal or hexadecimal:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim EmployeeIsMarried As Boolean

    EmployeeIsMarried = &HFA26B5
    Response.Write("Employee Is Married? " & EmployeeIsMarried)
%>

</body>
</html>

Boolean Values and Procedures

 

Introduction

Boolean values can be involved with procedures. This means that a Boolean variable can be passed to a procedure and/or a function can be made to return a Boolean value.

Passing a Boolean Variable as Argument

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:

Private Sub CheckingEmployee(ByVal IsFullTime As Boolean)

End Sub

In the same way, you can pass as many Boolean arguments as you need, and you can combine Boolean and non-Boolean arguments as you judge necessary. Then, in the body of the procedure, use (or do not use) the Boolean argument as you wish.

Returning a Boolean Value

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 parentheses. Here is an example:

Public Function IsDifferent() As Boolean
    
End Function

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

Public Function IsDifferent(ByVal Value1 As Integer, _
                                ByVal Value2 As Integer) As Boolean
    
End Function

Before exiting the function, you must return a value that evaluates to True or False. We will see an example below.

Boolean Built-In Functions

 

Converting a Value to Boolean

To convert a value to Boolean, you can use the CBool() function. Its syntax is:

Function CBool(ByVal Expression As Object) As Boolean

The CBool function takes as argument the expression to be evaluated. It should produce a valid Boolean value. If it does, the function returns True or False.

Checking Whether a Value is Numeric

One of the most valuable operations you will perform on a value consists To find out whether a value is numeric or not, you can call the IsNumeric() function. Its syntax is:

Public Function IsNumeric(ByVal Expression As Object) As Boolean

This function takes as argument the value or expression to be evaluated. If the argument holds or can produce a valid integer or a decimal value, the function returns True. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Value As Object

    Value = 258.08 * 9920.3479

    Response.Write("Is Numeric? " & IsNumeric(Value))
%>

</body>
</html>

This would produce:

Is Numeric

If the argument is holding any other value that cannot be identified as a number, the function produces False. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Value As Object

    Value = #12/4/1770#
    Response.Write("Is Numeric? " & IsNumeric(Value))
%>

</body>
</html>

Checking for Valid Date/Time

To find out whether an expression holds, or doesn't hold, a valid date, a valid, you can call the IsDate() function. Its syntax is:

Public Function IsDate(ByVal Expression As Object) As Boolean

This function takes an argument as the expression to be evaluated. If the argument holds a valid date and/or time, the function returns True. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim DateHired As Object

    DateHired = "9/16/2001"
    Response.Write("Is Date? " & IsDate(DateHired))
%>

</body>
</html>

This would produce:

Is Date

If the value of the argument cannot be evaluated to a valid date or time, the function returns False. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim DateHired As Object

    DateHired = "Who Knows?"
    Response.Write("Is Date? " & IsDate(DateHired))
%>

</body>
</html>

Checking for Nothing

To find out whether a variable or an expression holds a Nothing value, you can call the IsNothing() function. Its syntax is:

Public Function IsNothing(ByVal Expression As Object) As Boolean

When calling this function, you can pass it a value or an expression. If the argument holds a valid value, this function returns False. If the argument does not hold a value, this function produces True.

 
 
 
 

Logical Operators

 

Introduction

A comparison is an operation used to get the Boolean result of two values one checked against the other. Such a comparison is performed between two values of the same type.

Equal: =

To compare two variables for equality, use the = operator. Its syntax is:

Value1 = Value2

The comparison for equality

Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim IsFullTime As Boolean

    Response.Write("Is Employee Full Time? " & IsFullTime)
    Response.Write("<br />")

    IsFullTime = True
    Response.Write("Is Employee Full Time? " & IsFullTime)
%>

</body>
</html>

This would produce:

Comparison

Not Equal: <>

To check for inequality between two values or expressions, you can use the <> operator. Its formula is:

Variable1 <> Variable2

The comparison for inequality

Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<script language="vbscript" type="text/vbsscript" runat="server">

Public Function IsDifferent(ByVal Value1 As Integer, _
                            ByVal Value2 As Integer) As Boolean
    Return (Value1 <> Value2)
End Function

</script>
<title>Exercise</title>

</head>
<body>

<%
    Dim a%, b%
    Dim Result As Boolean

    a% = 12 : b% = 48
    Result = IsDifferent(a%, b%)

    Response.Write("The resulting comparison of 12 <> 48 is " & Result)
%>

</body>
</html>

This would produce:

Inequality

Less Than: <

To find out whether one value is lower than another, use the < operator. Its syntax is:

Value1 < Value2

Flowchart: Less Than

Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim PartTimeSalary, ContractorSalary As Double
    Dim IsLower As Boolean

    PartTimeSalary = 20.15
    ContractorSalary = 22.48
    IsLower = PartTimeSalary < ContractorSalary

    Response.Write("Part Time Salary:  " & PartTimeSalary & "<br />" & _
                   "Contractor Salary: " & ContractorSalary & "<br />" & _
                   "Is PartTimeSalary < ContractorSalary? " & IsLower)
    Response.Write("<br />----------------------------------<br />")

    PartTimeSalary = 25.55
    ContractorSalary = 12.68
    IsLower = PartTimeSalary < ContractorSalary

    Response.Write("Part Time Salary:  " & PartTimeSalary & "<br />" & _
                   "Contractor Salary: " & ContractorSalary & "<br />" & _
                   "Is PartTimeSalary < ContractorSalary? " & IsLower)
%>

</body>
</html>

This would produce:

Comparison

Less Than or Equal to: <=

To know if two values are the same or if the first is less than the second, you use the <= operator. Its formula is:

Value1 <= Value2

The <= operation performs a comparison as any of the last two. If both Value1 and VBalue2 hold the same value, result is true or positive. If the left operand, in this case Value1, holds a value lower than the second operand, in this case Value2, the result is still true:

Less than or equal to

Greater Than: >

To find out if one of two values is greater than the other, you use the > operator. Its formula is:

Value1 > Value2

Greater Than

Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim PartTimeSalary, ContractorSalary As Double
    Dim IsLower As Boolean

    PartTimeSalary = 20.15
    ContractorSalary = 22.48
    IsLower = PartTimeSalary > ContractorSalary

    Response.Write("Part Time Salary:  " & PartTimeSalary & "<br />" & _
                   "Contractor Salary: " & ContractorSalary & "<br />" & _
                   "Is PartTimeSalary > ContractorSalary? " & IsLower)
    Response.Write("<br />----------------------------------<br />")

    PartTimeSalary = 25.55
    ContractorSalary = 12.68
    IsLower = PartTimeSalary > ContractorSalary

    Response.Write("Part Time Salary:  " & PartTimeSalary & "<br />" & _
                   "Contractor Salary: " & ContractorSalary & "<br />" & _
                   "Is PartTimeSalary > ContractorSalary? " & IsLower)
%>

</body>
</html>

This would produce:

Comparison

Greater or Equal to: >=

To combine the > and =, you get >=. This is the "greater than or equal to" operator. Its formula is:

Value1 >= Value2

Greater Than Or Equal

Incrementing or Decrementing a Value

 

Incrementing a Variable

To increment the value held by a variable, you can add 1 to it and add the expression to the variable itself. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Value As Integer

    Value = 24

    Response.Write(Value)
    Response.Write("<br />")

    Value = Value + 1

    Response.Write(Value)
%>

</body>
</html>

This would produce:

Increment

Decrementing a Value

To decrement a variable, you can subtract 1 from it and assign the result to the variable itself. This operation works as if a value is decremented by 1, as in Value = Value 1. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Value As Integer

    Value = 24

    Response.Write(Value)
    Response.Write("<br />")

    Value = Value - 1

    Response.Write(Value)
%>

</body>
</html>

This would produce:

Decrement

Techniques of Incrementing and Decrementing a Variable

It is not unusual to add or subtract a constant value to or from a variable. All you have to do is to declare another variable that would hold the new value. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Value As Double = 12.75

    Response.Write("Value = " & Value)
    Response.Write("<br />")

    Value = Value + 2.42

    Response.Write("Value = " & Value)
%>

</body>
</html>

This would produce:

Increment

To add a value to a variable and change the value that the variable is holding, you can combine the assignment = and the addition + operators to produce a new operator as +=

Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Value As Double = 12.75

    Response.Write("Value = " & Value)
    Response.Write("<br />")

    Value += 2.42

    Response.Write("Value = " & Value)
%>

</body>
</html>

This program produces the same result as the previous. To decrement the value of a variable, instead of the addition, use the subtraction and apply the same technique. This is done with the -= operator. Here is an example:

<%@ Page Language="VB"  %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Value As Double = 12.75

    Response.Write("Value = " & Value)
    Response.Write("<br />")

    Value -= 2.42

    Response.Write("Value = " & Value)
%>

</body>
</html>

This would produce:

Decrement

Here is a summary table of the logical operators we have studied:

 
Operator Meaning Example Opposite
= Equality to a = b <>
<> Not equal to 12 <> 7 =
< Less than 25 < 84 >=
<= Less than or equal to Cab <= Tab >
> Greater than 248 > 55 <=
>= Greater than or equal to Val1 >= Val2 <

 

 
 
   
 

Home Copyright © 2009-2010 FunctionX Next