Introduction to Boolean Conjunctions
Introduction to Boolean Conjunctions
Introduction to Conditional Conjunctions
Overview
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.
Practical Learning: Introducing Conditional Conjunctions
printfn "========================================================" printfn " - Georgia - State Income Tax -" printfn "========================================================" let mutable addedAmount = 0.00 let mutable taxRate = 0.00 printfn "Enter the information for tax preparation" printf "Gross Salary: " let grossSalary = float(stdin.ReadLine()) // Georgia let filingStatus = "Married Filing Joint or Head of Household"; if grossSalary >= 10_000 then addedAmount <- 340.00 taxRate <- 5.75 elif grossSalary >= 7_000 then addedAmount <- 190.00 taxRate <- 5.00 elif grossSalary >= 5_000 then addedAmount <- 110.00 taxRate <- 4.00 elif grossSalary >= 3_000 then addedAmount <- 50.00 taxRate <- 3.00 elif grossSalary >= 1_000 then addedAmount <- 10.00 taxRate <- 2.00 else // if grossSalary < 1_000 then addedAmount <- 0.00 taxRate <- 1.00 let taxAmount = addedAmount + (grossSalary * taxRate / 100.00) let net_pay = grossSalary - taxAmount printfn "========================================================" printfn "- Georgia - State Income Tax -" printfn "--------------------------------------------------------" printfn $"Gross Salary: {grossSalary:f}" printfn $"Filing Status: {filingStatus}" printfn "Tax Rate: %0.2f%c" taxRate '%' printfn $"Tax Amount: {taxAmount:f}" printfn $"Net Pay: {net_pay:f}" printfn "========================================================"
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 999.99 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 999.99 Filing Status: Married Filing Joint or Head of Household Tax Rate: 1.00% Tax Amount: 10.00 Net Pay: 989.99 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 1000 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 1000.00 Filing Status: Married Filing Joint or Head of Household Tax Rate: 2.00% Tax Amount: 30.00 Net Pay: 970.00 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5726.87 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5726.87 Filing Status: Married Filing Joint or Head of Household Tax Rate: 4.00% Tax Amount: 339.07 Net Pay: 5387.80 ======================================================== Press any key to close this window . . .
printfn "========================================================" printfn " - Georgia - State Income Tax -" printfn "========================================================" let mutable addedAmount = 0.00 let mutable taxRate = 0.00 printfn "Enter the information for tax preparation" printf "Gross Salary: " let grossSalary = float(stdin.ReadLine()) // Georgia let filingStatus = "Single" if grossSalary >= 7_000 then addedAmount <- 230.00 taxRate <- 5.75 elif grossSalary >= 5_250 then addedAmount <- 143.00 taxRate <- 5.00 elif grossSalary >= 3_750 then addedAmount <- 83.00 taxRate <- 4.00 elif grossSalary >= 2_250 then addedAmount <- 38.00 taxRate <- 3.00 elif grossSalary >= 750 then addedAmount <- 8.00 taxRate <- 2.00 else // if grossSalary < 1_000 then addedAmount <- 0.00 taxRate <- 1.00 let taxAmount = addedAmount + (grossSalary * taxRate / 100.00) let net_pay = grossSalary - taxAmount printfn "========================================================" printfn "- Georgia - State Income Tax -" printfn "--------------------------------------------------------" printfn $"Gross Salary: {grossSalary:f}" printfn $"Filing Status: {filingStatus}" printfn "Tax Rate: %0.2f%c" taxRate '%' printfn $"Tax Amount: {taxAmount:f}" printfn $"Net Pay: {net_pay:f}" printfn "========================================================"
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 749.99 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 749.99 Filing Status: Single Tax Rate: 1.00% Tax Amount: 7.50 Net Pay: 742.49 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 750 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 750.00 Filing Status: Single Tax Rate: 2.00% Tax Amount: 23.00 Net Pay: 727.00 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5726.87 ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5726.87 Filing Status: Single Tax Rate: 5.00% Tax Amount: 429.34 Net Pay: 5297.53 ======================================================== Press any key to close this window . . .
Nesting a Conditional Statement
In the body of a 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 then // The nesting, main, parent, first, or external condition if condition2 then // The nested, child, second, or internal condition statement(s)
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 then if condition2 then if condition3 then statement(s)
Practical Learning: Nesting a Conditional Statement
printfn "========================================================" printfn " - Georgia - State Income Tax -" printfn "========================================================" let mutable taxRate = 0.00 let mutable addedAmount = 0.00 let mutable filingStatus = "Unknown" printfn "Enter the information for tax preparation" printf "Gross Salary: " let grossSalary = float(stdin.ReadLine()) printfn "Filing Status" printfn "s - Single" printfn "c - Married Filing Joint or Head of Household" printf "Enter Filing Status: " let answer = char(stdin.ReadLine()) if answer = 's' then filingStatus <- "Single" if grossSalary >= 7_000 then addedAmount <- 290 taxRate <- 5.75 elif grossSalary >= 5_250 then addedAmount <- 143 taxRate <- 5.00 elif grossSalary >= 3_750 then addedAmount <- 83 taxRate <- 4.00 elif grossSalary >= 2_250 then addedAmount <- 38 taxRate <- 3.00 elif grossSalary >= 750 then addedAmount <- 8.00 taxRate <- 2.00 else // if grossSalary < 1_000 then addedAmount <- 0 taxRate <- 1.00 else filingStatus <- "Married Filing Joint or Head of Household" if grossSalary >= 10_000 then addedAmount <- 340 taxRate <- 5.75 elif grossSalary >= 7_000 then addedAmount <- 190 taxRate <- 5.00 elif grossSalary >= 5_000 then addedAmount <- 110 taxRate <- 4.00 elif grossSalary >= 3_000 then addedAmount <- 50 taxRate <- 3.00 elif grossSalary >= 1_000 then addedAmount <- 10 taxRate <- 2.00 else // if grossSalary < 1_000 then addedAmount <- 0 taxRate <- 1.00 let taxAmount = addedAmount + (grossSalary * taxRate / 100.00) let net_pay = grossSalary - taxAmount printfn "========================================================" printfn "- Georgia - State Income Tax -" printfn "--------------------------------------------------------" printfn $"Gross Salary: {grossSalary:f}" printfn $"Filing Status: {filingStatus}" printfn "Tax Rate: %0.2f%c" taxRate '%' printfn $"Tax Amount: {taxAmount:f}" printfn $"Net Pay: {net_pay:f}" printfn "========================================================"
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5368.47 Filing Status s - Single c - Married Filing Joint or Head of Household Enter Filing Status: s ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5368.47 Filing Status: Single Tax Rate: 5.00% Tax Amount: 411.42 Net Pay: 4957.05 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5368.47 Filing Status s - Single c - Married Filing Joint or Head of Household Enter Filing Status: c ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5368.47 Filing Status: Married Filing Joint or Head of Household Tax Rate: 4.00% Tax Amount: 324.74 Net Pay: 5043.73 ======================================================== Press any key to close this window . . .
printfn "========================================================" printfn " - Georgia - State Income Tax -" printfn "========================================================" let mutable taxRate = 0.00 let mutable addedAmount = 0.00 let mutable filingStatus = "Unknown" printfn "Enter the information for tax preparation" printf "Gross Salary: " let grossSalary = float(stdin.ReadLine()) printfn "Filing Status" printfn "1 - Single" printfn "s - Married Filing Separate" printfn "j - Married Filing Joint or Head of Household" printf "Enter Filing Status: " let answer = char(stdin.ReadLine()) if answer = 'j' then filingStatus <- "Married Filing Joint or Head of Household" if grossSalary >= 10_000 then addedAmount <- 340 taxRate <- 5.75 elif grossSalary >= 7_000 then addedAmount <- 190 taxRate <- 5.00 elif grossSalary >= 5_000 then addedAmount <- 110 taxRate <- 4.00 elif grossSalary >= 3_000 then addedAmount <- 50 taxRate <- 3.00 elif grossSalary >= 1_000 then addedAmount <- 10 taxRate <- 2.00 else // if grossSalary < 1_000: then addedAmount <- 0 taxRate <- 1.00 elif answer = 's' then filingStatus <- "Married Filing Separate" if grossSalary >= 5_000 then addedAmount <- 170 taxRate <- 5.75 elif grossSalary >= 3_500 then addedAmount <- 95 taxRate <- 5.00; elif grossSalary >= 2_500 then addedAmount <- 55 taxRate <- 4.00 elif grossSalary >= 1_500 then addedAmount <- 25 taxRate <- 3.00 elif grossSalary >= 500 then addedAmount <- 5 taxRate <- 2.00 else // if grossSalary < 1_000 then addedAmount <- 0 taxRate <- 1.00 else // if answer = Anything) filingStatus <- "Single" if grossSalary >= 7_000 then addedAmount <- 230 taxRate <- 5.75 elif grossSalary >= 5_250 then addedAmount <- 143 taxRate <- 5.00 elif grossSalary >= 3_750 then addedAmount <- 83 taxRate <- 4.00 elif grossSalary >= 2_250 then addedAmount <- 38 taxRate <- 3.00 elif grossSalary >= 750 then addedAmount <- 8 taxRate <- 2.00 else // if grossSalary < 750 then addedAmount <- 0 taxRate <- 1.00 let taxAmount = addedAmount + (grossSalary * taxRate / 100.00) let net_pay = grossSalary - taxAmount printfn "========================================================" printfn "- Georgia - State Income Tax -" printfn "--------------------------------------------------------" printfn $"Gross Salary: {grossSalary:f}" printfn $"Filing Status: {filingStatus}" printfn "Tax Rate: %0.2f%c" taxRate '%' printfn $"Tax Amount: {taxAmount:f}" printfn $"Net Pay: {net_pay:f}" printfn "========================================================"
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5278.75 Filing Status 1 - Single s - Married Filing Separate j - Married Filing Joint or Head of Household Enter Filing Status: j ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5278.75 Filing Status: Married Filing Joint or Head of Household Tax Rate: 4.00% Tax Amount: 321.15 Net Pay: 4957.60 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5278.75 Filing Status 1 - Single s - Married Filing Separate j - Married Filing Joint or Head of Household Enter Filing Status: s ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5278.75 Filing Status: Married Filing Separate Tax Rate: 5.75% Tax Amount: 473.53 Net Pay: 4805.22 ======================================================== Press any key to close this window . . .
======================================================== - Georgia - State Income Tax - ======================================================== Enter the information for tax preparation Gross Salary: 5278.75 Filing Status 1 - Single s - Married Filing Separate j - Married Filing Joint or Head of Household Enter Filing Status: Separated from husband, living as a single person ======================================================== - Georgia - State Income Tax - -------------------------------------------------------- Gross Salary: 5278.75 Filing Status: Single Tax Rate: 5.00% Tax Amount: 406.94 Net Pay: 4871.81 ======================================================== Press any key to close this window . . .
A Boolean Conjunction
In human languages, a conjunction is a word or expression that connects or joins two other words or expressions. There are various words to play that role and they have different goals.
In application programming, a Boolean conjunction is a word that connects two expressions to find out whether both expressions are true. To support this operation, the F# language provides an operator as &&. The Boolean conjunction follows a formula as follow:
condition1 && condition2
Based on this formula, create conditional expression, as condition1. Add an && operator. Then add another condition expression, ascondition2. Each of the conditions is formulated as a Boolean expression we we were introduced in Lesson 4 as follows:
operand1 Boolean-operator operand2
An operand can be the name of a variable or it can be a value. The operator can be one of the Boolean operators we saw in Lesson 4; but there are some details to which you must pay attention.
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:
printfn "======================================================================================================="
printfn "To grant you this job, we need to ask a few questions."
printf "Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? "
let degree = int(stdin.ReadLine())
printfn "-------------------------------------------------------------------------------------------------------"
printf "Do you hold a SECRET or equivalent security level (Type y or another character): "
let security = char(stdin.ReadLine())
printfn "-------------------------------------------------------------------------------------------------------"
if degree = 1 && security = 'y' then
printfn "Welcome on board."
else
printfn "We will get back to you..."
printfn "======================================================================================================="
Here is an example of running the program:
======================================================================================================= To grant you this job, we need to ask a few questions. Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type y or another character): y ------------------------------------------------------------------------------------------------------- Welcome on board. ======================================================================================================= Press any key to close this window . . .
Here is another example of running the program:
======================================================================================================= To grant you this job, we need to ask a few questions. Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type y or another character): u ------------------------------------------------------------------------------------------------------- We will get back to you... ======================================================================================================= Press any key to close this window . . .
Here is another example of running the program:
======================================================================================================= To grant you this job, we need to ask a few questions. Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? 7 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type y or another character): y ------------------------------------------------------------------------------------------------------- We will get back to you... ======================================================================================================= Press any key to close this window . . .
Practical Learning: Introducing Boolean Conjunctions
printfn "============================================" printfn " - Missouri - State Income Tax -" printfn "============================================" let mutable taxRate = 0.00 let mutable amountAdded = 0.00 printfn "Enter the information for tax preparation" printf "Gross Salary: " let grossSalary = float(stdin.ReadLine()) // Missouri if grossSalary >= 0.00 && grossSalary <= 106 then amountAdded <- 0 taxRate <- 0.00 elif grossSalary > 106 && grossSalary <= 1_073 then amountAdded <- 0 taxRate <- 1.50 elif grossSalary > 1_073 && grossSalary <= 2_146 then amountAdded <- 16 taxRate <- 2.00 elif grossSalary > 2_146 && grossSalary <= 3_219 then amountAdded <- 37 taxRate <- 2.50 elif grossSalary > 3_219 && grossSalary <= 4_292 then amountAdded <- 64 taxRate <- 3.00 elif grossSalary > 4_292 && grossSalary <= 5_365 then amountAdded <- 96 taxRate <- 3.50 elif grossSalary > 5_365 && grossSalary <= 6_438 then amountAdded <- 134 taxRate <- 4.00 elif grossSalary > 6_438 && grossSalary <= 7_511 then amountAdded <- 177 taxRate <- 4.50 elif grossSalary > 7_511 && grossSalary <= 8_584 then amountAdded <- 225 taxRate <- 5.00 else // if grossSalary > 8_584 then amountAdded <- 279 taxRate <- 5.40 let taxAmount = amountAdded + (grossSalary * taxRate / 100.00) let netPay = grossSalary - taxAmount printfn "==============================================" printfn "- Missouri - State Income Tax -" printfn "----------------------------------------------" printfn $"Gross Salary: {grossSalary:f}" printfn "Tax Rate: %0.2f%c" taxRate '%' printfn $"Amount Added: {amountAdded:f}" printfn "----------------------------------------------" printfn $"Tax Amount: {taxAmount:f}" printfn $"Net Pay: {netPay:f}" printfn "=============================================="
============================================ - Missouri - State Income Tax - ============================================ Enter the information for tax preparation Gross Salary: 3688.97 ============================================== - Missouri - State Income Tax - ---------------------------------------------- Gross Salary: 3688.97 Tax Rate: 3.00% Amount Added: 64.00 ---------------------------------------------- Tax Amount: 174.67 Net Pay: 3514.30 ============================================== Press any key to close this window . . .
Omitting Checking Equality
Condition code as follows:
let submitted = true
let received = false
if submitted = true && received = false then
printfn "The application was previously submitted and it has been received."
printfn "The application is currently under review."
This would produce:
The application was previously submitted and it has been received. The application is currently under review. ================================================================== Press any key to close this window . . .
In the previous Lesson, we saw that when a contidion is checking for equality, you can omit its = true expression. Here is example:
let submitted = true
let received = false
if submitted && received = false then
printfn "The application was previously submitted and it has been received."
printfn "The application is currently under review."
In the same way, if each condition of a conjunction is checking for equality, you can omit each = true expression. Here are examples:
From this:
let submitted = true let approved = true if submitted = true && approved = true then printfn "The application was submitted and approved." printfn "==========================================="
To this:
let submitted = true let approved = true if submitted && approved then printfn "The application was submitted and approved." printfn "==========================================="
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. Based on this, the above code can be written as follows:
printfn "=======================================================================================================" printfn "To grant you this job, we need to ask a few questions." printf "Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? " let degree = int(stdin.ReadLine()) printfn "-------------------------------------------------------------------------------------------------------" printf "Do you hold a SECRET or equivalent security level (Type y or another character): " let security = char(stdin.ReadLine()) printfn "-------------------------------------------------------------------------------------------------------" if (degree = 1) && (security = 'y') then printfn "Welcome on board." else printfn "We will get back to you..." printfn "======================================================================================================="
Practical Learning: Isolating the Conditions of a Conjunction
printfn "============================================" printfn " - Missouri - State Income Tax -" printfn "============================================" let mutable taxRate = 0.00 let mutable amountAdded = 0.00 printfn "Enter the information for tax preparation" printf "Gross Salary: " let grossSalary = float(stdin.ReadLine()) // Missouri if (grossSalary >= 0) && ((grossSalary <= 106) then amountAdded <- 0 taxRate <- 0.00 elif (grossSalary > 106) && (grossSalary <= 1_073) then amountAdded <- 0 taxRate <- 1.50 elif (grossSalary > 1_073) && (grossSalary <= 2_146) then amountAdded <- 16 taxRate <- 2.00 elif (grossSalary > 2_146) && (grossSalary <= 3_219) then amountAdded <- 37 taxRate <- 2.50 elif ((grossSalary > 3_219) && ((grossSalary <= 4_292) then amountAdded <- 64 taxRate <- 3.00 elif (grossSalary > 4_292) && (grossSalary <= 5_365)) then amountAdded <- 96 taxRate <- 3.50 elif (grossSalary > 5_365) && (grossSalary <= 6_438) then amountAdded <- 134 taxRate <- 4.00 elif (grossSalary > 6_438) && (grossSalary <= 7_511) then amountAdded <- 177 taxRate <- 4.50 elif ((grossSalary > 7_511) && (grossSalary <= 8_584) then amountAdded <- 225 taxRate <- 5.00 else // if grossSalary > 8_584 then amountAdded <- 279 taxRate <- 5.40 let taxAmount = amountAdded + (grossSalary * taxRate / 100.00) let netPay = grossSalary - taxAmount printfn "==============================================" printfn "- Missouri - State Income Tax -" printfn "----------------------------------------------" printfn $"Gross Salary: {grossSalary:f}" printfn "Tax Rate: %0.2f%c" taxRate '%' printfn $"Amount Added: {amountAdded:f}" printfn "----------------------------------------------" printfn $"Tax Amount: {taxAmount:f}" printfn $"Net Pay: {netPay:f}" printfn "=============================================="
============================================ - Missouri - State Income Tax - ============================================ Enter the information for tax preparation Gross Salary: 7884.65 ============================================== - Missouri - State Income Tax - ---------------------------------------------- Gross Salary: 7884.65 Tax Rate: 5.00% Amount Added: 225.00 ---------------------------------------------- Tax Amount: 619.23 Net Pay: 7265.42 ============================================== Press any key to close this window . . .
Negating a Condition
Remember that, in a conjunction, each condition is formulated as a Boolean operation. In the previous lesson, we saw that, to negate an logical operation, we can precede it with the not 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 not operator. Here is an example:
printfn "=======================================================================================================" printfn "To grant you this job, we need to ask a few questions." printf "Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? " let degree = int(stdin.ReadLine()) printfn "-------------------------------------------------------------------------------------------------------" printf "Do you hold a SECRET or equivalent security level (Type y for Yes any character for No): " let security = char(stdin.ReadLine()) printfn "-------------------------------------------------------------------------------------------------------" if not (degree = 1 && security = 'y') then printfn "The company management needs to investigate your application more seriously." else printfn "You are hired." printfn "======================================================================================================="
Here is an example of running the program:
======================================================================================================= To grant you this job, we need to ask a few questions. Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? 1 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type y for Yes any character for No): y ------------------------------------------------------------------------------------------------------- You are hired. ======================================================================================================= Press any key to close this window . . .
Here is another example of running the program:
======================================================================================================= To grant you this job, we need to ask a few questions. Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? 5 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type y for Yes any character for No): y ------------------------------------------------------------------------------------------------------- The company management needs to investigate your application more seriously. ======================================================================================================= Press any key to close this window . . .
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 not operator. Here is an example:
printfn "Traffic Tickets Management"
printfn "This application allows the county to set the ticket amount from an image filmed by a traffic camera."
printfn "====================================================================================================="
let mutable ticket = 0
printf "Was the driver going at 15 miles above the speed limit (Type y for Yes or any character for No)? "
let speedAnswer = char(stdin.ReadLine())
printfn "------------------------------------------------------------------------------------------------------"
printfn "Road Status"
printfn "R - Regular traffic"
printfn "Z - School Zone"
printf "Enter the road status (Type R, S, or Z)? "
let status = char(stdin.ReadLine())
let valid = speedAnswer = 'y' && not (status = 'Z')
if valid = true then
ticket <- 45
printfn "======================================================================================================"
printfn "Ticket Amount: %i" ticket
printfn "======================================================================================================"
Here is an example of running the program:
Traffic Tickets Management This application allows the county to set the ticket amount from an image filmed by a traffic camera. ===================================================================================================== Was the driver going at 15 miles above the speed limit (Type y for Yes or any character for No)? y ------------------------------------------------------------------------------------------------------ Road Status R - Regular traffic Z - School Zone Enter the road status (Type R, S, or Z)? R ====================================================================================================== Ticket Amount: 45 ====================================================================================================== Press any key to close this window . . .
Here is another example of running the program:
Traffic Tickets Management This application allows the county to set the ticket amount from an image filmed by a traffic camera. ===================================================================================================== Was the driver going at 15 miles above the speed limit (Type y for Yes or any character for No)? n ------------------------------------------------------------------------------------------------------ Road Status R - Regular traffic Z - School Zone Enter the road status (Type R, S, or Z)? i ====================================================================================================== Ticket Amount: 0 ====================================================================================================== Press any key to close this window . . .
If necessary, you can negate each of both conditions. Here is an example:
printfn "=======================================================================================================" printfn "To grant you this job, we need to ask a few questions." printf "Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? " let degree = int(stdin.ReadLine()) printfn "-------------------------------------------------------------------------------------------------------" printf "Do you hold a SECRET or equivalent security level (Type y for Yes any character for No): " let security = char(stdin.ReadLine()) printfn "-------------------------------------------------------------------------------------------------------" if not (degree = 1) && not (security = 'y') then printfn "We will get back to you..." else printfn "You employment application is currently under review." printfn "======================================================================================================="
Here is an example of running the program:
======================================================================================================= To grant you this job, we need to ask a few questions. Do you have a college degree (undergraduate or graduate (Type 1 for Yes or another number for No)? 5 ------------------------------------------------------------------------------------------------------- Do you hold a SECRET or equivalent security level (Type y for Yes any character for No): n ------------------------------------------------------------------------------------------------------- We will get back to you... ======================================================================================================= Press any key to close this window . . .
Checking for Non-Equality
In Boolean algebra, to check whether a certain variable holds a value different from a constant you have, you can use the <> 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:
printfn "Traffic Tickets Management" printfn "This application allows the county to set the ticket amount from an image filmed by a traffic camera." printfn "-----------------------------------------------------------------------------------------------------" let mutable ticket = 45 printfn "Road Status" printfn "R - Regular traffic" printfn "S - Sensitive Area" printfn "Z - School Zone" printfn "U - Unknown or information not available" printf "Enter the road status (Type R, S, or Z)? " let status = char(stdin.ReadLine()) let valid = status <> 'R' && status <> 'U' if valid = true then ticket <- 90 printfn "=====================================================================================================" printfn "Ticket Amount: %d" ticket printfn "====================================================================================================="
Here is an example of testing the program:
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 Enter the road status (Type R, S, or Z)? R ===================================================================================================== Ticket Amount: 45 ===================================================================================================== Press any key to close this window . . .
Here is another example of testing the program:
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 Enter the road status (Type R, S, or Z)? S ===================================================================================================== Ticket Amount: 90 ===================================================================================================== Press any key to close this window . . .
A Conjunction for Non-Equivalence
The <, <=, >, and >= operators can be applied to any 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:
printfn "================================================================" printfn "Job Interview" printfn "----------------------------------------------------------------" printfn "To grant you this job, we need to ask a few questions." printfn "Levels of Security Clearance:" printfn "0. Unknown or None" printfn "1. Non-Sensitive" printfn "2. National Security - Non-Critical Sensitive" printfn "3. National Security - Critical Sensitive" printfn "4. National Security - Special Sensitive" printfn "----------------------------------------------------------------" printf "Type your level (1-4): " let level = int(stdin.ReadLine()); printfn "================================================================" if level >= 2 && level <= 4 then printfn "Welcome on board." else printfn "We will get back to you..." printfn "================================================================"
Here is an example of running the program:
================================================================ Job Interview ---------------------------------------------------------------- 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 ---------------------------------------------------------------- Type your level (1-4): 2 ================================================================ Welcome on board. ================================================================ Press any key to close this window . . .
Here is another example of running the program:
================================================================ Job Interview ---------------------------------------------------------------- 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 ---------------------------------------------------------------- Type your level (1-4): 1 ================================================================ We will get back to you... ================================================================ Press any key to close this window . . .
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 = operator while the other condition uses a >= operator:
let mutable name = "Gabrielle Towa" let mutable timeWorked = 42.50 let mutable status = "Full-Time" let mutable verdict = status = "Full-Time" && timeWorked >= 40.00 printfn "Employee Name: %s" name printfn "Time Worked: %0.2f" timeWorked printfn "Employment Status: %s" status printfn "Condition-1 AND Condition-2: %b" verdict printfn "--------------------------------------------------------------------------------------" // Both conditions are True: if verdict = true then printfn "The time sheet has been approved." else printfn "Part-time employees are not allowed to work overtime." printfn "======================================================================================" name <- "Stephen Anders" status <- "Full-Time" timeWorked <- 38.00 verdict <- status = "Full-Time" && timeWorked >= 40.00 printfn "Employee Name: %s" name printfn "Time Worked: %0.2f" timeWorked printfn "Employment Status: %s" status printfn "Condition-1 AND Condition-2: %b" verdict printfn "--------------------------------------------------------------------------------------" // The first condition is True AND the second condition is False: if verdict = true then printfn "The time sheet has been approved." else printfn "Time Sheet under review: Every full-time employee must work at least 40 hours a week." printfn "======================================================================================" name <- "Rose Sandt" status <- "Part-Time" timeWorked <- 44.00 verdict <- status = "Full-Time" && timeWorked >= 40.00 printfn "Employee Name: %s" name printfn "Time Worked: %0.2f" timeWorked printfn "Employment Status: %s" status printfn "Condition-1 AND Condition-2: %b" verdict printfn "--------------------------------------------------------------------------------------" // The first condition is False AND the second condition is True: if verdict = true then printfn "The time sheet has been approved." else printfn "Part-time employees are not allowed to work overtime." printfn "======================================================================================" name <- "Joseph Lee" status <- "Part-Time" timeWorked <- 36.50 verdict <- status = "Full-Time" && timeWorked >= 40.00 printfn "Employee Name: %s" name printfn "Time Worked: %0.2f" timeWorked printfn "Employment Status: %s" status printfn "Condition-1 AND Condition-2: %b" verdict printfn "--------------------------------------------------------------------------------------" // The first condition is False AND the second condition is False: if verdict = true then printfn "Part-time employees work part-time." else printfn "Part-time employees are not allowed to work overtime." printfn "======================================================================================"
This would produce:
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. ====================================================================================== Press any key to close this window . . .
In the above example, we wrote each conjunction as one unit (such as status = "Full-Time" && timeWorked >= 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:
let mutable name = "Gabrielle Towa" let mutable timeWorked = 42.50 let mutable status = "Full-Time" let mutable verdict = (status = "Full-Time") && (timeWorked >= 40.00) printfn "Employee Name: %s" name printfn "Time Worked: %0.2f" timeWorked printfn "Employment Status: %s" status printfn "Condition-1 AND Condition-2: %b" verdict printfn "--------------------------------------------------------------------------------------" // Both conditions are True: if verdict = true then printfn "The time sheet has been approved." else printfn "Part-time employees are not allowed to work overtime." printfn "======================================================================================" name <- "Stephen Anders" status <- "Full-Time" timeWorked <- 38.00 verdict <- (status = "Full-Time") && (timeWorked >= 40.00) printfn "Employee Name: %s" name printfn "Time Worked: %0.2f" timeWorked printfn "Employment Status: %s" status printfn "Condition-1 AND Condition-2: %b" verdict printfn "--------------------------------------------------------------------------------------" // The first condition is True AND the second condition is False: if verdict = true then printfn "The time sheet has been approved." else printfn "Time Sheet under review: Every full-time employee must work at least 40 hours a week." printfn "======================================================================================" name <- "Rose Sandt" status <- "Part-Time" timeWorked <- 44.00 verdict <- (status = "Full-Time") && (timeWorked >= 40.00) printfn "Employee Name: %s" name printfn "Time Worked: %0.2f" timeWorked printfn "Employment Status: %s" status printfn "Condition-1 AND Condition-2: %b" verdict printfn "--------------------------------------------------------------------------------------" // The first condition is False AND the second condition is True: if verdict = true then printfn "The time sheet has been approved." else printfn "Part-time employees are not allowed to work overtime." printfn "======================================================================================" name <- "Joseph Lee" status <- "Part-Time" timeWorked <- 36.50 verdict <- (status = "Full-Time") && (timeWorked >= 40.00) printfn "Employee Name: %s" name printfn "Time Worked: %0.2f" timeWorked printfn "Employment Status: %s" status printfn "Condition-1 AND Condition-2: %b" verdict printfn "--------------------------------------------------------------------------------------" // The first condition is False AND the second condition is False: if verdict = true then printfn "Part-time employees work part-time." else printfn "Part-time employees are not allowed to work overtime." printfn "======================================================================================"
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 && condition2 && condition3 && . . . && 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 the operations are true.
Of course, you can nest a Boolean condition inside another conditional statement.
|
|||
Previous | Copyright © 2009-2024, FunctionX | Saturday 16 September 2023 | Next |
|