Home

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

  1. Start Microsoft Access
  2. In the list of files, click Business Mathematics database from the previous lesson
  3. In the Navigation Pane, right-click Loan Decision2 and click Design View
  4. On the form, right-click the Decide button and click Build Event...
  5. In the Choose Builder dialog box, click Code Builder and click OK
  6. Implement the event as follows:
    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
  7. Return to Microsoft Access and switch the to Form View
  8. Click the Decide button

    Nesting a Conditional Statement

  9. Click Customer Credit Store and type 428
  10. Click the Decide button

    Nesting a Conditional Statement

  11. Click Customer Credit Store and type 568
  12. Click the Decide button

    Nesting a Conditional Statement

  13. Click Customer Credit Store and type 685
  14. Click the Decide button

    Nesting a Conditional Statement

  15. Save and close the form

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

  1. In the Navigation Pane, right-click Compound Interest1 and click Design View
  2. On the form, right-click the Calculate button and click Build Event...
  3. In the Choose Builder dialog box, double-click Code Builder and implement the event as follows:
    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 Semi-Annually", _
                                            "Compounded Anually")))))
        txtInterestEarned = FormatNumber(interestEarned)
        txtFutureValue = FormatNumber(futureValue)
    End Sub
  4. Return to Microsoft Access and switch the to Form View

    Nesting an Immediate If Function

  5. Click Principal and type 8500
  6. Click Interest Rate and type 4.25
  7. Click Periods and type 10
  8. Click the Calculate button
  9. In the input box, type 3

    Nesting an Immediate If Function

  10. Click OK

    Nesting an Immediate If Function

  11. Click the Calculate button
  12. In the input box, type 8 and press Enter

    Nesting an IIF() Function

  13. Save and close the form

Boolean Conjunctions

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:

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

  1. In the Navigation Pane, right-click Brokerage Company1 and click Design View
  2. On the form, right-click the Calculate button and click Build Event...
  3. In the Choose Builder dialog box, double-click Code Builder and implement the event as follows:
    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
  4. Return to Microsoft Access and switch the to Form View

    Creating and Using Boolean Conjunctions

  5. Click Number of Shares and type 1500
  6. Click Price per Share and type 48.75

    Nesting an IIF() Function

     

  7. Click Calculate

    Nesting an Immediate If Function

  8. Click the Calculate button
  9. In the input box, type 8 and press Enter
  10. Save and close the form

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.

Boolean Disjunctions

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 sub-expression while a disjunction (or conjunction) can be used to evaluate another sub-expression.

As seen previously, one way you can combine conditional statements is by nesting them.

Practical Learning: Ending the Lesson


Previous Copyright © 2008-2022, FunctionX, Inc. Next