Introduction to Conditions

 Introduction to Boolean Values

 Introduction

 In Lesson 3, we introduced numeric values and strings. And we have used those so far. A value is referred to as Boolean if it can be either true or false. As you may imagine, the essence of a Boolean value is to check that a condition is true or false, valid or invalid.
 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: ```Public Module Exercise Public Function Main() As Integer Dim EmployeeIsMarried As Boolean Return 0 End Function End Module```

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:

```Public Module Exercise

Public Function Main() As Integer
Dim EmployeeIsMarried As Boolean

MsgBox("Employee Is Married? " & EmployeeIsMarried)
Return 0
End Function

End Module```

This would produce:

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:

```Public Module Exercise

Public Function Main() As Integer
Dim EmployeeIsMarried As Boolean

EmployeeIsMarried = -792730
MsgBox("Employee Is Married? " & EmployeeIsMarried)
Return 0
End Function

End Module```

The number can be decimal or hexadecimal:

```Public Module Exercise

Public Function Main() As Integer
Dim EmployeeIsMarried As Boolean

EmployeeIsMarried = &HFA26B5
MsgBox("Employee Is Married? " & EmployeeIsMarried)
Return 0
End Function

End Module```
 Boolean Values and Procedures

 Introduction

As with the other data types that we have used so far, 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. Some of the issues involved with procedures require conditional statements that we will study in the next lesson. Still, the basic functionality is possible with what we have learned so far.

 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

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 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```

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. We will see an example below.

 Boolean Built-In Functions

 Converting a Value to Boolean

To assist you with validating some values or variables to true or false, the Visual Basic language provides many functions. First, to convert a value to Boolean, you can use the CBool() function. Its syntax is:

`Function CBool(ByVal Expression As Object) As Boolean`

Like all conversion functions, CBool takes one 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 of finding out whether it is numeric or not. To assist you with this, the Visual Basic language provides a function named IsNumeric. 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:

```Public Module Exercise

Public Function Main() As Integer
Dim Value As Object

Value = 258.08 * 9920.3479

MsgBox("Is Numeric? " & IsNumeric(Value))
Return 0
End Function

End Module```

This would produce:

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

```Public Module Exercise

Public Function Main() As Integer
Dim Value As Object

Value = #12/4/1770#

MsgBox("Is Numeric? " & IsNumeric(Value))
Return 0
End Function

End Module```

This would produce:

 Checking for Valid Date/Time

To find out whether an expression holds a valid date, a valid, or not, 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:

```Public Module Exercise

Public Function Main() As Integer
Dim DateHired As Object

DateHired = "9/16/2001"
MsgBox("Is Date? " & IsDate(DateHired))
Return 0
End Function

End Module```

This would produce:

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

```Public Module Exercise

Public Function Main() As Integer
Dim DateHired As Object

DateHired = "Who Knows?"
MsgBox("Is Date? " & IsDate(DateHired))
Return 0
End Function

End Module```

This would produce:

 Checking for Nothing

If you have a variable or an expression and want to find out whether it 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.

 Equality

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

`Value1 = Value2`

The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. From our syntax, the value of Value1 would be compared with the value of Value2. If Value1 and Value2 hold the same value, the comparison produces a True result. If they are different, the comparison renders false or 0.

Here is an example:

```Module Exercise

Public Function Main() As Integer
Dim IsFullTime As Boolean

MsgBox("Is Employee Full Time? " & IsFullTime)

IsFullTime = True
MsgBox("Is Employee Full Time? " & IsFullTime)
Return 0
End Function

End Module```

This would produce:

 Inequality <>

As opposed to checking for equality, you may instead want to know whether two values are different. The operator used to perform this comparison is <> and its formula is:

`Variable1 <> Variable2`

If the operands on both sides of the operator are the same, the comparison renders false. If both operands hold different values, then the comparison produces a true result. This also shows that the equality = and the inequality <> operators are opposite.

Here is an example:

```Module Exercise

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

Public Function Main() As Integer
Dim a%, b%
Dim Result As Boolean

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

MsgBox("The resulting comparison of 12 <> 48 is " & Result)
Return 0
End Function

End Module```

This would produce:

 A Lower Value <

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

`Value1 < Value2`

The value held by Value1 is compared to that of Value2. As it would be done with other operations, the comparison can be made between two variables, as in Variable1 < Variable2. If the value held by Variable1 is lower than that of Variable2, the comparison produces a True.

Here is an example:

```Module Exercise

Public Function Main() As Integer
Dim PartTimeSalary, ContractorSalary As Double
Dim IsLower As Boolean

PartTimeSalary = 20.15
ContractorSalary = 22.48
IsLower = PartTimeSalary < ContractorSalary

MsgBox("Part Time Salary:  " & PartTimeSalary & vbCrLf & _
"Contractor Salary: " & ContractorSalary & vbCrLf & _
"Is PartTimeSalary < ContractorSalary? " & IsLower)

PartTimeSalary = 25.55
ContractorSalary = 12.68
IsLower = PartTimeSalary < ContractorSalary

MsgBox("Part Time Salary:  " & PartTimeSalary & vbCrLf & _
"Contractor Salary: " & ContractorSalary & vbCrLf & _
"Is PartTimeSalary < ContractorSalary? " & IsLower)
Return 0
End Function

End Module```

This would produce:

 Equality and Lower Value <=

The previous two operations can be combined to compare two values. This allows you to know if two values are the same or if the first is less than the second. The operator used is <= and its syntax 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:

 Greater Value >

When two values of the same type are distinct, one of them is usually higher than the other. VBasic provides a logical operator that allows you to find out if one of two values is greater than the other. The operator used for this operation uses the > symbol. Its syntax is:

`Value1 > Value2`

Both operands, in this case Value1 and Value2, can be variables or the left operand can be a variable while the right operand is a constant. If the value on the left of the > operator is greater than the value on the right side or a constant, the comparison produces a True value. Otherwise, the comparison renders False or null:

Here is an example:

```Module Exercise

Public Function Main() As Integer
Dim PartTimeSalary, ContractorSalary As Double
Dim IsLower As Boolean

PartTimeSalary = 20.15
ContractorSalary = 22.48
IsLower = PartTimeSalary > ContractorSalary

MsgBox("Part Time Salary:  " & PartTimeSalary & vbCrLf & _
"Contractor Salary: " & ContractorSalary & vbCrLf & _
"Is PartTimeSalary > ContractorSalary? " & IsLower)

PartTimeSalary = 25.55
ContractorSalary = 12.68
IsLower = PartTimeSalary > ContractorSalary

MsgBox("Part Time Salary:  " & PartTimeSalary & vbCrLf & _
"Contractor Salary: " & ContractorSalary & vbCrLf & _
"Is PartTimeSalary > ContractorSalary? " & IsLower)
Return 0
End Function

End Module```

This would produce:

 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. Its syntax is:

`Value1 >= Value2`

A comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of Value2 are the same, the comparison produces a True value. If the value of the left operand is greater than that of the right operand, the comparison still produces True. If the value of the left operand is strictly less than the value of the right operand, the comparison produces a False result:

 Incrementing or Decrementing a Value

 Incrementing a Variable

We are used to counting numbers such as 1, 2, 3, 4, etc. In reality, when counting such numbers, we are simply adding 1 to a number in order to get the next number in the range. The simplest technique of incrementing a value consists of adding 1 to it. After adding 1, the value or the variable is (permanently) modified and the variable would hold the new value. This is illustrated in the following example:

```Module Exercise

Public Function Main() As Integer
Dim Value As Integer

Value = InputBox("Enter the Value:")

MsgBox(Value)

Value = Value + 1

MsgBox(Value)

Return 0
End Function

End Module```

This would produce:

 Decrementing a Value

When counting numbers backward, such as 8, 7, 6, 5, etc, we are in fact subtracting 1 from a value in order to get the lesser value. This operation is referred to as decrementing a value. This operation works as if a value is decremented by 1, as in Value = Value – 1:

```Module Exercise

Public Function Main() As Integer
Dim Value As Integer

Value = InputBox("Enter the Value:")

MsgBox(Value)

Value = Value - 1

MsgBox(Value)

Return 0
End Function

End Module```

This would produce:

 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:

```Module Exercise

Public Function Main() As Integer
Dim Value As Double = 12.75

MsgBox("Value = " & Value)

Value = Value + 2.42

MsgBox("Value = " & Value)

Return 0
End Function

End Module```

This would produce:

The above technique requires that you use an extra variable in your application. The advantage is that each value can hold its own value although the value of the second variable depends on whatever would happen to the original or source variable.

Sometimes in your program you will not need to keep the original value of the source variable. You may want to permanently modify the value that a variable is holding. In this case you can perform the addition operation directly on the variable by adding the desired value to the variable. This operation modifies whatever value a variable is holding and does not need an additional variable.

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:

```Module Exercise

Public Function Main() As Integer
Dim Value As Double = 12.75

MsgBox("Value = " & Value)

Value += 2.42

MsgBox("Value = " & Value)

Return 0
End Function

End Module```

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. In the above program, the variable can have its value decremented by combining the assignment and the subtraction operations on the variable. This is done with the -= operator. Here is an example:

```Module Exercise

Public Function Main() As Integer
Dim Value As Double = 12.75

MsgBox("Value = " & Value)

Value -= 2.42

MsgBox("Value = " & Value)

Return 0
End Function

End Module```

This would produce:

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 <