Home

Boolean Conjunctions and Disjunctions

 

Boolean Conjunctions

 

Introduction

A conjunction consists of joining two Boolean operations. The operation is performed using the && operator. The formula to follow is:

Operation1 && Operation2

Each of the operations on both sides of the && operator must produce a true or a false result. The result is as follows:

  • If Operation1 is true and Operation2 is true, the whole expression produces a true result. Here is an example:
    let mutable fullName = "Angie Chapman";
    let mutable jobCategory = "Supervisor";
    let mutable status = "Full Time";
    let mutable salary = 0;
    
    if jobCategory = "Supervisor" && status = "Full Time" then
        salary <- 85000;
    
    printfn "Employee Record";
    printfn "Full Name:     %s" fullName;
    printfn "Yearly Salary: %d" salary;
    This would produce:
    Employee Record
    Full Name:     Angie Chapman
    Yearly Salary: 85000
    Press any key to continue . . .
  • If either Operation1 or Operation2 is false (including if both are false), the whole expression produces a false result. Here is an example:
    let mutable fullName = "Angie Chapman";
    let mutable jobCategory = "Supervisor";
    let mutable status = "Full Time";
    let mutable salary = 0;
    
    if jobCategory = "Supervisor" && status = "Full Time" then
        salary <- 85000;
    else
        salary <- 40000;
    
    printfn "Employee Record";
    printfn "Full Name:     %s" fullName;
    printfn "Yearly Salary: %d" salary;
    printfn "-----------------------------------";
    
    fullName <- "Anthony Feyer";
    jobCategory <- "Intern";
    status <- "Full Time";
    
    if jobCategory = "Supervisor" && status = "Full Time" then
        salary <- 52460;
    else
        salary <- 40000;
    
    printfn "Employee Record";
    printfn "Full Name:     %s" fullName;
    printfn "Yearly Salary: %d" salary;
    This would produce:
    Employee Record
    Full Name:     Angie Chapman
    Yearly Salary: 85000
    -----------------------------------
    Employee Record
    Full Name:     Anthony Feyer
    Yearly Salary: 40000
    
    Press any key to continue . . .

To make your code easier to read, it is a good idea to include each Boolean operation in its own parentheses. Here are examples:

let mutable fullName = "Angie Chapman";
let mutable jobCategory = "Supervisor";
let mutable status = 1;
let mutable salary = 0;

if (jobCategory = "Supervisor") && (status = 1) then
    salary <- 85000;
else
    salary <- 40000;

printfn "Employee Record";
printfn "Full Name:     %s" fullName;
match status with
| 0 -> printfn "Employment Status: Part Time"
| 1 -> printfn "Employment Status: Full Time"
| _ -> printfn "Employment Status: Unknown"
printfn "Yearly Salary: %d" salary;
printfn "-----------------------------------";

fullName <- "Anthony Feyer";
jobCategory <- "Intern";
status <- 0;

if (jobCategory = "Supervisor") && (status = 1) then
    salary <- 52460;
else
    salary <- 40000;

printfn "Employee Record";
printfn "Full Name:     %s" fullName;
match status with
| 0 -> printfn "Employment Status: Part Time"
| 1 -> printfn "Employment Status: Full Time"
| _ -> printfn "Employment Status: Unknown"
printfn "Yearly Salary: %d" salary;

This would produce:

Employee Record
Full Name:     Angie Chapman
Employment Status: Full Time
Yearly Salary: 85000
-----------------------------------
Employee Record
Full Name:     Anthony Feyer
Employment Status: Part Time
Yearly Salary: 40000

Press any key to continue . . .

In the same way, you can join as many Boolean operations as you want:

Operation1 && Operation2 && Operation3 && Operation_n

When the expression is checked, if any of the operations is false, the whole operation is false. The only time the whole operation is true is if all of the operations are true.

 
   
 

Disjunctions

A disjunction is a combination of Boolean operations where at least one of the operations needs to be true for the whole operation to be true. The operation is performed using the || operator. The formula to follow is:

Operation1 || Operation2

The operation as a whole works as follows:

  • If at least one of the individual Boolean operations is true regardless of the result of the other operation(s), the whole operation produces a true result. Here is an example:
    let mutable fullName = "Bertha Grizzler";
    let mutable membershipCategory = 1;
    let mutable fee = 0.00;
    
    if membershipCategory = 1 || membershipCategory = 3 then
        fee <- 45.00;
    else
        fee <- 85.00;
    
    printfn "Sports Club Membership";
    printfn "Member Name:         %s" fullName;
    match membershipCategory with
    | 1 -> printfn "Membership Category: Teen"
    | 2 -> printfn "Membership Category: Adult"
    | 3 -> printfn "Membership Category: Senior"
    | _ -> printfn "Employment Status: Unknown"
    printfn "Yearly Fee:          %.2f" fee;
    This would produce:
    Sports Club Membership
    Member Name:         Bertha Grizzler
    Membership Category: Teen
    Yearly Fee:          45.00
    
    Press any key to continue . . .
    As mentioned for the conjunction, it is a good idea to include each Boolean operation in its parentheses
  • If all operations are false, only then will the whole operation produce a false result. Here is an example:
    let mutable fullName = "Bertha Grizzler";
    let mutable membershipCategory = 1;
    let mutable fee = 0.00;
    
    if (membershipCategory = 1) || (membershipCategory = 3) then
        fee <- 45.00;
    else
        fee <- 85.00;
    
    printfn "Sport Club Membership";
    printfn "Member Name:         %s" fullName;
    match membershipCategory with
    | 1 -> printfn "Membership Category: Teen"
    | 2 -> printfn "Membership Category: Adult"
    | 3 -> printfn "Membership Category: Senior"
    | _ -> printfn "Employment Status: Unknown"
    printfn "Yearly Fee:          %.2f" fee;
    printfn "-----------------------------------";
    
    fullName <- "Jennifer Jacobson";
    membershipCategory <- 2;
    
    if (membershipCategory = 1) || (membershipCategory = 3) then
        fee <- 45.00;
    else
        fee <- 85.00;
    
    printfn "Sport Club Membership";
    printfn "Member Name:         %s" fullName;
    match membershipCategory with
    | 1 -> printfn "Membership Category: Teen"
    | 2 -> printfn "Membership Category: Adult"
    | 3 -> printfn "Membership Category: Senior"
    | _ -> printfn "Employment Status: Unknown"
    printfn "Yearly Fee:          %.2f" fee;
    printfn "-----------------------------------";
    
    fullName <- "Samuel Aronson";
    membershipCategory <- 3;
    
    if (membershipCategory = 1) || (membershipCategory = 3) then
        fee <- 45.00;
    else
        fee <- 85.00;
    
    printfn "Sport Club Membership";
    printfn "Member Name:         %s" fullName;
    match membershipCategory with
    | 1 -> printfn "Membership Category: Teen"
    | 2 -> printfn "Membership Category: Adult"
    | 3 -> printfn "Membership Category: Senior"
    | _ -> printfn "Employment Status: Unknown"
    printfn "Yearly Fee:          %.2f" fee;
    This would produce:
    Sport Club Membership
    Member Name:         Bertha Grizzler
    Membership Category: Teen
    Yearly Fee:          45.00
    -----------------------------------
    Sport Club Membership
    Member Name:         Jennifer Jacobson
    Membership Category: Adult
    Yearly Fee:          85.00
    -----------------------------------
    Sport Club Membership
    Member Name:         Samuel Aronson
    Membership Category: Senior
    Yearly Fee:          45.00
    
    Press any key to continue . . .

You can create a conditional statement that includes as many disjunctions as you want:

Operation1 || Operation2 || Operation_n

The rule is the same: If any on the individual operations is false, the whole operation is false. The whole operation is true only if all of the operations are false.

 
 
   
 

Previous Copyright © 2009-2015 FunctionX Next