Introduction to Conditional Conjunctions
Introduction to Conditional Conjunctions
Fundamentals of Conditional Conjunctions
Introduction
In previous lessons, all the conditions we studied were dealing with one issue. Sometimes, you want to combine two or more conditions that address a common issue.
Nesting a Conditional Statement
A conditional statement has a body, which is from where the condition is defined to where its behavior ends. In the body of the conditional statement, you can create another conditional statement. This is referred to as nesting the condition. The condition nesting can be formulated as follows:
if( condition1 ) # The nesting, main, parent, first, or external condition { if( condition2 ) # The nested, child, second, or internal condition { statement(s) } }
Here is an example:
Write-Host '========================================================'
Write-Host ' - Georgia - State Income Tax -'
Write-Host '========================================================'
$filingStatus
$taxRate
$addedAmount
$grossSalary = 5368.47
Write-Host 'Filing Status'
Write-Output 's - Single'
Write-Output 'c - Married Filing Joint or Head of Household'
$filingStatus = "s"
if($filingStatus -eq "s") {
$filingStatus = "Single"
if($grossSalary -ge 7000) {
$addedAmount = 230
$taxRate = 5.75
}
elseif($grossSalary -ge 5250) {
$addedAmount = 143
$taxRate = 5.00
}
elseif($grossSalary -ge 3750) {
$addedAmount = 83
$taxRate = 4.00
}
elseif($grossSalary -ge 2250) {
$addedAmount = 38
$taxRate = 3.00
}
elseif($grossSalary -ge 750) {
$addedAmount = 8
$taxRate = 2.00
}
else { # if $grossSalary -lt 750)
$addedAmount = 0
$taxRate = 1.00
}
}
else {
$filingStatus = "Married Filing Joint or Head of Household"
if($grossSalary -ge 10000) {
$addedAmount = 340
$taxRate = 5.75
}
elseif($grossSalary -ge 7000) {
$addedAmount = 190
$taxRate = 5.00
}
elseif($grossSalary -ge 5000) {
$addedAmount = 110
$taxRate = 4.00
}
elseif($grossSalary -ge 3000) {
$addedAmount = 50
$taxRate = 3.00
}
elseif($grossSalary -ge 1000) {
$addedAmount = 10
$taxRate = 2.00
}
else # if $grossSalary -lt 1000:)
{
$addedAmount = 0
$taxRate = 1.00
}
}
$taxAmount = $addedAmount + ($grossSalary * $taxRate / 100.00)
$netPay = $grossSalary - $taxAmount
Write-Host '========================================================'
Write-Host '- Georgia - State Income Tax -'
Write-Host '--------------------------------------------------------'
Write-Output "Gross Salary: $grossSalary"
Write-Output "Filing Status: $filingStatus"
Write-Output "Tax Rate: $taxRate%"
Write-Output "Tax Amount: $taxAmount"
Write-Output "Net Pay: $netPay"
Write-Host '========================================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 ======================================================== - Georgia - State Income Tax - ======================================================== Filing Status s - Single c - Married Filing Joint or Head of Household ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5368.47 Filing Status: Single Tax Rate: 5% Tax Amount: 411.4235 Net Pay: 4957.0465 ========================================================
In the same way, you can nest one conditional statement in one, then nest that new one in another conditional statement, and so on. This can be formulated as follows:
if( condition1 ) { if( condition2 ) { if( condition3 ) { statement(s) } } }
A Logical Conjunction
Remember that you can nest one condition in another condition as in:
if( condition1 ) { if( condition2 ) { statement(s) } }
Instead of nesting the second condition, in some (but not all) cases, you can combine the conditions. This operation is referred to as a conjunction, or a Boolean conjunction, or a logical conjunction. The operation is performed with an operator named -and. The litteral formula of a Boolean conjunction is:
condition1 -and condition2
As we saw in the previous lesson and previous sections, each condition is formulated as a Boolean operation with two operands separated by a Boolean operator:
operand1 Boolean-operator operand2
The whole expression can be written in the parenthesis of a conditional statement, as in:
if(condition1 -and condition2) { statement(s) }
Therefore, if using a conditional statement, the whole Boolean conjunction can be formulated as follows:
if(operand1 operator1 operand2 -and operand3 operator2 operand4) { statement(s) }
The (condition1 & condition2) operation is referred to as a binary conjunction or a logical binary conjunction. During the operation, the first condition, condition1, is checked. Then the second condition, condition2, also is checked (regardless of the result of the first condition).
You must formulate each condition to produce a true or a false result. The result is as follows:
The conjunction operation can be resumed as follows:
Condition-1 | Contition 2 | Condition-1 -and Condition-2 |
True | True | True |
True | False | False |
False | True | False |
False | False | False |
Conjunctions and Boolean Operators
Introduction
We mentioned that each condition in a conjunction is formulated as a Boolean expression:
condition1 -and condition2
This means that you will use the Boolean operators we saw already but there are some details to keep in mind.
Checking for Equality
One of the operations you can perform in a conjunction is to check for equality. Since a variable cannot be equal to two values, you should use a different variable for each condition. Here is an example:
Write-Host '======================================================================================='
Write-Host 'To grant you this job, you must have a college degree (undergraduate or graduate).'
$degree = 1
Write-Host '---------------------------------------------------------------------------------------'
Write-Host 'To grant you this job, you must hold a SECRET security level or higher.'
$security = $true
Write-Host '---------------------------------------------------------------------------------------'
if( $degree -eq 1 -and $security -eq $true ) {
Write-Host 'Since you have a college degree and hold a Secret security clearance, welcome on board.'
}
else {
Write-Host 'We will get back to you.'
}
Write-Host '======================================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 ======================================================================================= To grant you this job, you must have a college degree (undergraduate or graduate). --------------------------------------------------------------------------------------- To grant you this job, you must hold a SECRET security level or higher. --------------------------------------------------------------------------------------- Since you have a college degree and hold a Secret security clearance, welcome on board. =======================================================================================
Here is another version of the program:
Write-Host '==================================================================================' Write-Host 'To grant you this job, you must have a college $degree (undergraduate or graduate).' $degree = 0 Write-Host '----------------------------------------------------------------------------------' Write-Host 'To grant you this job, you must hold a SECRET $security level or higher.' $security = $true Write-Host '----------------------------------------------------------------------------------' if( $degree -eq 1 -and $security -eq $true ) { Write-Host 'Since you have a college $degree and hold a Secret $security clearance, welcome on board.' } else { Write-Host 'After a careful review of your résumé, we regret to inform you that we decided to ' Write-Host 'consider a different candidate. We will keep your résumé on file in case another ' Write-Host 'opportunity comes up. We wish you good luck.' } Write-Host '=================================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 ================================================================================== To grant you this job, you must have a college degree (undergraduate or graduate). ---------------------------------------------------------------------------------- To grant you this job, you must hold a SECRET security level or higher. ---------------------------------------------------------------------------------- After a careful review of your résumé, we regret to inform you that we decided to consider a different candidate. We will keep your résumé on file in case another opportunity comes up. We wish you good luck. ==================================================================================
Isolating the Conditions of a Conjunction
Conditional statements can be difficult to read, especially when they are long. When it comes to conjunctions, to make your code easy to read, you can put each condition in its own parentheses. Here is an example from the above code:
Write-Host '==================================================================================' Write-Host 'To grant you this job, you must have a college $degree (undergraduate or graduate).' $degree = 0 Write-Host '----------------------------------------------------------------------------------' Write-Host 'To grant you this job, you must hold a SECRET $security level or higher.' $security = $true Write-Host '----------------------------------------------------------------------------------' if( ($degree -eq 1) -and ($security -eq $true) ) { Write-Host 'Since you have a college $degree and hold a Secret $security clearance, welcome on board.' } else { Write-Host 'After a careful review of your résumé, we regret to inform you that we decided to ' Write-Host 'consider a different candidate. We will keep your résumé on file in case another ' Write-Host 'opportunity comes up. We wish you good luck.' } Write-Host '=================================================================================='
Negating a Condition
Remember that, in a conjunction, each condition is formulated as a Boolean operation. To negate a logical operation, we already know that we can use the ! operator. You can apply this operator in a conjunction. You have various options.
Because a conjunction is a whole Boolean operation by itself, you can negate the whole conjunction. To do this, put the whole operaion in parentheses and precede it with the ! operator. Here is an example:
Write-Host '=================================================================================='
Write-Host 'To grant you this job, you must have a college $degree (undergraduate or graduate).'
$degree = 0
Write-Host '----------------------------------------------------------------------------------'
Write-Host 'To grant you this job, you must hold a SECRET $security level or higher.'
$security = $true
Write-Host '----------------------------------------------------------------------------------'
if( !($degree -eq 1 -and $security -eq $true) ) {
Write-Host 'After a careful review of your résumé, we regret to inform you that we have '
Write-Host 'decided to consider a different candidate for this position. We will keep '
Write-Host 'your résumé on file in case another opportunity comes up. We wish you good luck.'
}
else {
Write-Host 'Congratulations! That company has decided that you are the best fit for this position.'
}
Write-Host '=================================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 ================================================================================== To grant you this job, you must have a college degree (undergraduate or graduate). ---------------------------------------------------------------------------------- To grant you this job, you must hold a SECRET security level or higher. ---------------------------------------------------------------------------------- After a careful review of your résumé, we regret to inform you that we have decided to consider a different candidate for this position. We will keep your résumé on file in case another opportunity comes up. We wish you good luck. ==================================================================================
If you want, you can negate only one of the conditions. In that case, that condition must be included in parentheses. Then, precede that condition with the ! operator. Here is an example:
Write-Host 'Traffic Tickets Management'
Write-Host '====================================================================================================='
Write-Host 'This application allows the county to set the ticket amount from an image filmed by a traffic camera.'
Write-Host '-----------------------------------------------------------------------------------------------------'
$ticket = 90
$driverGoingAt15MilesAboveTheSpeedLimit = 'Yes'
Write-Host 'Road Status'
Write-Host 'R - Regular traffic'
Write-Host 'Z - School Zone'
$roadStatus = 'R'
$valid = $driverGoingAt15MilesAboveTheSpeedLimit -eq 'Yes' -and !($roadStatus -eq 'Z')
if($valid -eq $true) {
$ticket = 45
}
Write-Host '======================================================================================================'
Write-Host 'Ticket Amount:' $ticket
Write-Host '======================================================================================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 Traffic Tickets Management ===================================================================================================== This application allows the county to set the ticket amount from an image filmed by a traffic camera. ----------------------------------------------------------------------------------------------------- Road Status R - Regular traffic Z - School Zone ====================================================================================================== Ticket Amount: 45 ======================================================================================================
If necessary, you can negate each of both conditions. Here is an example:
Write-Host '==================================================================================' Write-Host 'To grant you this job, you must have a college $degree (undergraduate or graduate).' $degree = 0 Write-Host '----------------------------------------------------------------------------------' Write-Host 'To grant you this job, you must hold a SECRET $security level or higher.' $security = $false Write-Host '----------------------------------------------------------------------------------' if( !($degree -eq 1) -and !($security -eq $true) ) { Write-Host "It appears that you don't have a college degree." Write-Host "You also don't have an appropriate level of security clearance." Write-Host "We will get back to you." } else { Write-Host 'Your employment application is currently under review.' } Write-Host '=================================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 ================================================================================== To grant you this job, you must have a college $degree (undergraduate or graduate). ---------------------------------------------------------------------------------- To grant you this job, you must hold a SECRET $security level or higher. ---------------------------------------------------------------------------------- It appears that you don't have a college degree. You also don't have an appropriate level of security clearance. We will get back to you. ==================================================================================
Checking for Non-Equality
In Boolean algebra, to check whwether a certain variable holds a value different from a constant you have, you can use the -ne operator. Because a variable can easily be different from two values, you can use the same variable on two conditions of a conjunction. Here are examples:
Write-Host 'Traffic Tickets Management'
Write-Host 'This application allows the county to set the ticket amount from an image filmed by a traffic camera.'
Write-Host '-----------------------------------------------------------------------------------------------------'
$ticket = 45
Write-Output 'Road Status'
Write-Output 'R - Regular traffic'
Write-Output 'S - Sensitive Area'
Write-Output 'Z - School Zone'
Write-Output 'U - Unknown or information not available'
$status = 'R'
$valid = $status -ne 'R' -and $status -ne 'U'
if($valid -eq $true) {
$ticket = 90
}
Write-Output '====================================================================================================='
Write-Output 'Ticket Amount: ' $ticket
Write-Output '====================================================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 Traffic Tickets Management This application allows the county to set the ticket amount from an image filmed by a traffic camera. ----------------------------------------------------------------------------------------------------- Road Status R - Regular traffic S - Sensitive Area Z - School Zone U - Unknown or information not available ===================================================================================================== Ticket Amount: 45 =====================================================================================================
Here is another version of the program:
Write-Host 'Traffic Tickets Management' Write-Host 'This application allows the county to set the ticket amount from an image filmed by a traffic camera.' Write-Host '-----------------------------------------------------------------------------------------------------' $ticket = 90 Write-Host 'Road Status' Write-Host 'R - Regular traffic' Write-Host 'S - Sensitive Area' Write-Host 'Z - School Zone' Write-Host 'U - Unknown or information not available' $status = 'S' $valid = $status -ne 'R' -and $status -ne 'U' if($valid -eq $true) { $ticket = 90 } Write-Host '=====================================================================================================' Write-Host 'Ticket Amount: ' $ticket Write-Host '====================================================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 Traffic Tickets Management This application allows the county to set the ticket amount from an image filmed by a traffic camera. ----------------------------------------------------------------------------------------------------- Road Status R - Regular traffic S - Sensitive Area Z - School Zone U - Unknown or information not available ===================================================================================================== Ticket Amount: 90 =====================================================================================================
A Conjunction for Non-Equivalence
The -lt, -le, -gt, and -ge operators are also available for conditional statements. You can apply any of those operators to a condition of a conjunction. You can use the same variable for each condition or you can use a different variable for each condition. Here is an example that checks that a number is in a certain range:
Write-Host '================================================================' Write-Output 'To grant you this job, we need to ask a few questions.' Write-Output '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 = 2 Write-Host '================================================================' if( ($level -ge 2) -and ($level -le 4) ) { Write-Host 'Security clearance selected:' $level Write-Host 'Hiring team message: Welcome on board.' } else { Write-Host 'We will get back to you...' } Write-Host '================================================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 ================================================================ To grant you this job, we need to ask a few questions. 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 ================================================================ Security clearance selected: 2 Hiring team message: Welcome on board. ================================================================
Here is another version of the program:
Write-Host '================================================================' Write-Host 'To grant you this job, we need to ask a few questions.' 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 = '1. Non-Sensitive' Write-Host '================================================================' if( ($level -ge 2) -and ($level -le 4) ) { Write-Host 'Security clearance selected:' $level Write-Host 'Hiring team message: Welcome on board.' } else { Write-Host 'Security clearance selected:' $level Write-Host 'Hiring team message: This job requires a higher level of security.' } Write-Host '=========================================================================='
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 ================================================================ To grant you this job, we need to ask a few questions. 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 ================================================================ Security clearance selected: 1. Non-Sensitive Hiring team message: This job requires a higher level of security. ==========================================================================
In the same way, you can use a combination of the Boolean operators any appropriate way you want. Here are examples where one condition uses the -eq operator white the other condition uses a -ge operator:
$name = 'Gabrielle Towa' $timeWorked = 42.50 $status = 'Full-Time' $verdict = $status -eq 'Full-Time' -and $timeWorked -ge 40.00 Write-Host 'Employee Name: ' $name Write-Host 'Time Worked: ' $timeWorked Write-Host 'Employment Status: ' $status Write-Host 'Condition-1 AND Condition-2:' $verdict Write-Host '--------------------------------------------------------------------------------------' # Both conditions are True: 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 = 'Full-Time' $timeWorked = 38.00 $verdict = $status -eq 'Full-Time' -and $timeWorked -ge 40.00 Write-Output 'Employee Name: ' $name Write-Output 'Time Worked: ' $timeWorked Write-Output 'Employment Status: ' $status Write-Output 'Condition-1 AND Condition-2:' $verdict Write-Host '--------------------------------------------------------------------------------------' # The first condition is True AND the second condition is False: if ($verdict -eq $true) { Write-Host 'The time sheet has been approved.' } else { Write-Host 'Time Sheet under review: Every full-time employee must work at least 40 hours a week.' } Write-Host '======================================================================================' $name = 'Rose Sandt' $status = 'Part-Time' $timeWorked = 44.00 $verdict = $status -eq 'Full-Time' -and $timeWorked -ge 40.00 Write-Host 'Employee Name: ' $name Write-Output 'Time Worked: ' $timeWorked Write-Host 'Employment Status: ' $status Write-Output 'Condition-1 AND Condition-2: ' $verdict Write-Host '--------------------------------------------------------------------------------------' # The first condition is False AND the second condition is True: 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 = 'Joseph Lee' $status = 'Part-Time' $timeWorked = 36.50 $verdict = $status -eq 'Full-Time' -and $timeWorked -ge 40.00 Write-Output 'Employee Name: ' $name Write-Host 'Time Worked: ' $timeWorked Write-Output 'Employment Status: ' $status Write-Host 'Condition-1 AND Condition-2:' $verdict Write-Output '--------------------------------------------------------------------------------------' # The first condition is False AND the second condition is False: if ($verdict -eq $true) { Write-Host 'Part-time employees work part-time.' } else { Write-Host 'Part-time employees are not allowed to work overtime.' } Write-Host '======================================================================================'
This would produce:
PS C:\Windows\System32> C:\Exercises\Conjunctions.ps1 Employee Name: Gabrielle Towa Time Worked: 42.5 Employment Status: Full-Time Condition-1 AND Condition-2: True -------------------------------------------------------------------------------------- The time sheet has been approved. ====================================================================================== Employee Name: Stephen Anders Time Worked: 38 Employment Status: Full-Time Condition-1 AND Condition-2: False -------------------------------------------------------------------------------------- Time Sheet under review: Every full-time employee must work at least 40 hours a week. ====================================================================================== Employee Name: Rose Sandt Time Worked: 44 Employment Status: Part-Time Condition-1 AND Condition-2: False -------------------------------------------------------------------------------------- Part-time employees are not allowed to work overtime. ====================================================================================== Employee Name: Joseph Lee Time Worked: 36.5 Employment Status: Part-Time Condition-1 AND Condition-2: False -------------------------------------------------------------------------------------- Part-time employees are not allowed to work overtime. ======================================================================================
In the above example, we wrote each conjunction as one unit (such as $status -eq "Full-Time" -and timeWorked -ge 40.00). Remember that, when creating a logical conjunction, to make your code easy to read, you should put each condition in its own parenthesis. Here are examples:
$name = 'Gabrielle Towa' $timeWorked = 42.50 $status = 'Full-Time' $verdict = ($status -eq 'Full-Time') -and ($timeWorked -ge 40.00) Write-Output 'Employee Name: ' $name Write-Output 'Time Worked: ' $timeWorked Write-Output 'Employment Status: ' $status Write-Output 'Condition-1 AND Condition-2:' $verdict Write-Host '--------------------------------------------------------------------------------------' # Both conditions are True: 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 = 'Full-Time' $timeWorked = 38.00 $verdict = ($status -eq 'Full-Time') -and ($timeWorked -ge 40.00) Write-Output 'Employee Name: ' $name Write-Output 'Time Worked: ' $timeWorked Write-Output 'Employment Status: ' $status Write-Output 'Condition-1 AND Condition-2:' $verdict Write-Output '--------------------------------------------------------------------------------------' # The first condition is True AND the second condition is False: if ($verdict -eq $true) { Write-Host 'The time sheet has been approved.' } else { Write-Host 'Time Sheet under review: Every full-time employee must work at least 40 hours a week.' } Write-Host '======================================================================================' $name = 'Rose Sandt' $status = 'Part-Time' $timeWorked = 44.00 $verdict = ($status -eq 'Full-Time') -and ($timeWorked -ge 40.00) Write-Host 'Employee Name: ' $name Write-Host 'Time Worked: ' $timeWorked Write-Host 'Employment Status: ' $status Write-Host 'Condition-1 AND Condition-2: ' $verdict Write-Host '--------------------------------------------------------------------------------------' # The first condition is False AND the second condition is True: 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 = 'Joseph Lee' $status = 'Part-Time' $timeWorked = 36.50 $verdict = ($status -eq 'Full-Time') -and ($timeWorked -ge 40.00) Write-Output 'Employee Name: ' $name Write-Output 'Time Worked: ' $timeWorked Write-Output 'Employment Status: ' $status Write-Output 'Condition-1 AND Condition-2:' $verdict Write-Output '--------------------------------------------------------------------------------------' # The first condition is False AND the second condition is False: if ($verdict -eq $true) { Write-Host 'Part-time employees work part-time.' } else { Write-Host 'Part-time employees are not allowed to work overtime.' } Write-Host '======================================================================================'
Combining Various Conjunctions
Depending on your goal, if two conditions are not enough, you can create as many conjunctions as you want. The formula to follow is:
condition1-andcondition2-andcondition3-and. . .-andcondition_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 the operations are true.
Of course, you can nest a Boolean condition inside another conditional statement.
Previous | Copyright © 2024-2025, FunctionX | Tuesday 11 February 2025, 15:45 | Next |