Boolean Disjunctions

Introduction

A Boolean disjunction is a conditional statement where you combine more than one condition but only one of the conditions needs to be true for the whole operation to be true. This operation is performed using the Boolean "OR" operator. The primary formula to follow is:

condition1 OR condition2

The operation works as follows:

The operation can be resumed as follows:

Condition 1 Condition 2 Condition 1 AND Condition 2 Condition 1 OR Condition 2
False False False False
False True False True
True False False True
True True True True

To perform this operation in C#, you can use an operator represented as ||. As seen with the Boolean conjunction, the formula for a Boolean disjunction is:

condition1 || condition2

We already saw that a condition is formulated as:

operand1 Boolean-operator operand2

As a result, a Boolean disjunction can be formulated as follows:

operand1 operator1 operand2 || operand3 operator2 operand4

Practical LearningPractical Learning: Introducing Boolean Conjunctions

  1. Start Microsoft Visual Studio and create a new ASP.NET Core Web App named TaxPreparation03. Uncheck the Configure For HTTPS check box
  2. In the Solution Explorer, expand wwwroot
  3. In the Solution Explorer, underwwwrppt, right-click css -> Add -> New Item...
  4. In the left list of the Add New Item dialog box, under Visual C#, expand ASP.NET Core. Expand Web, and click Content
  5. In the middle list, click Style Sheet
  6. Change the file Name to TaxPreparation
  7. Click Add
  8. Change the document as follows:
    body {
    }
    
    .delimiter   { margin:      auto;
                   width:       450px; }
    .common-font { font-family: Georgia, Garamond, 'Times New Roman', serif; }
  9. In the Solution Explorer, under Pages, expand Shared
  10. In the Solution Explorer, under Pages and under Shared, double-click _Layout.cshtml
  11. Change the document as follows:
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>@ViewData["Title"] - DelTax Services</title>
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
        <link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/TaxPreparation03.styles.css" asp-append-version="true" />
        <link rel="stylesheet" href="~/css/TaxPreparation.css" asp-append-version="true" />
    </head>
    <body>
        <header>
            <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
                <div class="container">
                    <a class="navbar-brand" asp-area="" asp-page="/Index">DelTax Home</a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                            aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                    </button>
                    <div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
                        <ul class="navbar-nav flex-grow-1">
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-dark" asp-area="" asp-page="/Privacy">Privacy</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
        </header>
        <div class="container">
            <main role="main" class="pb-3">
                @RenderBody()
            </main>
        </div>
    
        <footer class="border-top footer text-muted">
            <div class="container">
                <p class="text-center common-font">&copy; 2022 - DelTax Services - <a asp-area="" asp-page="/Privacy">Privacy</a></p>
            </div>
        </footer>
    
        <script src="~/lib/jquery/dist/jquery.min.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
        <script src="~/js/site.js" asp-append-version="true"></script>
    
        @await RenderSectionAsync("Scripts", required: false)
    </body>
    </html>
  12. In the Solution Explorer, expand Pages
  13. In the Solution Explorer, under Pages, double-click Index.cshtml

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:

@page
@model Valuable.Pages.ExerciseModel

@{
    string degree = "y";
    bool certified = false;
    string strDecision = "";

    if( degree == "y" || certified == true )
        strDecision = "Congratulations: We need to schedule the next interview (Human Resources) for you.";
    else
        strDecision = "This job requires either a college degree or a professional certification in application programming.";
}

<h3>Employment Application</h3>
<hr />
<pre>Does the candidate hold a college degree? @degree
Is the candidate a certified programmer (Microsoft, Oracle, etc)? @certified
--------------------------------------------------------------------------------------------
Decision: @strDecision
============================================================================================</pre>

This would produce:

Employment Application

Does the candidate hold a college degree? y
Is the candidate a certified programmer (Microsoft, Oracle, etc)? False
----------------------------------------------------------------------------
Decision: Congratulations: We need to schedule the next interview (Human Resources) for you.

Practical LearningPractical Learning: Introducing Logical Disjunctions

  1. Change the document as follows:
    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    
    @{
        string strNetPay = "";
        string strTaxRate = "";
        string strTaxAmount = "";
    
        string strStateName = "";
        string strGrossSalary = "";
    
        if (Request.HasFormContentType)
        {
            double taxRate = 0.00;
            double grossSalary = double.Parse(Request.Form["txtGrossSalary"]);
    
            strStateName = Request.Form["selStates"];
    
            if( (strStateName == "Illinois") || (strStateName == "Utah") )
                taxRate = 4.95;
            
            double taxAmount = grossSalary * taxRate / 100.00;
            double netPay = grossSalary - taxAmount;
    
            strGrossSalary = $"{grossSalary:F}";
            strTaxRate = $"{taxRate/100:P}";
    
            strTaxAmount = $"{taxAmount:F}";
            strNetPay = $"{(grossSalary - taxAmount):F}";
        }
    }
    
    <h1 class="text-center common-font">- State Income Tax -</h1>
    <hr />
    <div class="delimiter common-font">
        <form name="frmTaxPreparation" method="post">
            <table align="center">
                <tr>
                    <td style="width: 200px">Select a State:</td>
                    <td>
                        <select id="selStates" name="selStates">
                            <option value="Alaska">Alaska</option>
                            <option value="Arkansas">Arkansas</option>
                            <option value="Colorado">Colorado</option>
                            <option value="Florida">Florida</option>
                            <option value="Georgia">Georgia</option>
                            <option value="Illinois">Illinois</option>
                            <option value="Indiana">Indiana</option>
                            <option value="Kentucky">Kentucky</option>
                            <option value="Massachusetts">Massachusetts</option>
                            <option value="Michigan">Michigan</option>
                            <option value="Missouri">Missouri</option>
                            <option value="Mississippi">Mississippi</option>
                            <option value="Nevada">Nevada</option>
                            <option value="New Hampshire">New Hampshire</option>
                            <option value="North Carolina">North Carolina</option>
                            <option value="Pennsylvania">Pennsylvania</option>
                            <option value="South Dakota">South Dakota</option>
                            <option value="Tennessee">Tennessee</option>
                            <option value="Texas">Texas</option>
                            <option value="Utah">Utah</option>
                            <option value="Washington">Washington</option>
                            <option value="Wyoming">Wyoming</option>
                        </select>
                    </td>
                </tr>
            </table>
    
        <hr />
        
        <table class="table">
            <tr>
                <td width="125"><label for="txtGrossSalary">Gross Salary:</label></td>
                <td><input type="text" id="txtGrossSalary" name="txtGrossSalary" value="@strGrossSalary" class="form-control" /></td>
            </tr>
            <tr>
                <td></td>
                <td class="pcenter"><input type="submit" value="Evaluate Taxes" class="btn-primary" /></td>
            </tr>
        </table>
        </form>
        
        <table class="table">
            <tr>
                <td width="125">Gross Salary:</td>
                <td>@strGrossSalary</td>
            </tr>
            <tr>
                <td>State Name:</td>
                <td>@strStateName</td>
            </tr>
            <tr>
                <td>Tax Rate:</td>
                <td>@strTaxRate</td>
            </tr>
            <tr>
                <td>Tax Amount:</td>
                <td>@strTaxAmount</td>
            </tr>
            <tr>
                <td>Net Pay:</td>
                <td>@strNetPay</td>
            </tr>
        </table>
    </div>
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging
  3. In the Select a State combo box select Utah
  4. Click the Gross Salary text box and type 1495.75
  5. Click the Evaluate Taxes button:

    Conjunctional Disjunctions

  6. Return to your programming environment

A Disjunction for Non-Equivalence

As with a conjunction, in a Boolean disjunction, one or both of the conditions can use an operator that is neither equality (==) nor non-equality (!=). For one of those other operator, you can precede the operator with the is keyword. Here is an example:

@page
@model Valuable.Pages.ExerciseModel

@{
    int security = 3;
    int education = 1;
    string strDecision = "";
    
    if( security is >= 2 || education is > 3 )
        strDecision = "Congratulations: Welcome on board.";
    else
        strDecision = "Well... We will get back to you...";
}

<h3>Employment Application</h3>
<hr />
<pre>Job Requirements Evaluation
======================================================
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
------------------------------------------------------
Levels of Education:
1. Kindergarten or Elementary School
2. High School
3. Some College or Associate Degree
4. Bachelor's Degree
5. Graduate Degree
======================================================
After candidate's evaluation
Security Level: @security
Level of Education: @education
======================================================
Decision: @strDecision
======================================================</pre>

This would produce:

Employment Application

Job Requirements Evaluation
======================================================
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
------------------------------------------------------
Levels of Education:
1. Kindergarten or Elementary School
2. High School
3. Some College or Associate Degree
4. Bachelor's Degree
5. Graduate Degree
======================================================
After candidate's evaluation
Security Level: 3
Level of Education: 1
======================================================
Decision: Congratulations: Welcome on board.
======================================================

In some cases, you can use the same variable for both conditions that use the "Less Than" (<), the "Less Than or Equal" (<=), the "Greater Than" (>), or the "Greater Than or Equal" (>=) operator. To make to make your code easy to read, you can use an operator named or. This operator is combined with the is operator. The formula to follow is:

operand is condition1 or condition2

Start with variable that both conditions use. The variable must be followed by the is operator. Then formulate each conditional using the "Less Than" (<), the "Less Than or Equal" (<=), the "Greater Than" (>), or the "Greater Than or Equal" (>=) operator. Separate both conditions with the or operator. Here is an example:

@page
@model Valuable.Pages.ExerciseModel

@{
    int security = 3;
    int education = 3;
    string strDecision = "";
    
    if( security is >= 2 or > 3 )
        strDecision = "Congratulations: Welcome on board.";
    else
        strDecision = "Well... We will get back to you...";
}

<h3>Employment Application</h3>
<hr />
<pre>Job Requirements Evaluation
======================================================
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
------------------------------------------------------
Levels of Education:
1. Kindergarten or Elementary School
2. High School
3. Some College or Associate Degree
4. Bachelor's Degree
5. Graduate Degree
======================================================
Candidate's Evaluation:
Level of Security Clearance: @security
Level of Education: @education
------------------------------------------------------
Decision: @strDecision
======================================================</pre>

This would produce:

Employment Application

Job Requirements Evaluation
======================================================
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
------------------------------------------------------
Levels of Education:
1. Kindergarten or Elementary School
2. High School
3. Some College or Associate Degree
4. Bachelor's Degree
5. Graduate Degree
======================================================
Candidate's Evaluation:
Level of Security Clearance: 3
Level of Education: 3
------------------------------------------------------
Decision: Congratulations: Welcome on board.
======================================================

To make your code easy to read, you can include each condition in its own parentheses. This can be done as follows:

@page
@model Valuable.Pages.ExerciseModel

@{
    int security = 1;
    int education = 4;
    string strDecision = "";
    
    if( security is (>= 2) or (> 3) )
        strDecision = "Congratulations: Welcome on board.";
    else
        strDecision = "Well... We will get back to you...";
}

<h3>Employment Application</h3>
<hr />
<pre>Job Requirements Evaluation
======================================================
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
------------------------------------------------------
Levels of Education:
1. Kindergarten or Elementary School
2. High School
3. Some College or Associate Degree
4. Bachelor's Degree
5. Graduate Degree
======================================================
Candidate's Evaluation:
Level of Security Clearance: @security
Level of Education: @education
------------------------------------------------------
Decision: @strDecision
======================================================</pre>

This would produce:

Employment Application

Job Requirements Evaluation
======================================================
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
------------------------------------------------------
Levels of Education:
1. Kindergarten or Elementary School
2. High School
3. Some College or Associate Degree
4. Bachelor's Degree
5. Graduate Degree
======================================================
Candidate's Evaluation:
Level of Security Clearance: 1
Level of Education: 4
------------------------------------------------------
Decision: Well... We will get back to you...
======================================================

Combining Disjunctions

You can create a conditional statement that includes as many disjunctions as you want. The formula to follow is:

condition1 || condition2 || . . . || 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 conditions:

@page
@model Valuable.Pages.ExerciseModel

@{
    int level = 3;
    string strDecision = "";
    
    if( level == 2 || level == 3 || level == 4 )
        strDecision = "Congratulations: Welcome on board.";
    else
        strDecision = "Well... We will get back to you...";
}

<h3>Employment Application</h3>
<hr />
<pre>================================================================
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
================================================================
Candidate's Evaluation:
Level of Security Clearance: @level
----------------------------------------------------------------
Decision: @strDecision
================================================================</pre>

This would produce:

Employment Application

================================================================
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
================================================================
Candidate's Evaluation:
Level of Security Clearance: 3
----------------------------------------------------------------
Decision: Congratulations: Welcome on board.
================================================================

Practical LearningPractical Learning: Introducing Logical Disjunctions

  1. Change the code as follows:
    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    
    @{
        string strNetPay = "";
        string strTaxRate = "";
        string strTaxAmount = "";
    
        string strStateName = "";
        string strGrossSalary = "";
    
        if (Request.HasFormContentType)
        {
            double taxRate = 0.00;
            double grossSalary = double.Parse(Request.Form["txtGrossSalary"]);
    
            strStateName = Request.Form["selStates"];
    
            if( (strStateName == "Illinois") || (strStateName == "Utah") )
                taxRate = 4.95;
            else if ((strStateName == "Kentucky") || (strStateName == "Massachusetts") || (strStateName == "New Hampshire"))
                taxRate = 5.00;
            else if ((strStateName == "Florida") || (strStateName == "Nevada") || (strStateName == "Texas") || (strStateName == "Washington") || (strStateName == "Wyoming"))
                taxRate = 0.00;
            
            double taxAmount = grossSalary * taxRate / 100.00;
            double netPay = grossSalary - taxAmount;
    
            strGrossSalary = $"{grossSalary:F}";
            strTaxRate = $"{taxRate/100:P}";
    
            strTaxAmount = $"{taxAmount:F}";
            strNetPay = $"{(grossSalary - taxAmount):F}";
        }
    }
    
    <h1 class="text-center common-font">- State Income Tax -</h1>
    <hr />
    <div class="delimiter common-font">
        <form name="frmTaxPreparation" method="post">
            <table align="center">
                <tr>
                    <td style="width: 200px">Select a State:</td>
                    <td>
                        <select id="selStates" name="selStates">
                            <option value="Alaska">Alaska</option>
                            <option value="Arkansas">Arkansas</option>
                            <option value="Colorado">Colorado</option>
                            <option value="Florida">Florida</option>
                            <option value="Georgia">Georgia</option>
                            <option value="Illinois">Illinois</option>
                            <option value="Indiana">Indiana</option>
                            <option value="Kentucky">Kentucky</option>
                            <option value="Massachusetts">Massachusetts</option>
                            <option value="Michigan">Michigan</option>
                            <option value="Missouri">Missouri</option>
                            <option value="Mississippi">Mississippi</option>
                            <option value="Nevada">Nevada</option>
                            <option value="New Hampshire">New Hampshire</option>
                            <option value="North Carolina">North Carolina</option>
                            <option value="Pennsylvania">Pennsylvania</option>
                            <option value="South Dakota">South Dakota</option>
                            <option value="Tennessee">Tennessee</option>
                            <option value="Texas">Texas</option>
                            <option value="Utah">Utah</option>
                            <option value="Washington">Washington</option>
                            <option value="Wyoming">Wyoming</option>
                        </select>
                    </td>
                </tr>
            </table>
    
        <hr />
        
        <table class="table">
            <tr>
                <td width="125"><label for="txtGrossSalary">Gross Salary:</label></td>
                <td><input type="text" id="txtGrossSalary" name="txtGrossSalary" value="@strGrossSalary" class="form-control" /></td>
            </tr>
            <tr>
                <td></td>
                <td class="pcenter"><input type="submit" value="Evaluate Taxes" class="btn-primary" /></td>
            </tr>
        </table>
        </form>
        
        <table class="table">
            <tr>
                <td width="125">Gross Salary:</td>
                <td>@strGrossSalary</td>
            </tr>
            <tr>
                <td>State Name:</td>
                <td>@strStateName</td>
            </tr>
            <tr>
                <td>Tax Rate:</td>
                <td>@strTaxRate</td>
            </tr>
            <tr>
                <td>Tax Amount:</td>
                <td>@strTaxAmount</td>
            </tr>
            <tr>
                <td>Net Pay:</td>
                <td>@strNetPay</td>
            </tr>
        </table>
    </div>
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging:
  3. In the Select a State combo box, select Massachusetts
  4. Click the Evaluate Taxes button:

    Conjunctional Disjunctions

  5. In the Select a State combo box, select Texas
  6. Click the Evaluate Taxes button:

    Conjunctional Disjunctions

  7. Return to your programming environment
  8. Close your programming environment

Previous Copyright © 2001-2022, FunctionX Thursday 28 October 2021 Next