Boolean Conjunctions and Disjunctions 

Conditional Conjunctions
Nesting a Conditional Statement
In the body of the conditional statement, you can create another conditional statement. This is referred to as nesting the condition.
Practical Learning: Nesting a Conditional Statement
Private Sub cmdDecide_Click() Dim decision As String Dim creditScore As Integer If IsNumeric(txtCreditScore) Then creditScore = CInt(Nz(txtCreditScore)) If creditScore >= 650 Then decision = "Loan Approved" ElseIf creditScore >= 550 Then decision = "We will need 2 professional and 3 personal references." Else decision = "Loan Denied" End If End If txtDecision = decision End Sub
Nesting an Immediate If Function
To get the functionality of an If...Then...ElseIf... conditional statement, you can nest one IIf() function in another. In reality, you would call the IIf() function in place of the FalsePart of another IIf() function. The nesting can be resolved as follows:
Function IIf(ByVal Expression As Boolean, ByVal TruePart As Object, Function IIf(ByVal Expression As Boolean, ByVal TruePart As Object, Function IIf(ByVal Expression As Boolean, ByVal TruePart As Object, Function IIf(ByVal Expression As Boolean, ByVal TruePart As Object, ByVal FalsePart As Object ) As Object ) As Object ) As Object ) As Object
By nesting IIf() calls, a long expression like the following can be written in one line:
Private Sub FirstName_LostFocus() If FirstName = "" Then If LastName = "" Then FullName = "" Else FullName = LastName End If Else FullName = LastName & ", " & FirstName End If End Sub
Practical Learning: Nesting an Immediate If Function
Private Sub cmdCalculate_Click() Dim factor Dim principal Dim futureValue Dim interestRate Dim interestEarned Dim periods As Integer Dim compounded As Integer Dim compoundFrequency As Integer principal = CDbl(Nz(txtPrincipal)) interestRate = CDbl(Nz(txtInterestRate)) / 100# periods = CInt(Nz(txtPeriods)) compounded = CInt(InputBox("Enter a number for the desired compound frequency:" & vbCrLf & _ "1  Daily" & vbCrLf & _ "2  Weekly" & vbCrLf & _ "3  Monthly" & vbCrLf & _ "4  Quarterly" & vbCrLf & _ "5  Semiannually" & vbCrLf & _ "6  Anually", _ "Compound interest", "1")) compoundFrequency = iif(compounded = 1, 365, iif(compounded = 2, 52, iif(compounded = 3, 12, iif(compounded = 4, 4, iif(compounded = 5, 2, 1))))) factor = iif(compounded = 1, interestRate / 365, _ iif(compounded = 2, interestRate / 52, _ iif(compounded = 3, interestRate / 12, _ iif(compounded = 4, interestRate / 4, _ iif(compounded = 5, interestRate / 2, interestRate) _ ) _ ) _ ) _ ) futureValue = principal * ((1# + factor) ^ (periods * compoundFrequency)) interestEarned = futureValue  principal txtCompounded = iif(compounded = 1, "Compounded Daily", _ iif(compounded = 2, "Compounded Weekly", _ iif(compounded = 3, "Compounded Monthly", _ iif(compounded = 4, "Compounded Quarterly", _ iif(compounded = 6, "Compounded SemiAnnually", _ "Compounded Anually"))))) txtInterestEarned = FormatNumber(interestEarned) txtFutureValue = FormatNumber(futureValue) End Sub
When you nest one condition in another condition as in:
If condition1 = True ' The first or external condition If condition2 = True ' The second or internal condition statement(s) End If End If
you are in fact saying "If condition1 verifies, Then if condition2 verifies, do this...". To support a simplified version of this scenario, the Visual Basic language provides the Boolean conjunction operator named And. Its primary formula is:
condition1 And Condition2
statement(s)
You must formulate each condition to produce a true or a false result. The result is as follows:
Sub Calculate()
Dim principal As Double, commission As Double
Dim numberOfShares As Integer, pricePerShare As Double
numberOfShares = CInt(TxtNumberOfShares)
pricePerShare = CDbl(TxtPricePerShare)
principal = numberOfShares * pricePerShare
If principal >= 0.00 And principal <= 2500.00 Then
commission = 26.25 + (principal * 0.0014)
End If
End Sub
To make your code easier to read, it is a good idea to include each Boolean operation in its own parentheses.
Practical Learning: Creating and Using Boolean Conjunctions
Private Sub cmdCalculate_Click() Dim principal As Double, commission As Double Dim numberOfShares As Integer, pricePerShare As Double numberOfShares = CInt(Nz(txtNumberOfShares)) pricePerShare = CDbl(Nz(txtPricePerShare)) principal = numberOfShares * pricePerShare If principal = 0# Then commission = 0# End If If (principal > 0#) And (principal <= 2500#) Then commission = 26.25 + (principal * 0.0014) End If If (principal > 2500#) And (principal <= 6000#) Then commission = 45# + (principal * 0.0054) End If If (principal > 6000#) And (principal <= 20000#) Then commission = 60# + (principal * 0.0028) End If If (principal > 20000#) And (principal <= 50000#) Then commission = 75# + (principal * 0.001875) End If If (principal > 50000#) And (principal <= 500000#) Then commission = 131.25 + (principal * 0.0009) End If If (principal > 500000#) Then commission = 206.25 + (principal * 0.000075) End If txtPrincipal = FormatNumber(principal) txtCommission = FormatNumber(commission) txtTotalInvestment = FormatNumber(principal + commission) End Sub
Creating Many Conjunctions
Depending on your program, if two conditions are not enough, you can create as many conjunctions as you want. The formula to follow is:
condition1 And condition2 And condition3 And . . . And condition_n
When the expression is checked, if any of the operations is false, the whole operation is false. The only time the whole operation is true is if all of the operations are true.
Of course, you can nest a Boolean condition inside another conditional statement.
Introduction
A Boolean disjunction is a combination of conditions where only one of the conditions needs to be true for the whole operation to be true. This operation is performed using the Boolean disjunction operator represented as Or. The primary formula to follow is:
condition1 Or condition2
The operation works as follows:
Combining Conjunctions and Disjunctions
Conjunctions and disjunctions can be used in the same expression. A conjunction (or disjunction) can be used to evaluate one subexpression while a disjunction (or conjunction) can be used to evaluate another subexpression.
As seen previously, one way you can combine conditional statements is by nesting them.
Practical Learning: Ending the Lesson


Previous  Copyright © 20082021, FunctionX, Inc.  Next 
