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 is the program that could be used to check these conditions: Private Sub cmdCondition_Click() Dim TypeOfHouse As String Dim Choice As Integer Dim Value As Double Dim IndoorGarageAnswer As Integer Dim Answer As String TypeOfHouse = "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?")) TypeOfHouse = Choose(Choice, "Single Family", _ "Townhouse", _ "Condominium") IndoorGarageAnswer = _ MsgBox("Does the house have an indoor garage (1=Yes/0=No)?", _ vbQuestion Or vbYesNo, _ "Real Estate") Answer = IIf(IndoorGarageAnswer = vbYes, "Yes", "No") If (TypeOfHouse = "Single Family") And _ (Value <= 550000) And _ (IndoorGarageAnswer = vbYes) Then MsgBox "Desired House Type: " & vbTab & TypeOfHouse & 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 End Sub We saw that when two conditions are combined, the interpreter first checks the first condition, followed by the second. In the same way, if three conditions need to be considered, the interpreter evaluates the truthfulness of the first condition:
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:
If the second condition is false, the whole combination is considered 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:
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:
From our discussion so far, the truth table of the combinations can be illustrated as follows:
The whole combination is true only if all three conditions are true. This can be illustrated as follows:
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:
The other properties would not be considered, especially if they have more than one story:
We can show this operation as follows:
To support "either or" conditions in the Visual Basic language, you use the Or operator. Here is an example: Private Sub cmdCondition_Click() Dim TypeOfHouse As String Dim Choice As Integer Dim Stories As Integer TypeOfHouse = "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)) TypeOfHouse = Choose(Choice, "Single Family", _ "Townhouse", _ "Condominium") Stories = CInt(InputBox("How many stories?", "Real Estate", 1)) If Choice = 1 Or Stories = 1 Then MsgBox "Desired House Type:" & vbTab & TypeOfHouse & vbCrLf & _ "Number of Stories:" & vbTab & vbTab & Stories MsgBox "Desired House Matched" Else MsgBox "The House Doesn't Match the Desired Criteria" End If End Sub Here is an example of running the program:
As done for the And operator, to make a logical disjunction easy to read, you can include each statement in parentheses: Private Sub cmdCondition_Click() . . . No Change If (Choice = 1) Or (Stories = 1) Then MsgBox "Desired House Type:" & vbTab & TypeOfHouse & vbCrLf & _ "Number of Stories:" & vbTab & vbTab & Stories MsgBox "Desired House Matched" Else MsgBox "The House Doesn't Match the Desired Criteria" End If End Sub 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:
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:
If we find a condo and it is one story, both criteria are true. This can be illustrated in the following table:
The following run of the program demonstrates this:
A Boolean OR operation produces a false result only if BOTH conditions ARE FALSE:
Here is another example of running the program:
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. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|