Managing Conditional Statements
Managing Conditional Statements
Fundamentals of Managing Conditional Statements
We know how to create normal conditional statements and loops. Here is an example:
Module Exercise
Private Sub RequestNumber()
Dim Number%
Number% = InputBox("Enter a number that is lower than 5")
If Number% <= 5 Then
MsgBox(Number%)
End If
End Sub
Public Function Main() As Integer
RequestNumber()
Return 0
End Function
End Module
When this program runs, if the user enter a number lower than 5 (included), a message box would display that number. If the user enter a number higher than 5, the program would end but would not display it. In a typical program, after validating a condition, you may want to take action. To do that, you can create a section of program inside the validating conditional statement. In fact, you can create a conditional statement inside of another conditional statement. This is referred to as nesting a condition. Any condition can be nested in another and multiple conditions can be included inside of another.
Here is an example where an If...Then condition is nested inside of a Do...Loop While loop:
Module Exercise
Private Sub RequestNumber()
Dim Number%
Do
Number% = InputBox("Enter a number that is lower than 5")
If Number% <= 5 Then
MsgBox(Number%)
End If
Loop While Number <= 5
End Sub
Public Function Main() As Integer
RequestNumber()
Return 0
End Function
End Module
When the program runs, it asks the user to provide a number lower than 5 (included). If the user provides such a number, the value of the number is displayed and the user is asked to provide another number. This would continue as long as the user is entering a number lower than 5. Otherwise, the program would stop.
|
|
Module BethesdaCarRental
Private Function GetEmployeeName(ByVal EmplNbr As Long) As String
Dim Name As String
If EmplNbr = 22804 Then
Name = "Helene Mukoko"
ElseIf EmplNbr = 92746 Then
Name = "Raymond Kouma"
ElseIf EmplNbr = 54080 Then
Name = "Henry Larson"
ElseIf EmplNbr = 86285 Then
Name = "Gertrude Monay"
Else
Name = "Unknown"
End If
Return Name
End Function
Public Function Main() As Integer
Dim EmployeeNumber As Long, EmployeeName As String
Dim CustomerName As String
Dim TagNumber As String, CarSelected As String
Dim RentStartDate As Date, RentEndDate As Date
Dim NumberOfDays As Integer
Dim RateType As String, RateApplied As Double
Dim OrderTotal As Double
Dim OrderInvoice As String
Dim Answer As MsgBoxResult
RateType = "Weekly Rate"
RateApplied = 0
OrderTotal = RateApplied
Do
EmployeeNumber =
CLng(InputBox("Employee number (who processed this order):",
"Bethesda Car Rental", "00000"))
EmployeeName = GetEmployeeName(EmployeeNumber)
CustomerName = InputBox("Enter Customer Name:",
"Bethesda Car Rental", "John Doe")
TagNumber = InputBox("Enter the tag number of the car to rent:",
"Bethesda Car Rental", "000000")
CarSelected = Microsoft.VisualBasic.Switch(
TagNumber = "297419", "BMW 335i",
TagNumber = "485M270", "Chevrolet Avalanche",
TagNumber = "247597", "Honda Accord LX",
TagNumber = "924095", "Mazda Miata",
TagNumber = "772475", "Chevrolet Aveo",
TagNumber = "M931429", "Ford E150XL",
TagNumber = "240759", "Buick Lacrosse",
True, "Unidentified Car")
RentStartDate = CDate(InputBox("Enter Rent Start Date:",
"Bethesda Car Rental", #1/1/1900#))
RentEndDate = CDate(InputBox("Enter Rend End Date:",
"Bethesda Car Rental", #1/1/1900#))
NumberOfDays = DateDiff(DateInterval.Day, RentStartDate, RentEndDate)
RateApplied = CDbl(InputBox("Enter Rate Applied:", _
"Bethesda Car Rental", 0))
Dim TemporaryRate As Double
Select Case NumberOfDays
Case 0, 1
RateType = "Daily Rate"
OrderTotal = RateApplied
Case 2
RateType = "Weekend Rate"
TemporaryRate = RateApplied * 50 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * 2
Case 2 To 7
RateType = "Weekly Rate"
TemporaryRate = RateApplied * 25 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * NumberOfDays
Case Is > 8
RateType = "Monthly Rate"
TemporaryRate = RateApplied * 15 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * NumberOfDays
End Select
OrderInvoice = "===========================" & vbCrLf &
"=//= BETHESDA CAR RENTAL =//=" & vbCrLf &
"==-=-= Order Processing =-=-==" & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Processed by:" & vbTab & EmployeeName & vbCrLf &
"Processed for:" & vbTab & CustomerName & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Car Selected:" & vbCrLf &
vbTab & "Tag #:" & vbTab & TagNumber & vbCrLf &
vbTab & "Car:" & vbTab & CarSelected & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Start Date:" & vbTab & RentStartDate & vbCrLf &
"End Date:" & vbTab & RentEndDate & vbCrLf &
"Nbr of Days:" & vbTab & NumberOfDays & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Rate Type:" & vbTab & RateType & vbCrLf &
"Rate Applied:" & vbTab & FormatCurrency(RateApplied) & vbCrLf &
"Order Total:" & vbTab & FormatCurrency(OrderTotal) & vbCrLf &
"==========================="
MsgBox(OrderInvoice,
MsgBoxStyle.Information Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
Answer = MsgBox("Do you want to process another order?",
MsgBoxStyle.Information Or MsgBoxStyle.YesNo,
"Bethesda Car Rental")
Loop Until Answer = MsgBoxResult.No
Return 0
End Function
End Module
The Goto statement allows a program execution to jump to another section of a procedure in which it is being used. In order to use the Goto statement, insert a name on a particular section of your procedure so you can refer to that name. The name, also called a label, is made of one word and follows the rules we have applied to names (the name can be anything), then followed by a colon ":".
The following program uses a For loop to count from 2 to 18, but when it encounters 10, it jumps to a designated section of the program:
Module Exercise
Public Function Main() As Integer
Dim i As Integer
For i = 2 To 18 Step 1
If i = 10 Then
GoTo StoppingHere
End If
MsgBox("Value: " & i)
Next
StoppingHere:
MsgBox("The execution jumped here.")
Return 0
End Function
End Module
This would produce:
In the same way, you can create as many labels as you judge them necessary in your code and refer to them when you want. Here is an example with various labels:
Module Exercise
Public Function Main() As Integer
Dim Answer As Byte
Answer = InputBox(" -=- Multiple Choice Question -=-" & vbCrLf &
"To create a constant in your code, " &
"you can use the Constant keyword" & vbCrLf &
"Your choice (1=True/2=False)? ")
If Answer = 1 Then GoTo Wrong
If Answer = 2 Then GoTo Right
Wrong: MsgBox("Wrong: The keyword used to create a constant is Const")
GoTo Leaving
Right: MsgBox("Right: Constant is not a keyword")
Leaving:
Return 0
End Function
End Module
Here is an example of executing the program with Answer = 1:
Here is another example of executing the same program with Answer = 2:
|
|
Module BethesdaCarRental
Private Function GetEmployeeName(ByVal EmplNbr As Long) As String
Dim Name As String
If EmplNbr = 22804 Then
Name = "Helene Mukoko"
ElseIf EmplNbr = 92746 Then
Name = "Raymond Kouma"
ElseIf EmplNbr = 54080 Then
Name = "Henry Larson"
ElseIf EmplNbr = 86285 Then
Name = "Gertrude Monay"
Else
Name = "Unknown"
End If
Return Name
End Function
Public Function Main() As Integer
Dim EmployeeNumber As Long, EmployeeName As String
Dim CustomerName As String
Dim TagNumber As String, CarSelected As String
Dim RentStartDate As Date, RentEndDate As Date
Dim NumberOfDays As Integer
Dim RateType As String, RateApplied As Double
Dim OrderTotal As Double
Dim OrderInvoice As String
Dim Answer As MsgBoxResult
RateType = "Weekly Rate"
RateApplied = 0
OrderTotal = RateApplied
Do
EmployeeNumber =
CLng(InputBox("Employee number (who processed this order):", _
"Bethesda Car Rental", "00000"))
EmployeeName = GetEmployeeName(EmployeeNumber)
CustomerName = InputBox("Enter Customer Name:",
"Bethesda Car Rental", "John Doe")
TagNumber = InputBox("Enter the tag number of the car to rent:", _
"Bethesda Car Rental", "000000")
CarSelected = Microsoft.VisualBasic.Switch(
TagNumber = "297419", "BMW 335i",
TagNumber = "485M270", "Chevrolet Avalanche",
TagNumber = "247597", "Honda Accord LX",
TagNumber = "924095", "Mazda Miata",
TagNumber = "772475", "Chevrolet Aveo",
TagNumber = "M931429", "Ford E150XL",
TagNumber = "240759", "Buick Lacrosse",
True, "Unidentified Car")
RentStartDate = CDate(InputBox("Enter Rent Start Date:",
"Bethesda Car Rental", #1/1/1900#))
RentEndDate = CDate(InputBox("Enter Rend End Date:",
"Bethesda Car Rental", #1/1/1900#))
If RentEndDate < RentStartDate Then
MsgBox("The values you entered for the start and end dates " &
"are not consecute. The rent start date must occur " &
"prior to the rent end date",
MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
GoTo RestartOrderProcessing
End If
NumberOfDays = DateDiff(DateInterval.Day, RentStartDate, RentEndDate)
RateApplied = CDbl(InputBox("Enter Rate Applied:",
"Bethesda Car Rental", 0))
Dim TemporaryRate As Double
Select Case NumberOfDays
Case 0, 1
RateType = "Daily Rate"
OrderTotal = RateApplied
Case 2
RateType = "Weekend Rate"
TemporaryRate = RateApplied * 50 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * 2
Case 2 To 7
RateType = "Weekly Rate"
TemporaryRate = RateApplied * 25 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * NumberOfDays
Case Is > 8
RateType = "Monthly Rate"
TemporaryRate = RateApplied * 15 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * NumberOfDays
End Select
OrderInvoice = "===========================" & vbCrLf &
"=//= BETHESDA CAR RENTAL =//=" & vbCrLf &
"==-=-= Order Processing =-=-==" & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Processed by:" & vbTab & EmployeeName & vbCrLf &
"Processed for:" & vbTab & CustomerName & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Car Selected:" & vbCrLf &
vbTab & "Tag #:" & vbTab & TagNumber & vbCrLf &
vbTab & "Car:" & vbTab & CarSelected & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Start Date:" & vbTab & RentStartDate & vbCrLf &
"End Date:" & vbTab & RentEndDate & vbCrLf &
"Nbr of Days:" & vbTab & NumberOfDays & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Rate Type:" & vbTab & RateType & vbCrLf &
"Rate Applied:" & vbTab & FormatCurrency(RateApplied) & vbCrLf &
"Order Total:" & vbTab & FormatCurrency(OrderTotal) & vbCrLf &
"==========================="
MsgBox(OrderInvoice,
MsgBoxStyle.Information Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
RestartOrderProcessing:
Answer = MsgBox("Do you want to process another order?",
MsgBoxStyle.Information Or MsgBoxStyle.YesNo,
"Bethesda Car Rental")
Loop Until Answer = MsgBoxResult.No
Return 0
End Function
End ModuleSo far, we have learned to write a conditional statement that is true or false. You can reverse the true (or false) value of a condition by making it false (or true). To support this operation, the Visual Basic language provides an operator called Not. Its formula is:
Not Expression
When writing the statement, type Not followed by a logical expression. The expression can be a simple Boolean expression. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim IsMarried As Boolean
MsgBox("Is Married: " & IsMarried)
MsgBox("Is Married: " & Not IsMarried)
Return 0
End Function
End Module
This would produce:


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.
Now consider the following program we saw in Lesson 11:
Module Exercise
Public Function Main() As Integer
Dim IsMarried As Boolean
Dim TaxRate As Double
TaxRate = 33.0
MsgBox("Tax Rate: " & TaxRate & "%")
IsMarried = True
If IsMarried = True Then
TaxRate = 30.65
MsgBox("Tax Rate: " & TaxRate & "%")
End If
Return 0
End Function
End Module
This would produce:
![]() |
![]() |
Probably the most classic way of using the Not operator consists of reversing a logical expression. To do this, you precede the logical expression with the Not operator. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim IsMarried As Boolean
Dim TaxRate As Double
TaxRate = 33.0
MsgBox("Tax Rate: " & TaxRate & "%")
IsMarried = True
If Not IsMarried Then
TaxRate = 30.65
MsgBox("Tax Rate: " & TaxRate & "%")
End If
Return 0
End Function
End Module
This would produce:
![]() |
In the same way, you can negate any logical expression.
|
Exiting a Procedure or a Loop |
In the conditional statements and loops we have created so far, we assumed that the whole condition would be processed. Here is an example:
Module Exercise
Private Sub ShowNumbers()
Dim Number As Short
For Number = 1 To 6
MsgBox(Number)
Next
End Sub
Public Function Main() As Integer
ShowNumbers()
Return 0
End Function
End Module
This would produce:
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
In some cases, you may want to exit a conditional statement or a loop before its end. To assist with with this, the Visual Basic language provides the Exit keyword. This keyword works like an operator. It can be applied to a procedure or a For loop. Consider the following ShowNames procedure:
Module Exercise
Private Sub ShowNames()
MsgBox("Patricia Katts")
MsgBox("Gertrude Monay")
MsgBox("Hermine Nkolo")
MsgBox("Paul Bertrand Yamaguchi")
End Sub
Public Function Main() As Integer
ShowNames()
Return 0
End Function
End Module
When the procedure is called, it displays four message boxes that each shows a name. Imagine that at some point you want to ask the compiler to stop in the middle of a procedure. To do this, in the section where you want to stop the flow of a procedure, type Exit Sub. Here is an example:
Module Exercise
Private Sub ShowNames()
MsgBox("Patricia Katts")
MsgBox("Gertrude Monay")
Exit Sub
MsgBox("Hermine Nkolo")
MsgBox("Paul Bertrand Yamaguchi")
End Sub
Public Function Main() As Integer
ShowNames()
Return 0
End Function
End Module
This time, when the program runs, the ShowNames procedure would be accessed and would start displaying the message boxes. After displaying two, the Exit Sub would ask the compiler to stop and get out of the procedure.
Because a function is just a type of procedure that is meant to return a value, you can use the Exit keyword to get out of a function before the End Function line. To do this, in the section where you want to stop the flow of the function, type Exit Function.
|
Exiting a For Loop Counter |
You can also exit a For loop. To do this, in the section where you want to stop, type Exit For. Here is an example to stop a continuing For loop:
Module Exercise
Private Sub ShowNumbers()
Dim Number As Short
For Number = 1 To 6
MsgBox(Number)
If Number = 4 Then
Exit For
End If
Next
End Sub
Public Function Main() As Integer
ShowNumbers()
Return 0
End Function
End Module
When this program executes, it is supposed to display numbers from 1 to 6, but an If...Then condition states that if it gets to the point where the number is 4, it should stop. If you use an Exit For statement, the compiler would stop the flow of For and continue with code after the Next keyword.
|
Exiting a Do Loop |
You can also use the Exit operator to get out of a Do loop. To do this, inside of a Do loop where you want to stop, type Exit Do.
|
Logical Conjunction |
|
Introduction |
As mentioned already, you can nest one conditional statement inside of another. To illustrate, imagine you create a program that would be used by a real estate company that sells houses. You may face a customer who wants to purchase a single family house but the house should not cost over $550,001. To implement this scenario, you can first write a program that asks the user to select a type of house and then a conditional statement would check the type of house. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim Type As String
Dim Choice As Integer
Dim Value As Double
Type = "Unknown"
Choice = CInt(InputBox("Enter the type of house you want to purchase"
& vbCrLf &
"1. Single Family" & vbCrLf &
"2. Townhouse" & vbCrLf &
"3. Condominium" & vbCrLf & vbCrLf &
"You Choice? "))
Value = CDbl(InputBox("Up to how much can you afford?"))
Type = Choose(Choice, "Single Family",
"Townhouse",
"Condominium")
Return 0
End Function
End Module
If the user selects a single family, you can then write code inside the conditional statement of the single family. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim Type As String
Dim Choice As Integer
Dim Value As Double
Type = "Unknown"
Choice = CInt(InputBox("Enter the type of house you want to purchase"
& vbCrLf &
"1. Single Family" & vbCrLf &
"2. Townhouse" & vbCrLf &
"3. Condominium" & vbCrLf & vbCrLf &
"You Choice? "))
Value = CDbl(InputBox("Up to how much can you afford?"))
Type = Choose(Choice, "Single Family",
"Townhouse",
"Condominium")
If Choice = 1 Then
MsgBox("Desired House Type: " & vbTab & Type & vbCrLf &
"Maximum value afforded: " & vbTab & FormatCurrency(Value))
End If
Return 0
End Function
End Module
In that section, you can then write code that would request and check the value the user entered. If that value is valid, you can take necessary action. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim Type As String
Dim Choice As Integer
Dim Value As Double
Type = "Unknown"
Choice =
CInt(InputBox("Enter the type of house you want to purchase" & vbCrLf &
"1. Single Family" & vbCrLf &
"2. Townhouse" & vbCrLf &
"3. Condominium" & vbCrLf & vbCrLf &
"You Choice? "))
Value = CDbl(InputBox("Up to how much can you afford?"))
Type = Choose(Choice, "Single Family",
"Townhouse",
"Condominium")
If Choice = 1 Then
MsgBox("Desired House Type: " & vbTab & Type & vbCrLf &
"Maximum value afforded: " & vbTab & FormatCurrency(Value))
If Value <= 550000 Then
MsgBox("Desired House Matched")
Else
MsgBox("The House Doesn't Match the Desired Criteria")
End If
End If
Return 0
End Function
End Module
|
A Conditional Conjunction |
Using conditional nesting, we have seen how you can write one conditional that depends on another. But you must write one first condition, check it, then nest the other condition. This works fine and there is nothing against it. To provide with with an alternative, you can use what is referred to as a logical conjunction. It consists of writing one If...Then expression that checks two conditions at the same time. To illustrate, once again consider a customer who wants to purchase a single family home that is less than $550,000. You can consider two statements as follows:
To implement it, you would need to write an If...Then condition as:
If The house is single family AND The house costs less than $550,000 Then
Validate
End If
In the Visual Basic language, the operator used to perform a logical conjunction is And. Here is an example of using it:
Module Exercise
Public Function Main() As Integer
Dim Type As String
Dim Choice As Integer
Dim Value As Double
Type = "Unknown"
Choice = _
CInt(InputBox("Enter the type of house you want to purchase" & vbCrLf &
"1. Single Family" & vbCrLf &
"2. Townhouse" & vbCrLf &
"3. Condominium" & vbCrLf & vbCrLf &
"You Choice? "))
Value = CDbl(InputBox("Up to how much can you afford?"))
Type = Choose(Choice, "Single Family",
"Townhouse",
"Condominium")
If Type = "Single Family" And Value <= 550000 Then
MsgBox("Desired House Type: " & vbTab & Type & vbCrLf &
"Maximum value afforded: " & vbTab & FormatCurrency(Value))
MsgBox("Desired House Matched")
Else
MsgBox("The House Doesn't Match the Desired Criteria")
End If
Return 0
End Function
End Module
By definition, a logical conjunction combines two conditions. To make the program easier to read, each side of the conditions can be included in parentheses. Here is an example:
Module Exercise
Public Function Main() As Integer
. . . No Change
If (Type = "Single Family") And (Value <= 550000) Then
MsgBox("Desired House Type: " & vbTab & Type & vbCrLf &
"Maximum value afforded: " & vbTab & FormatCurrency(Value))
MsgBox("Desired House Matched")
Else
MsgBox("The House Doesn't Match the Desired Criteria")
End If
Return 0
End Function
End Module
To understand how logical conjunction works, from a list of real estate properties, after selecting the house type, if you find a house that is a single family home, you put it in the list of considered properties:
| Type of House | House |
| The house is single family | True |

If you find a house that is less than or equal to $550,000, you retain it:
| Price Range | Value |
| $550,000 | True |
For the current customer, you want a house to meet BOTH criteria. If the house is a town house, based on the request of our customer, its conditional value is false. If the house is less than $550,000, such as $485,000, the value of the Boolean Value is true:



If the house is a town house, based on the request of our customer, its conditional value is false. If the house is more than $550,000, the value of the Boolean Value is true. In logical conjunction, if one of the conditions is false, the result if false also. This can be illustrated as follows:
| Type of House | House Value | Result |
| Town House | $625,000 | Town House AND $625,000 |
| False | False | False |
Suppose we find a single family home. The first condition is true for our customer. With the AND Boolean operator, if the first condition is true, then we consider the second criterion. Suppose that the house we are considering costs $750,500: the price is out of the customer's range. Therefore, the second condition is false. In the AND Boolean algebra, if the second condition is false, even if the first is true, the whole condition is false. This would produce the following table:
| Type of House | House Value | Result |
| Single Family | $750,500 | Single Family AND $750,500 |
| True | False | False |
Suppose we find a townhouse that costs $420,000. Although the second condition is true, the first is false. In Boolean algebra, an AND operation is false if either condition is false:
| Type of House | House Value | Result |
| Town House | $420,000 | Town House AND $420,000 |
| False | True | False |
If we find a single family home that costs $345,000, both conditions are true. In Boolean algebra, an AND operation is true if BOTH conditions are true. This can be illustrated as follows:
| Type of House | House Value | Result |
| Single Family | $345,000 | Single Family AND $345,000 |
| True | True | True |
These four tables can be resumed as follows:
| If Condition1 is | If Condition2 is | Condition1 AND Condition2 |
| False | False | False |
| False | True | False |
| True | False | False |
| True | True | True |
As you can see, a logical conjunction is true only of BOTH conditions are true.
|
|
Module BethesdaCarRental
Private Function GetEmployeeName(ByVal EmplNbr As Long) As String
Dim Name As String
If EmplNbr = 22804 Then
Name = "Helene Mukoko"
ElseIf EmplNbr = 92746 Then
Name = "Raymond Kouma"
ElseIf EmplNbr = 54080 Then
Name = "Henry Larson"
ElseIf EmplNbr = 86285 Then
Name = "Gertrude Monay"
Else
Name = "Unknown"
End If
Return Name
End Function
Private Function GetCarCondition() As String
Dim Status As Integer
Dim Condition As String
Do
Status = CInt(InputBox( _
"After inpecting it, enter car condition:" & vbCrLf &
"1. Excellent - No scratch, no damage, no concern" & vbCrLf &
"2. Good - Some concerns (scratches or missing something)." &
"Make sure the customer is aware." & vbCrLf &
"3. Drivable - The car is good enough to drive." &
"The customer must know the status of the car " &
"and agree to rent it",
"Bethesda Car Rental", 1))
Select Case Status
Case 1
Condition = "Excellent"
Case 2
Condition = "Good"
Case 3
Condition = "Drivable"
Case Else
Condition = "Unknown"
End Select
Loop Until (Status > 0) And (Status < 4)
Return Condition
End Function
Public Function Main() As Integer
Dim EmployeeNumber As Long, EmployeeName As String
Dim CustomerName As String
Dim TagNumber As String, CarSelected As String
Dim CarCondition As String
Dim RentStartDate As Date, RentEndDate As Date
Dim NumberOfDays As Integer
Dim RateType As String, RateApplied As Double
Dim OrderTotal As Double
Dim OrderInvoice As String
Dim Answer As MsgBoxResult
RateType = "Weekly Rate"
RateApplied = 0
OrderTotal = RateApplied
Do
EmployeeNumber =
CLng(InputBox("Employee number (who processed this order):",
"Bethesda Car Rental", "00000"))
EmployeeName = GetEmployeeName(EmployeeNumber)
CustomerName = InputBox("Enter Customer Name:",
"Bethesda Car Rental", "John Doe")
TagNumber = InputBox("Enter the tag number of the car to rent:",
"Bethesda Car Rental", "000000")
CarSelected = Microsoft.VisualBasic.Switch(
TagNumber = "297419", "BMW 335i",
TagNumber = "485M270", "Chevrolet Avalanche",
TagNumber = "247597", "Honda Accord LX",
TagNumber = "924095", "Mazda Miata",
TagNumber = "772475", "Chevrolet Aveo",
TagNumber = "M931429", "Ford E150XL",
TagNumber = "240759", "Buick Lacrosse",
True, "Unidentified Car")
CarCondition = GetCarCondition()
RentStartDate = CDate(InputBox("Enter Rent Start Date:",
"Bethesda Car Rental", #1/1/1900#))
RentEndDate = CDate(InputBox("Enter Rend End Date:",
"Bethesda Car Rental", #1/1/1900#))
If RentEndDate < RentStartDate Then
MsgBox("The values you entered for the start and end dates " &
"are not consecute. The rent start date must occur " &
"prior to the rent end date",
MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
GoTo RestartOrderProcessing
End If
NumberOfDays = DateDiff(DateInterval.Day, RentStartDate, RentEndDate)
RateApplied = CDbl(InputBox("Enter Rate Applied:",
"Bethesda Car Rental", 0))
Dim TemporaryRate As Double
Select Case NumberOfDays
Case 0, 1
RateType = "Daily Rate"
OrderTotal = RateApplied
Case 2
RateType = "Weekend Rate"
TemporaryRate = RateApplied * 50 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * 2
Case 2 To 7
RateType = "Weekly Rate"
TemporaryRate = RateApplied * 25 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * NumberOfDays
Case Is > 8
RateType = "Monthly Rate"
TemporaryRate = RateApplied * 15 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * NumberOfDays
End Select
OrderInvoice = "===========================" & vbCrLf &
"=//= BETHESDA CAR RENTAL =//=" & vbCrLf &
"==-=-= Order Processing =-=-==" & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Processed by:" & vbTab & EmployeeName & vbCrLf &
"Processed for:" & vbTab & CustomerName & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Car Selected:" & vbCrLf &
vbTab & "Tag #:" & vbTab & TagNumber & vbCrLf &
vbTab & "Car:" & vbTab & CarSelected & vbCrLf &
"Car Condition:" & vbTab & CarCondition & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Start Date:" & vbTab & RentStartDate & vbCrLf &
"End Date:" & vbTab & RentEndDate & vbCrLf &
"Nbr of Days:" & vbTab & NumberOfDays & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Rate Type:" & vbTab & RateType & vbCrLf &
"Rate Applied:" & vbTab & FormatCurrency(RateApplied) & vbCrLf &
"Order Total:" & vbTab & FormatCurrency(OrderTotal) & vbCrLf &
"==========================="
MsgBox(OrderInvoice, _
MsgBoxStyle.Information Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
RestartOrderProcessing:
Answer = MsgBox("Do you want to process another order?",
MsgBoxStyle.Information Or MsgBoxStyle.YesNo,
"Bethesda Car Rental")
Loop Until Answer = MsgBoxResult.No
Return 0
End Function
End Module![]() |
As seen above, the logical conjunction operator is used to combine two conditions. In some cases, you will need to combine more than two conditions. Imagine a customer wants to purchase a single family house that costs up to $450,000 with an indoor garage. This means that the house must fulfill these three requirements:
Here the program that could be used to check these conditions:
Module Exercise
Public Function Main() As Integer
Dim Type As String
Dim Choice As Integer
Dim Value As Double
Dim IndoorGarageAnswer As MsgBoxResult
Dim Answer As String
Type = "Unknown"
Choice =
CInt(InputBox("Enter the type of house you want to purchase" & vbCrLf &
"1. Single Family" & vbCrLf &
"2. Townhouse" & vbCrLf &
"3. Condominium" & vbCrLf & vbCrLf &
"You Choice? "))
Value = CDbl(InputBox("Up to how much can you afford?"))
Type = Choose(Choice, "Single Family",
"Townhouse",
"Condominium")
IndoorGarageAnswer =
MsgBox("Does the house have an indoor garage (1=Yes/0=No)? ",
MsgBoxStyle.Question Or MsgBoxStyle.YesNo,
"Real Estate")
Answer = IIf(IndoorGarageAnswer = MsgBoxResult.Yes, "Yes", "No")
If (Type = "Single Family") And (Value <= 550000) And
(IndoorGarageAnswer = MsgBoxResult.Yes) Then
MsgBox("Desired House Type: " & vbTab & Type & vbCrLf &
"Maximum value afforded: " & vbTab &
FormatCurrency(Value) & vbCrLf &
"House has indoor garage: " & vbTab & Answer)
MsgBox("Desired House Matched")
Else
MsgBox("The House Doesn't Match the Desired Criteria")
End If
Return 0
End Function
End Module
We saw that when two conditions are combined, the compiler first checks the first condition, followed by the second. In the same way, if three conditions need to be considered, the compiler evaluates the truthfulness of the first condition:
| Type of House |
| A |
| Town House |
| False |
If the first condition (or any condition) is false, the whole condition is false, regardless of the outcome of the other(s). If the first condition is true, then the second condition is evaluated for its truthfulness:
| Type of House | Property Value |
| A | B |
| Single Family | $655,000 |
| True | False |
If the second condition is false, the whole combination is considered false:
| A | B | A && B |
| True | False | False |
When evaluating three conditions, if either the first or the second is false, since the whole condition would become false, there is no reason to evaluate the third. If both the first and the second conditions are false, there is also no reason to evaluate the third condition. Only if the first two conditions are true will the third condition be evaluated whether it is true:
| Type of House | Property Value | Indoor Garage |
| A | B | C |
| Single Family | $425,650 | None |
| True | True | False |
The combination of these conditions in a logical conjunction can be written as A && B && C. If the third condition is false, the whole combination is considered false:
| A | B | A && B | C | A && B && C |
| True | True | True | False | False |
From our discussion so far, the truth table of the combinations can be illustrated as follows:
| A | B | C | A && B && C |
| False | Don't Care | Don't Care | False |
| True | False | Don't Care | False |
| True | True | False | False |
The whole combination is true only if all three conditions are true. This can be illustrated as follows:
| A | B | C | A && B && C |
| False | False | False | False |
| False | False | True | False |
| True | False | False | False |
| True | False | True | False |
| False | True | False | False |
| False | True | True | False |
| True | True | False | False |
| True | True | True | True |
Introduction
Our real estate company has single family homes, townhouses, and condominiums. All of the condos have only one level, also referred to as a story. Some of the single family homes have one story, some have two and some others have three levels. All townhouses have three levels.
Another customer wants to buy a home. The customer says that he primarily wants a condo, but if our real estate company doesn't have a condominium, that is, if the company has only houses, whatever it is, whether a house or a condo, it must have only one level (story) (due to an illness, the customer would not climb the stairs). When considering the properties of our company, we would proceed with these statements:
If we find a condo, since all of our condos have only one level, the criterion set by the customer is true. Even if we were considering another (type of) property, it wouldn't matter. This can be resumed in the following table:
| Type of House | House |
| Condominium | True |
The other properties would not be considered, especially if they have more than one story:
| Number of Stories | Value |
| 3 | False |
We can show this operation as follows:
| Condominium | One Story | Condominium or 1 Story |
| True | False | True |
|
Creating a Logical Disjunction |
To support "either or" conditions in the Visual Basic language, you use the Or operator. Here is an example:
Module Exercise
Public Function Main() As Integer
Dim Type As String
Dim Choice As Integer
Dim Stories As Short
Type = "Unknown"
Choice =
CInt(InputBox("Enter the type of house you want to purchase" & vbCrLf &
"1. Single Family" & vbCrLf &
"2. Townhouse" & vbCrLf &
"3. Condominium" & vbCrLf & vbCrLf &
"You Choice? ", "Real Estate", 1))
Type = Choose(Choice, "Single Family",
"Townhouse",
"Condominium")
Stories = CShort(InputBox("How many stories?", "Real Estate", 1))
If Choice = 1 Or Stories = 1 Then
MsgBox("Desired House Type:" & vbTab & Type & vbCrLf &
"Number of Stories:" & vbTab & vbTab & Stories)
MsgBox("Desired House Matched")
Else
MsgBox("The House Doesn't Match the Desired Criteria")
End If
Return 0
End Function
End Module
As done for the And operator, to make a logical disjunction easy to read, you can include each statement in parentheses:
Module Exercise
Public Function Main() As Integer
. . . No Change
If (Choice = 1) Or (Stories = 1) Then
MsgBox("Desired House Type:" & vbTab & Type & vbCrLf &
"Number of Stories:" & vbTab & vbTab & Stories)
MsgBox("Desired House Matched")
Else
MsgBox("The House Doesn't Match the Desired Criteria")
End If
Return 0
End Function
End Module
Here is an example of running the program:



Suppose that, among the properties our real estate company has available, there is no condominium. In this case, we would then consider the other properties:
| Type of House | House |
| Single Family | False |
If we have a few single family homes, we would look for one that has only one story. Once we find one, our second criterion becomes true:
| Type of House | One Story | Condominium OR 1 Story |
| False | True | True |
This can be illustrated in the following run of the above program:




If we find a condo and it is one story, both criteria are true. This can be illustrated in the following table:
| Type of House | One Story | Condominium OR 1 Story |
| False | True | True |
| True | True | True |
The following run of the program demonstrates this:




A Boolean OR operation produces a false result only if BOTH conditions ARE FALSE:
| If Condition1 is | If Condition2 is | Condition1 OR Condition2 |
| False | True | True |
| True | False | True |
| True | True | True |
| False | False | False |
Here is another example of running the program:



|
|
Module BethesdaCarRental
Private Function GetEmployeeName() As String
Dim Name As String
Dim EmployeeNumber As Long
EmployeeNumber =
CLng(InputBox("Employee number (who processed this order):",
"Bethesda Car Rental", "00000"))
If EmployeeNumber = 22804 Then
Name = "Helene Mukoko"
ElseIf EmployeeNumber = 92746 Then
Name = "Raymond Kouma"
ElseIf EmployeeNumber = 54080 Then
Name = "Henry Larson"
ElseIf EmployeeNumber = 86285 Then
Name = "Gertrude Monay"
Else
Name = "Unknown"
End If
Return Name
End Function
Private Function GetCarCondition() As String
Dim Status As Integer
Dim Condition As String
Do
Status =
CInt(InputBox("After inpecting it, enter car condition:" & vbCrLf &
"1. Excellent - No scratch, no damage, no concern" & vbCrLf &
"2. Good - Some concerns (scratches or missing something)." &
"Make sure the customer is aware." & vbCrLf &
"3. Drivable - The car is good enough to drive." &
"The customer must know the status of the car " &
"and agree to rent it",
"Bethesda Car Rental", 1))
Select Case Status
Case 1
Condition = "Excellent"
Case 2
Condition = "Good"
Case 3
Condition = "Drivable"
Case Else
Condition = "Unknown"
End Select
If (Status < 1) Or (Status > 3) Then
MsgBox("Please enter a valid number between 1 and 3",
MsgBoxStyle.Information Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
End If
Loop Until (Status > 0) And (Status < 4)
Return Condition
End Function
Private Function GetTankLevel() As String
Dim Level As Integer
Dim Result As String
Do
Level = CInt(InputBox("Enter Tank Level:" & vbCrLf &
"1. Empty" & vbCrLf &
"2. 1/4 Empty" & vbCrLf &
"3. 1/2 Full" & vbCrLf &
"4. 3/4 Full" & vbCrLf &
"5. Full",
"Bethesda Car Rental", 1))
Result = Microsoft.VisualBasic.Switch(Level = 1, "Empty",
Level = 2, "1/4 Empty",
Level = 3, "1/2 Full",
Level = 4, "3/4 Full",
True, "Full")
If (Level < 1) Or (Level > 5) Then
MsgBox("Invalid tank level. " &
"Please try again.",
MsgBoxStyle.Information Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
End If
Loop While (Level < 1) Or (Level > 5)
Return Result
End Function
Public Function Main() As Integer
Dim EmployeeName As String
Dim CustomerName As String
Dim TagNumber As String, CarSelected As String
Dim CarCondition As String
Dim TankLevel As String
Dim RentStartDate As Date, RentEndDate As Date
Dim NumberOfDays As Integer
Dim RateType As String, RateApplied As Double
Dim OrderTotal As Double
Dim OrderInvoice As String
Dim Answer As MsgBoxResult
RateApplied = 0
RateType = "Weekly Rate"
OrderTotal = RateApplied
Do
EmployeeName = GetEmployeeName()
CustomerName = InputBox("Enter Customer Name:", _
"Bethesda Car Rental", "John Doe")
TagNumber = InputBox("Enter the tag number of the car to rent:",
"Bethesda Car Rental", "000000")
CarSelected = Microsoft.VisualBasic.Switch(
TagNumber = "297419", "BMW 335i",
TagNumber = "485M270", "Chevrolet Avalanche",
TagNumber = "247597", "Honda Accord LX",
TagNumber = "924095", "Mazda Miata",
TagNumber = "772475", "Chevrolet Aveo",
TagNumber = "M931429", "Ford E150XL",
TagNumber = "240759", "Buick Lacrosse",
True, "Unidentified Car")
CarCondition = GetCarCondition()
TankLevel = GetTankLevel()
RentStartDate = CDate(InputBox("Enter Rent Start Date:",
"Bethesda Car Rental",
#1/1/1900#))
RentEndDate = CDate(InputBox("Enter Rend End Date:",
"Bethesda Car Rental",
#1/1/1900#))
If RentEndDate < RentStartDate Then
MsgBox("The values you entered for the start and end dates " &
"are not consecute. The rent start date must occur " &
"prior to the rent end date",
MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
GoTo RestartOrderProcessing
End If
NumberOfDays = DateDiff(DateInterval.Day, RentStartDate, RentEndDate)
RateApplied = CDbl(InputBox("Enter Rate Applied:", _
"Bethesda Car Rental", 0))
Dim TemporaryRate As Double
Select Case NumberOfDays
Case 0, 1
RateType = "Daily Rate"
OrderTotal = RateApplied
Case 2
RateType = "Weekend Rate"
TemporaryRate = RateApplied * 50 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * 2
Case 2 To 7
RateType = "Weekly Rate"
TemporaryRate = RateApplied * 25 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * NumberOfDays
Case Is > 8
RateType = "Monthly Rate"
TemporaryRate = RateApplied * 15 / 100
RateApplied = RateApplied - TemporaryRate
OrderTotal = RateApplied * NumberOfDays
End Select
OrderInvoice = "===========================" & vbCrLf &
"=//= BETHESDA CAR RENTAL =//=" & vbCrLf &
"==-=-= Order Processing =-=-==" & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Processed by:" & vbTab & EmployeeName & vbCrLf &
"Processed for:" & vbTab & CustomerName & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Car Selected:" & vbCrLf &
vbTab & "Tag #:" & vbTab & TagNumber & vbCrLf &
vbTab & "Car:" & vbTab & CarSelected & vbCrLf &
vbTab & "Tank:" & vbTab & TankLevel & vbCrLf &
"Car Condition:" & vbTab & CarCondition & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Start Date:" & vbTab & RentStartDate & vbCrLf &
"End Date:" & vbTab & RentEndDate & vbCrLf &
"Nbr of Days:" & vbTab & NumberOfDays & vbCrLf &
"------------------------------------------------" & vbCrLf &
"Rate Type:" & vbTab & RateType & vbCrLf &
"Rate Applied:" & vbTab & FormatCurrency(RateApplied) & vbCrLf &
"Order Total:" & vbTab & FormatCurrency(OrderTotal) & vbCrLf &
"==========================="
MsgBox(OrderInvoice,
MsgBoxStyle.Information Or MsgBoxStyle.OkOnly,
"Bethesda Car Rental")
RestartOrderProcessing:
Answer = MsgBox("Do you want to process another order?",
MsgBoxStyle.Information Or MsgBoxStyle.YesNo,
"Bethesda Car Rental")
Loop Until Answer = MsgBoxResult.No
Return 0
End Function
End Module|
Combinations of Disjunctions |
As opposed to evaluating only two conditions, you may face a situation that presents three of them and must consider a combination of more than two conditions. You would apply the same logical approach we reviewed for the logical conjunction, except that, in a group of logical disjunctions, if one of them is true, the whole statement becomes true.
|
|
|||
| Previous | Copyright © 2008-2026, FunctionX | Tueday 15 August 2024 | Next |
|
|
|||