Introduction to Logical Disjunctions
Introduction to Logical Disjunctions
Boolean Disjunctions
A Boolean disjunction is a conditional statement where you combine more than one condition but you are trying to know whether at least one of the conditions is false. This operation is commonly represented by a Boolean operator named -or. The primary formula to use it is:
condition1 -or condition2
Each condition is formulated as a Boolean operation:
operand1 Boolean-operator operand2 -or operand3 Boolean-operator operand4
As seen with conjunctions, the whole disjunction operation can be assigned to a Boolean variable or it can be written in the parentheses of a conditional statement.
This operation is referred to as a logical disjunction, or a Boolean disjunction, or simply a disjunction. This operation is resumed as follows:
A Boolean Operator for a Disjunction
When you are performing a logical disjunction, you can use any of the comparison operators we saw already (!, -eq, -ne, -lt, -le, -gt, -ge, and -not). When we studied conjunctions, we saw that you cannot logically use the same variable in two equality or non-equality conditions of a conjunction. In a disjunction, you can use the same variable or different variables in conditions. Here are examples that perform logical exclusions:
Write-Host '========================================================================================================='
Write-Host 'Job Requirements Evaluation'
Write-Host '---------------------------------------------------------------------------------------------------------'
# Do you hold a degree - Undergraduate or Graduate (y/n)?
$degree = 'y'
# Are you a certified programmer (Microsoft, Oracle, etc) (Type True or False)?
$certified = $true
if( $degree -eq 'y' -or $certified -eq $true ) {
Write-Host "Candidate holds a degree: $degree"
Write-Host "Candidate is certified: $certified"
Write-Host '---------------------------------------------------------------------------------------------------------'
Write-Host 'Congratulations: We need to schedule the next interview (Human Resources) for you.'
}
else {
Write-Host 'This job requires either a college degree or a professional certification in application programming.'
}
Write-Host '========================================================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Disjunctions.ps1 ========================================================================================================= Job Requirements Evaluation --------------------------------------------------------------------------------------------------------- Candidate holds a degree: y Candidate is certified: True --------------------------------------------------------------------------------------------------------- Congratulations: We need to schedule the next interview (Human Resources) for you. =========================================================================================================
As seen with conjunctions, to make your code easy to read, you can include each condition in its own parentheses. Here are examples:
$name = 'Gabrielle Towa' $timeWorked = 42.50 $status = 'Part-Time' # The first condition is True while the second condition is False: $verdict = ($status -eq 'Part-Time') -or ($status -eq 'Intern') Write-Host 'Employee Name: ' $name Write-Host 'Time Worked: ' $timeWorked Write-Host 'Employment Status: ' $status Write-Host 'Condition-1 OR Condition-2: ' $verdict Write-Host '----------------------------------------------------------' if($verdict -eq $true) { Write-Host 'The time sheet has been approved.' } else { Write-Host 'Part-time employees are not allowed to work overtime.' } Write-Host '==========================================================' $name = 'Stephen Anders' $status = 'Seasonal' $timeWorked = 38.00 # The first condition is False while the second condition is True: $verdict = ($status -eq 'Full-Time') -or ($status -eq 'Seasonal') Write-Host 'Employee Name: ' $name Write-Host 'Time Worked: ' $timeWorked Write-Host 'Employment Status: ' $status Write-Host 'Condition-1 OR Condition-2: ' $verdict Write-Host '----------------------------------------------------------' if($verdict -eq $true) { Write-Host 'The time sheet has been approved.' } else { Write-Host 'Time Sheet under review.' } Write-Host '==========================================================' $name = 'Rose Sandt' $status = 'Full-Time' $timeWorked = 44.00 # Both conditions are True: $verdict = ($status -eq 'Full-Time') -or ($status -eq 'Seasonal') Write-Host 'Employee Name: ' $name Write-Host 'Time Worked: ' $timeWorked Write-Host 'Employment Status: ' $status Write-Host 'Condition-1 OR Condition-2: ' $verdict Write-Host '----------------------------------------------------------' if($verdict -eq $true) { Write-Host 'The time sheet has been approved.' } else { Write-Host 'Part-time and seasonal employees must consult the management.' } Write-Host '==========================================================' $name = 'Joseph Lee' $status = 'Intern' $timeWorked = 36.50 # Both conditions are False: $verdict = ($status -eq 'Unknown') -or ($status -eq 'No Availability') Write-Output 'Employee Name: ' $name Write-Output 'Time Worked: ' $timeWorked Write-Output 'Employment Status: ' $status Write-Output 'Condition-1 OR Condition-2: ' $verdict Write-Output '----------------------------------------------------------' if($verdict -eq $true) { Write-Host 'Employees time sheets are subject to approval.' } else { Write-Host 'New employees must consult the Human Resources department.' } Write-Host '=========================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Disjunctions.ps1 Employee Name: Gabrielle Towa Time Worked: 42.5 Employment Status: Part-Time Condition-1 OR Condition-2: True ---------------------------------------------------------- The time sheet has been approved. ========================================================== Employee Name: Stephen Anders Time Worked: 38 Employment Status: Seasonal Condition-1 OR Condition-2: True ---------------------------------------------------------- The time sheet has been approved. ========================================================== Employee Name: Rose Sandt Time Worked: 44 Employment Status: Full-Time Condition-1 OR Condition-2: True ---------------------------------------------------------- The time sheet has been approved. ========================================================== Employee Name: Joseph Lee Time Worked: 36.5 Employment Status: Intern Condition-1 OR Condition-2: False ---------------------------------------------------------- New employees must consult the Human Resources department. ==========================================================
Disjunction Equality
Remember that, in a Boolean conjunction or disjunction, you use two conditions and each condition performs its own comparison. As we saw with conjunctions, each condition of a disjunction can use its own variable for its comparison. Here is an example we saw in our introduction:
Write-Host '========================================================================================================='
Write-Host 'Job Requirements Evaluation'
Write-Host '---------------------------------------------------------------------------------------------------------'
# Do you hold a degree - Undergraduate or Graduate (y/n)?
$degree = 'y'
# Are you a certified programmer (Microsoft, Oracle, etc) (Type True or False)?
$certified = $true
if( ($degree -eq 'y') -or ($certified -eq $true) ) {
Write-Output "Candidate holds a degree: $degree"
Write-Output "Candidate is certified: $certified"
Write-Host '---------------------------------------------------------------------------------------------------------'
Write-Host 'Congratulations: We need to schedule the next interview (Human Resources) for you.'
}
else {
Write-Host 'This job requires either a college degree or a professional certification in application programming.'
}
Write-Host '========================================================================================================='
Combining Disjunctions
You can create a conditional statement that includes as many disjunctions as you want. The formula to follow is:
condition1 -or condition2 -or . . . -or condition_n
The rule is: If any one of the individual operations is true, the whole operation is true. The whole operation is false only if all the operations are false. Here is an example that uses three conoditions:
Write-Host '================================================================'
Write-Host 'Levels of Security Clearance:'
Write-Output '0. Unknown or None'
Write-Output '1. Non-Sensitive'
Write-Output '2. National Security - Non-Critical Sensitive'
Write-Output '3. National Security - Critical Sensitive'
Write-Output '4. National Security - Special Sensitive'
$level = 3
Write-Host '================================================================'
if( $level -eq 2 -or $level -eq 3 -or $level -eq 4 ) {
Write-Host "Leve of Security Clearance: $level"
Write-Host 'Company Decision: Welcome on board.'
}
else {
Write-Host 'We will get back to you...'
}
Write-Host '================================================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Disjunctions.ps1 ================================================================ Levels of Security Clearance: 0. Unknown or None 1. Non-Sensitive 2. National Security - Non-Critical Sensitive 3. National Security - Critical Sensitive 4. National Security - Special Sensitive ================================================================ Leve of Security Clearance: 3 Company Decision: Welcome on board. ================================================================
Once again, remember that, to make your code easier to read, you can include each condition in its own parentheses. Here are examples:
Write-Host '==========================================' Write-Host ' - Amazing DeltaX - State Income Tax -' Write-Host '==========================================' Write-Host 'States' Write-Output ' AK - Alaska' Write-Output ' AR - Arkansas' Write-Output ' CO - Colorado' Write-Output ' FL - Florida' Write-Output ' GA - Georgia' Write-Output ' IL - Illinois' Write-Output ' IN - Indiana' Write-Output ' KY - Kentucky' Write-Output ' MA - Massachusetts' Write-Output ' MI - Michigan' Write-Output ' MO - Missouri' Write-Output ' MS - Mississippi' Write-Output ' NV - Nevada' Write-Output ' NH - New Hampshire' Write-Output ' NC - North Carolina' Write-Output ' PA - Pennsylvania' Write-Output ' SD - South Dakota' Write-Output ' TN - Tennessee' Write-Output ' TX - Texas' Write-Output ' UT - Utah' Write-Output ' WA - Washington' Write-Output ' WY - Wyoming' $abbr = "MA" $taxRate = 0.00 if( ($abbr -eq "IL") -or ($abbr -eq "UT") ) { $taxRate = 4.95 } elseif( ($abbr -eq "KY") -or ($abbr -eq "MA") -or ($abbr -eq "NH") ) { $taxRate = 5.00 } elseif( ($abbr -eq "FL") -or ($abbr -eq "NV") -or ($abbr -eq "TX") -or ($abbr -eq "WA") -or ($abbr -eq "WY") ) { $taxRate = 0.00 } $grossSalary = 2458.95 $taxAmount = $grossSalary * $taxRate / 100.00 $netPay = $grossSalary - $taxAmount Write-Host '============================================' Write-Host ' - Amazing DeltaX - State Income Tax -' Write-Host '--------------------------------------------' Write-Host "Gross Salary: $grossSalary" Write-Host "Tax Rate: $taxRate" Write-Host '--------------------------------------------' Write-Host "Tax Amount: $taxAmount" Write-Host "Net Pay: $netPay" Write-Host '============================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Disjunctions.ps1 ========================================== - Amazing DeltaX - State Income Tax - ========================================== States AK - Alaska AR - Arkansas CO - Colorado FL - Florida GA - Georgia IL - Illinois IN - Indiana KY - Kentucky MA - Massachusetts MI - Michigan MO - Missouri MS - Mississippi NV - Nevada NH - New Hampshire NC - North Carolina PA - Pennsylvania SD - South Dakota TN - Tennessee TX - Texas UT - Utah WA - Washington WY - Wyoming ============================================ - Amazing DeltaX - State Income Tax - -------------------------------------------- Gross Salary: 2458.95 Tax Rate: 5 -------------------------------------------- Tax Amount: 122.9475 Net Pay: 2336.0025 ============================================
Combining Conjunctions and Disjunctions
You can create a logical expression that combines conjunctions and disjunctions. Here is an example:
Write-Host 'Electric Bill Evaluation'
Write-Host '======================================================'
$zipCode = 20620
$counterStart = 10089
$counterEnd = 11126
$days = 29
$custChargeRate = 0.49315
$nrgChargeRate = 0.16580
$envChargeRate = 0.00043
$electricUse = $counterEnd - $counterStart
$custCharge = $days * $custChargeRate
$energyCharge = $electricUse * $nrgChargeRate
$envControlCharge = $electricUse * $envChargeRate
$serviceTotal = 0
# Let's imagine a fictitious scenario where the government
# wants to assist people in a certain group of ZIP codes by
# giving them a small discount ($3.15) in their electric bill.
if( ($zipCode -ge 20628 -and $zipCode -le 20685 ) -or
($zipCode -ge 21110 -and $zipCode -le 21120 ) -or
($zipCode -ge 21735 -and $zipCode -le 21795 ) ) {
$serviceTotal = $custCharge + $energyCharge + $envControlCharge - 3.15
}
else {
$serviceTotal = $custCharge + $energyCharge + $envControlCharge
}
Write-Output 'Electric Bill Summary:'
Write-Output '======================================================'
Write-Output "Counter Reading Start: $counterStart"
Write-Output "Counter Reading End: $counterEnd"
Write-Output "Total Electric Use: $electricUse"
Write-Output "Number of Days: $days"
Write-Output "Customer ZIP-Code: $zipCode"
Write-Output '======================================================'
Write-Output 'Energy Charge Credit'
Write-Output '======================================================'
Write-Output "Customer Charge Rate: $custChargeRate"
Write-Output "Customer Charge: $custCharge"
Write-Output '------------------------------------------------------'
Write-Output "Energy Charge Rate: $nrgChargeRate"
Write-Output "Energy Charge: $energyCharge"
Write-Output '------------------------------------------------------'
Write-Output "Energy Charge Rate: $envChargeRate"
Write-Output "Environmental Control Charge: $envControlCharge"
Write-Output '------------------------------------------------------'
Write-Output "Electric Servive Total: $serviceTotal"
Write-Output '======================================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Disjunctions.ps1 Electric Bill Evaluation ====================================================== Electric Bill Summary: ====================================================== Counter Reading Start: 10089 Counter Reading End: 11126 Total Electric Use: 1037 Number of Days: 29 Customer ZIP-Code: 20620 ====================================================== Energy Charge Credit ====================================================== Customer Charge Rate: 0.49315 Customer Charge: 14.30135 ------------------------------------------------------ Energy Charge Rate: 0.1658 Energy Charge: 171.9346 ------------------------------------------------------ Energy Charge Rate: 0.00043 Environmental Control Charge: 0.44591 ------------------------------------------------------ Electric Servive Total: 186.68186 ======================================================
Once again, to make your code easy to read, you can include each condition or group in its own parentheses. Here are examples:
Write-Host 'Electric Bill Evaluation' Write-Host '======================================================' $zipCode = 20620 $counterStart = 10089 $counterEnd = 11126 $days = 29 $custChargeRate = 0.49315 $nrgChargeRate = 0.16580 $envChargeRate = 0.00043 $electricUse = $counterEnd - $counterStart $custCharge = $days * $custChargeRate $energyCharge = $electricUse * $nrgChargeRate $envControlCharge = $electricUse * $envChargeRate $serviceTotal = 0 # Let's imagine a fictitious scenario where the government # wants to assist people in a certain group of ZIP codes by # giving them a small discount ($3.15) in their electric bill. if( ( ($zipCode -ge 20628) -and ($zipCode -le 20685) ) -or ( ($zipCode -ge 21110) -and ($zipCode -le 21120) ) -or ( ($zipCode -ge 21735) -and ($zipCode -le 21795) ) ) { $serviceTotal = $custCharge + $energyCharge + $envControlCharge - 3.15 } else { $serviceTotal = $custCharge + $energyCharge + $envControlCharge } Write-Host 'Electric Bill Summary:' Write-Host '======================================================' Write-Output "Counter Reading Start: $counterStart" Write-Output "Counter Reading End: $counterEnd" Write-Output "Total Electric Use: $electricUse" Write-Output "Number of Days: $days" Write-Output "Customer ZIP-Code: $zipCode" Write-Host '======================================================' Write-Host 'Energy Charge Credit' Write-Host '======================================================' Write-Output "Customer Charge Rate: $custChargeRate" Write-Output "Customer Charge: $custCharge" Write-Host '------------------------------------------------------' Write-Host "Energy Charge Rate: $nrgChargeRate" Write-Host "Energy Charge: $energyCharge" Write-Host '------------------------------------------------------' Write-Output "Energy Charge Rate: $envChargeRate" Write-Output "Environmental Control Charge: $envControlCharge" Write-Host '------------------------------------------------------' Write-Output "Electric Servive Total: $serviceTotal" Write-Host '======================================================'
|
|||
Previous | Copyright © 2024-2025, FunctionX | Monday 10 February 2025, 15:50 | Next |
|