Boolean Disjunctions

Introduction

A Boolean disjunction is a conditional statement where you combine more than one condition but you are trying to know whether at least one of them is false. This operation is commonly represented by a Boolean operator named "OR" operator. The primary formula to follow is:

condition1 OR condition2

Once again, each condition is formulated as a Boolean operation:

operand1 Boolean-operator operand2 OR operand3 Boolean-operator operand4

In that case, a conditional disjunction can be formulated as follows:

operand1 Boolean-operator operand2

As seen with conjunctions, the whole disjunction operation can be assigned to a Boolean variable or it can be written in the parentheses of a conditional statement.

Practical LearningPractical Learning: Introducing Boolean Conjunctions

  1. Start Microsoft Visual Studio and create a new Console App that supports .NET 8.0 (Long-Term Support) named TaxPreparation04
  2. Change the document as follows:
    using static System.Console;
    
    WriteLine("==========================================");
    WriteLine(" - Amazing DeltaX - State Income Tax -");
    WriteLine("==========================================");
    
    double taxRate = 0.00;
    string stateName = "";
    
    WriteLine("Enter the information for tax preparation");
    WriteLine("States");
    WriteLine(" AK - Alaska");
    WriteLine(" AR - Arkansas");
    WriteLine(" CO - Colorado");
    WriteLine(" FL - Florida");
    WriteLine(" GA - Georgia");
    WriteLine(" IL - Illinois");
    WriteLine(" IN - Indiana");
    WriteLine(" KY - Kentucky");
    WriteLine(" MA - Massachusetts");
    WriteLine(" MI - Michigan");
    WriteLine(" MO - Missouri");
    WriteLine(" MS - Mississippi");
    WriteLine(" NV - Nevada");
    WriteLine(" NH - New Hampshire");
    WriteLine(" NC - North Carolina");
    WriteLine(" PA - Pennsylvania");
    WriteLine(" SD - South Dakota");
    WriteLine(" TN - Tennessee");
    WriteLine(" TX - Texas");
    WriteLine(" UT - Utah");
    WriteLine(" WA - Washington");
    WriteLine(" WY - Wyoming");
    Write("Enter State Abbreviation: ");
    string abbr = ReadLine();
    WriteLine("--------------------------------------------");
    
    Write("Gross Salary: ");
    double grossSalary = double.Parse(ReadLine());
    
    double taxAmount = grossSalary * taxRate / 100.00;
    double netPay = grossSalary - taxAmount;
    
    WriteLine("============================================");
    WriteLine(" - Amazing DeltaX - State Income Tax -");
    WriteLine("--------------------------------------------");
    WriteLine($"State Name:   {stateName}");
    WriteLine($"Gross Salary: {grossSalary:f}");
    WriteLine($"Tax Rate:     {taxRate:f}%");
    WriteLine("--------------------------------------------");
    WriteLine($"Tax Amount:   {taxAmount:f}");
    WriteLine($"Net Pay:      {netPay:f}");
    WriteLine("============================================");

A Binary Logical Exclusion

Imagine you have a variable that can hold different values. At one time, you may want to know whether the variable is holding this or that value. This operation is referred to as a disjunction. Remember that a Boolean disjunction uses an "OR" operator and can be formulated as follows:

condition1 OR condition2

A binary exclusion is a disjunction where you want to know whether one of the conditions is the opposite of the other. This operation is evaluated as follows:

This operation is referred to as binary logical exclusion. It is named XOR and it is performed with an operator known as ∧. The operation can be resumed as follows:

Condition-1 Condition-2 Condition-1 XOR Condition-2
True False True
False True True
True True False
False False False

The binary logical exclusion can be presented as follows:

condition1  condition2

From the previous lesson, remember that each condition can be formulated as follows:

operand1 Boolean-operator operand2

In that case, a binary logical exclusion can be formulated as follows:

operand1 operator1 operand2  operand3 operator2 operand4

Remember that you can assign the expression to a variable as follows:

bool variable-name = condition1  condition2;

Otherwise, you can include the expression in the parenthesis of a conditional statement, as in:

if(condition1  condition2)
    statement(s);

A Boolean Operator for a Disjunction

When you are performing a logical disjunction, you can use any of the comparison operators we saw already (!, ==, !=, <, <=, >, >=, is, and not). When we studied conjunctions, we saw that you cannot logically use the same variable in two equality or non-equality conditions of a conjunction. In a disjunction, you can use the same variable or different variables in conditions. Here are examples that perform logical exclusions:

using static System.Console;

string name = "Gabrielle Towa";
double timeWorked = 42.50;
string status1 = "Part-Time";
// The first condition is True BUT the second condition is False:
bool verdict = status1 == "Part-Time" ^ status1 == "Intern";

WriteLine("Employee Name:             {0}", name);
WriteLine("Time Worked:               {0}", timeWorked);
WriteLine("Employment Status:         {0}", status1);
WriteLine("Condition-1 ^ Condition-2: {0}", verdict);
WriteLine("-------------------------------------------------------------");

if (verdict == true)
    WriteLine("The time sheet has been approved.");
else
    WriteLine("Part-time employees are not allowed to work overtime.");
WriteLine("=============================================================");

name = "Stephen Anders";
string status2 = "Seasonal";
timeWorked = 38.00;
// The first condition is False BUT the second condition is True:
verdict = status2 == "Full-Time" ^ status2 == "Seasonal";

WriteLine("Employee Name:             {0}", name);
WriteLine("Time Worked:               {0}", timeWorked);
WriteLine("Employment Status:         {0}", status2);
WriteLine("Condition-1 ^ Condition-2: {0}", verdict);
WriteLine("-------------------------------------------------------------");

if (verdict == true)
    WriteLine("The time sheet has been approved.");
else
    WriteLine("Time Sheet under review.");
WriteLine("==============================================================");

name = "Rose Sandt";
string status3 = "Full-Time";
timeWorked = 44.00;
// Both conditions are True:
verdict = status3 == "Full-Time" ^ status2 == "Seasonal";

WriteLine("Employee Name:             {0}", name);
WriteLine("Time Worked:               {0}", timeWorked);
WriteLine("Employment Status:         {0}", status3);
WriteLine("Condition-1 ^ Condition-2: {0}", verdict);
WriteLine("-------------------------------------------------------------");

if (verdict == true)
    WriteLine("The time sheet has been approved.");
else
    WriteLine("Part-time and seasonal employees must consult the management.");
WriteLine("=============================================================");

name = "Joseph Lee";
string status4 = "Intern";
timeWorked = 36.50;
// Both conditions are False:
verdict = status4 == "Unknown" ^ status4 == "No Availability";

WriteLine("Employee Name:             {0}", name);
WriteLine("Time Worked:               {0}", timeWorked);
WriteLine("Employment Status:         {0}", status4);
WriteLine("Condition-1 ^ Condition-2: {0}", verdict);
WriteLine("-------------------------------------------------------------");

if (verdict == true)
    WriteLine("Employees time sheets are subject to approval.");
else
    WriteLine("New employees must consult the Human Resources department.");
WriteLine("=============================================================");

This would produce:

Employee Name:             Gabrielle Towa
Time Worked:               42.5
Employment Status:         Part-Time
Condition-1 ^ Condition-2: True
-------------------------------------------------------------
The time sheet has been approved.
=============================================================
Employee Name:             Stephen Anders
Time Worked:               38
Employment Status:         Seasonal
Condition-1 ^ Condition-2: True
-------------------------------------------------------------
The time sheet has been approved.
==============================================================
Employee Name:             Rose Sandt
Time Worked:               44
Employment Status:         Full-Time
Condition-1 ^ Condition-2: False
-------------------------------------------------------------
Part-time and seasonal employees must consult the management.
=============================================================
Employee Name:             Joseph Lee
Time Worked:               36.5
Employment Status:         Intern
Condition-1 ^ Condition-2: False
-------------------------------------------------------------
New employees must consult the Human Resources department.
=============================================================

Press any key to close this window . . .

A Binary Logical Disjunction

If you need to check two conditions to validate just one of them, a logical operator you can use is represented by a beam operator |. The operation can be represented as follows:

condition1 | condition2

Remember that you can assign the expression to a variable as follows:

bool variable-name = condition1 | condition2;

Otherwise, you can include the expression in the parenthesis of a conditional statement, as in:

if(condition1 | condition2)
    statement(s);

The operation is evaluated as follows:

The operation can be resumed as follows:

Condition-1 Condition-2 Condition-1 ∧ Condition-2 Condition-1 | Condition-2
True True False True
True False True True
False True True True
False False False False

Here are examples of operations of binary logical disjunctions:

using static System.Console;

string name = "Gabrielle Towa";
double timeWorked = 42.50;
string status1 = "Part-Time";
// The first condition is True while the second condition is False:
bool verdict = status1 == "Part-Time" | status1 == "Intern";

WriteLine("Employee Name:             {0}", name);
WriteLine("Time Worked:               {0}", timeWorked);
WriteLine("Employment Status:         {0}", status1);
WriteLine("Condition-1 | Condition-2: {0}", verdict);
WriteLine("----------------------------------------------------------");

if (verdict == true)
    WriteLine("The time sheet has been approved.");
else
    WriteLine("Part-time employees are not allowed to work overtime.");
WriteLine("==========================================================");

name = "Stephen Anders";
string status2 = "Seasonal";
timeWorked = 38.00;
// The first condition is False while the second condition is True:
verdict = status2 == "Full-Time" | status2 == "Seasonal";

WriteLine("Employee Name:             {0}", name);
WriteLine("Time Worked:               {0}", timeWorked);
WriteLine("Employment Status:         {0}", status2);
WriteLine("Condition-1 | Condition-2: {0}", verdict);
WriteLine("----------------------------------------------------------");

if (verdict == true)
    WriteLine("The time sheet has been approved.");
else
    WriteLine("Time Sheet under review.");
WriteLine("==========================================================");

name = "Rose Sandt";
string status3 = "Full-Time";
timeWorked = 44.00;
// Both conditions are True:
verdict = status3 == "Full-Time" | status2 == "Seasonal";

WriteLine("Employee Name:             {0}", name);
WriteLine("Time Worked:               {0}", timeWorked);
WriteLine("Employment Status:         {0}", status3);
WriteLine("Condition-1 | Condition-2: {0}", verdict);
WriteLine("----------------------------------------------------------");

if (verdict == true)
    WriteLine("The time sheet has been approved.");
else
    WriteLine("Part-time and seasonal employees must consult the management.");
WriteLine("==========================================================");

name = "Joseph Lee";
string status4 = "Intern";
timeWorked = 36.50;
// Both conditions are False:
verdict = status4 == "Unknown" | status4 == "No Availability";

WriteLine("Employee Name:             {0}", name);
WriteLine("Time Worked:               {0}", timeWorked);
WriteLine("Employment Status:         {0}", status4);
WriteLine("Condition-1 | Condition-2: {0}", verdict);
WriteLine("----------------------------------------------------------");

if (verdict == true)
    WriteLine("Employees time sheets are subject to approval.");
else
    WriteLine("New employees must consult the Human Resources department.");
WriteLine("==========================================================");

This would produce:

Employee Name:             Gabrielle Towa
Time Worked:               42.5
Employment Status:         Part-Time
Condition-1 | Condition-2: True
----------------------------------------------------------
The time sheet has been approved.
==========================================================
Employee Name:             Stephen Anders
Time Worked:               38
Employment Status:         Seasonal
Condition-1 | Condition-2: True
----------------------------------------------------------
The time sheet has been approved.
==========================================================
Employee Name:             Rose Sandt
Time Worked:               44
Employment Status:         Full-Time
Condition-1 | Condition-2: True
----------------------------------------------------------
The time sheet has been approved.
==========================================================
Employee Name:             Joseph Lee
Time Worked:               36.5
Employment Status:         Intern
Condition-1 | Condition-2: False
----------------------------------------------------------
New employees must consult the Human Resources department.
==========================================================

Press any key to close this window . . .

Conditional Disjunctions

A Binary Conditional Disjunction

In the previous section, we saw that a binary logical expression performs a check on both conditions. In many cases, once you know the result of the first condition, the result of the second may become useless. Remember that a disjunctions is formulated as follows:

condition1 OR condition2

We already saw that a condition is formulated as:

operand1 Boolean-operator operand2

A binary conditional disjunction is an operation that works as follows:

The binary conditional disjunction is performed with an operator represented as ||. It can be formulated as follows:

condition1 || condition2

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

operand1 operator1 operand2 || operand3 operator2 operand4

The whole expression can be assigned to a Boolean variable or it can be written in the parentheses of a conditional statement. The operations can be resumed as follows:

Condition-1 Condition-2 Condition-1 & Condition-2 Condition-1 && Condition-2 Condition-1 ∧ Condition-2 Condition-1 | Condition-2 Condition-1 || Condition-2
True True True True False True True
True False False False True True True
False True False False True True True
False False False False False False False

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:

using static System.Console;

WriteLine("Job Requirements Evaluation");
WriteLine("=========================================================================================================");
Write("Do you hold a degree - Undergraduate or Graduate (y/n)?                       ");
string degree = ReadLine();
Write("Are you a certified programmer (Microsoft, Oracle, etc) (Type True or False)? ");
bool certified = bool.Parse(ReadLine());

WriteLine("---------------------------------------------------------------------------------------------------------");

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

WriteLine("=========================================================================================================");

Here is an example of running the application:

Job Requirements Evaluation
=========================================================================================================
Do you hold a degree - Undergraduate or Graduate (y/n)?                       Not at all
Are you a certified programmer (Microsoft, Oracle, etc) (Type True or False)? TRUE
---------------------------------------------------------------------------------------------------------
Congratulations: We need to schedule the next interview (Human Resources) for you.
=========================================================================================================

Press any key to close this window . . .

Here is another example of running the program:

Job Requirements Evaluation
=========================================================================================================
Do you hold a degree - Undergraduate or Graduate (y/n)?                       y
Are you a certified programmer (Microsoft, Oracle, etc) (Type True or False)? false
---------------------------------------------------------------------------------------------------------
Congratulations: We need to schedule the next interview (Human Resources) for you.
=========================================================================================================

Press any key to close this window . . .

Here is another example of the program:

Job Requirements Evaluation
=========================================================================================================
Do you hold a degree - Undergraduate or Graduate (y/n)?                       I am not sure
Are you a certified programmer (Microsoft, Oracle, etc) (Type True or False)? false
---------------------------------------------------------------------------------------------------------
This job requires either a college degree or a professional certification in application programming.
=========================================================================================================

Press any key to close this window . . .

Practical LearningPractical Learning: Introducing Logical Disjunctions

  1. Change the document as follows:
    using static System.Console;
    
    WriteLine("==========================================");
    WriteLine(" - Amazing DeltaX - State Income Tax -");
    WriteLine("==========================================");
    
    double taxRate = 0.00;
    string stateName = "";
    
    WriteLine("Enter the information for tax preparation");
    WriteLine("States");
    WriteLine(" AK - Alaska");
    WriteLine(" AR - Arkansas");
    WriteLine(" CO - Colorado");
    WriteLine(" FL - Florida");
    WriteLine(" GA - Georgia");
    WriteLine(" IL - Illinois");
    WriteLine(" IN - Indiana");
    WriteLine(" KY - Kentucky");
    WriteLine(" MA - Massachusetts");
    WriteLine(" MI - Michigan");
    WriteLine(" MO - Missouri");
    WriteLine(" MS - Mississippi");
    WriteLine(" NV - Nevada");
    WriteLine(" NH - New Hampshire");
    WriteLine(" NC - North Carolina");
    WriteLine(" PA - Pennsylvania");
    WriteLine(" SD - South Dakota");
    WriteLine(" TN - Tennessee");
    WriteLine(" TX - Texas");
    WriteLine(" UT - Utah");
    WriteLine(" WA - Washington");
    WriteLine(" WY - Wyoming");
    Write("Enter State Abbreviation: ");
    string abbr = ReadLine();
    WriteLine("--------------------------------------------");
            
    if( (abbr == "IL") || (abbr == "UT") )
                taxRate = 4.95;
    
    Write("Gross Salary: ");
    double grossSalary = double.Parse(ReadLine());
    
    double taxAmount = grossSalary * taxRate / 100.00;
    double netPay = grossSalary - taxAmount;
    
    WriteLine("============================================");
    WriteLine(" - Amazing DeltaX - State Income Tax -");
    WriteLine("--------------------------------------------");
    WriteLine($"Gross Salary: {grossSalary:f}");
    WriteLine($"Tax Rate:     {taxRate:f}%");
    WriteLine("--------------------------------------------");
    WriteLine($"Tax Amount:   {taxAmount:f}");
    WriteLine($"Net Pay:      {netPay:f}");
    WriteLine("============================================");
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging
  3. For the State Abbreviation, type UT and press Enter
  4. For the Gross Salary, type 1495.75 and press Enter:
    ==========================================
     - Amazing DeltaX - State Income Tax -
    ==========================================
    Enter the information for tax preparation
    States
     AK - Alaska
     AR - Arkansas
     CO - Colorado
     FL - Florida
     GA - Georgia
     IL - Illinois
     IN - Indiana
     KY - Kentucky
     MA - Massachusetts
     MI - Michigan
     MO - Missouri
     MS - Mississippi
     NV - Nevada
     NH - New Hampshire
     NC - North Carolina
     PA - Pennsylvania
     SD - South Dakota
     TN - Tennessee
     TX - Texas
     UT - Utah
     WA - Washington
     WY - Wyoming"
    Enter State Abbreviation: UT
    --------------------------------------------
    Gross Salary: 1495.75
    ============================================
     - Amazing DeltaX - State Income Tax -
    --------------------------------------------
    Gross Salary: 1495.75
    Tax Rate:     4.95%
    --------------------------------------------
    Tax Amount:   74.04
    Net Pay:      1421.71
    ============================================
    Press any key to close this window . . .
  5. Close the form and 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 the equality (==) nor the non-equality (!=). For one of those other operators, you can precede the operator with the is keyword. Here is an example:

using static System.Console;

WriteLine("Job Requirements Evaluation");
WriteLine("======================================================");
WriteLine("To grant you this job, we need to ask a few questions.");
WriteLine("------------------------------------------------------");
WriteLine("Levels of Security Clearance:");
WriteLine("0. Unknown or None");
WriteLine("1. Non-Sensitive");
WriteLine("2. National Security - Non-Critical Sensitive");
WriteLine("3. National Security - Critical Sensitive");
WriteLine("4. National Security - Special Sensitive");
WriteLine("------------------------------------------------------");
Write("Type your level of Security Clearance (1-4): ");
int security = int.Parse(ReadLine());
WriteLine("------------------------------------------------------");
WriteLine("Levels of Education:");
WriteLine("1. Kindergarten or Elementary School");
WriteLine("2. High School");
WriteLine("3. Some College or Associate Degree");
WriteLine("4. Bachelor's Degree");
WriteLine("5. Graduate Degree");
WriteLine("------------------------------------------------------");
Write("Type your level of Education (1-5): ");
int education = int.Parse(ReadLine());

WriteLine("======================================================");

if( security is >= 2 || education is > 3 )
    WriteLine("Congratulations: Welcome on board.");
else
    WriteLine("Well... We will get back to you...");

WriteLine("======================================================");

Here is an example of running the program:

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

Press any key to close this window . . .

Here is another example of running the program:

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

Press any key to close this window . . .

Here is one more example of running the program:

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

Press any key to close this window . . .

OR for a Disjunction

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 your code easy to read, you can use an operator named or. This operator is combined with the is operator. The primary formula to follow is:

operand is condition1 or condition2

Start with a variable that both conditions use. The variable must be followed by the is operator. Then formulate each condition 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:

using static System.Console;

WriteLine("Electric Bill Evaluation");
WriteLine("======================================================");
WriteLine("Enter the following pieces of information");
WriteLine("------------------------------------------------------");
Write("Customer ZIP Code:               ");
int zipCode      = int.Parse(ReadLine());
Write("Counter Start:                   ");
int counterStart = int.Parse(ReadLine());
Write("Counter End:                     ");
int counterEnd   = int.Parse(ReadLine());
Write("Number of Days:                  ");
int days         = int.Parse(ReadLine());
WriteLine("======================================================");

const double custChargeRate = 0.49315;
const double nrgChargeRate  = 0.16580;
const double envChargeRate  = 0.00043;

int   electricUse           = counterEnd - counterStart;

double custCharge           = days        * custChargeRate;
double energyCharge         = electricUse * nrgChargeRate;
double envControlCharge     = electricUse * envChargeRate;

double serviceTotal         = 0;

/* Let's imagine a fictitious scenario where the government 
 * wants to assist people in a certain group of ZIP codes by 
 * giving them a small discount ($3.15) in their electric bill. */
if( zipCode is <= 20628 or >= 20685 )
    serviceTotal            = custCharge + energyCharge + envControlCharge;
else
    serviceTotal            = custCharge + energyCharge + envControlCharge - 3.15;

WriteLine("Electric Bill Summary:");
WriteLine("======================================================");
WriteLine("Counter Reading Start:        {0, 8}", counterStart);
WriteLine("Counter Reading End:          {0, 8}", counterEnd);
WriteLine("Total Electric Use:           {0, 8}", electricUse);
WriteLine("Number of Days:               {0, 8}", days);
WriteLine("------------------------------------------------------");
WriteLine("Energy Charge Credit");
WriteLine("------------------------------------------------------");
WriteLine("Customer Chargee:             {0, 8:f}", custCharge);
WriteLine("Energy Charge:                {0, 8:f}", energyCharge);
WriteLine("Environmental Control Charge: {0, 8:f}", envControlCharge);
WriteLine("------------------------------------------------------");
WriteLine("Electric Servive Total:       {0, 8:f}", serviceTotal);
WriteLine("======================================================");

Here is an example of running the program:

Electric Bill Evaluation
======================================================
Enter the following pieces of information
------------------------------------------------------
Customer ZIP Code:               20625
Counter Start:                   10089
Counter End:                     11126
Number of Days:                  29
======================================================
Electric Bill Summary:
======================================================
Counter Reading Start:           10089
Counter Reading End:             11126
Total Electric Use:               1037
Number of Days:                     29
------------------------------------------------------
Energy Charge Credit
------------------------------------------------------
Customer Chargee:                14.30
Energy Charge:                  171.93
Environmental Control Charge:     0.45
------------------------------------------------------
Electric Servive Total:         186.68
======================================================

Press any key to close this window . . .

Usually, you should include the expression after is operator in parentheses. This can be done as follows:

if( zipCode is (<= 20628 or >= 20685) )
    serviceTotal = custCharge + energyCharge + envControlCharge;
else
    serviceTotal = custCharge + energyCharge + envControlCharge - 3.15;

Here is an example of running the program:

Electric Bill Evaluation
======================================================
Enter the following pieces of information
------------------------------------------------------
Customer ZIP Code:               20680
Counter Start:                   10089
Counter End:                     11126
Number of Days:                  29
======================================================
Electric Bill Summary:
======================================================
Counter Reading Start:           10089
Counter Reading End:             11126
Total Electric Use:               1037
Number of Days:                     29
------------------------------------------------------
Energy Charge Credit
------------------------------------------------------
Customer Chargee:                14.30
Energy Charge:                  171.93
Environmental Control Charge:     0.45
------------------------------------------------------
Electric Servive Total:         183.53
======================================================

Press any key to close this window . . .

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

if( zipCode is (<= 20628) or (>= 20685) )
    serviceTotal = custCharge + energyCharge + envControlCharge;
else
    serviceTotal = custCharge + energyCharge + envControlCharge - 3.15;

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 conoditions:

using static System.Console;

WriteLine("================================================================");

WriteLine("To grant you this job, we need to ask a few questions.");
WriteLine("Levels of Security Clearance:");
WriteLine("0. Unknown or None");
WriteLine("1. Non-Sensitive");
WriteLine("2. National Security - Non-Critical Sensitive");
WriteLine("3. National Security - Critical Sensitive");
WriteLine("4. National Security - Special Sensitive");
WriteLine("----------------------------------------------------------------");
Write("Type your level (1-4): ");
int level = int.Parse(ReadLine());
        
WriteLine("================================================================");
        
if( level == 2 || level == 3 || level == 4 )
    WriteLine("Welcome on board.");
else
    WriteLine("We will get back to you...");

WriteLine("================================================================");

Practical LearningPractical Learning: Introducing Logical Disjunctions

  1. Change the code as follows:
    using static System.Console;
    
    WriteLine("==========================================");
    WriteLine(" - Amazing DeltaX - State Income Tax -");
    WriteLine("==========================================");
    
    double taxRate = 0.00;
    
    WriteLine("Enter the information for tax preparation");
    WriteLine("States");
    WriteLine(" AK - Alaska");
    WriteLine(" AR - Arkansas");
    WriteLine(" CO - Colorado");
    WriteLine(" FL - Florida");
    WriteLine(" GA - Georgia");
    WriteLine(" IL - Illinois");
    WriteLine(" IN - Indiana");
    WriteLine(" KY - Kentucky");
    WriteLine(" MA - Massachusetts");
    WriteLine(" MI - Michigan");
    WriteLine(" MO - Missouri");
    WriteLine(" MS - Mississippi");
    WriteLine(" NV - Nevada");
    WriteLine(" NH - New Hampshire");
    WriteLine(" NC - North Carolina");
    WriteLine(" PA - Pennsylvania");
    WriteLine(" SD - South Dakota");
    WriteLine(" TN - Tennessee");
    WriteLine(" TX - Texas");
    WriteLine(" UT - Utah");
    WriteLine(" WA - Washington");
    WriteLine(" WY - Wyoming");
    Write("Enter State Abbreviation: ");
    string abbr = ReadLine();
    WriteLine("--------------------------------------------");
            
    if( (abbr == "IL") || (abbr == "UT") )
        taxRate = 4.95;
    else if ((abbr == "KY") || (abbr == "MA") || (abbr == "NH"))
        taxRate = 5.00;
    else if ((abbr == "FL") || (abbr == "NV") || (abbr == "TX") || (abbr == "WA") || (abbr == "WY"))
        taxRate = 0.00;
    
    Write("Gross Salary: ");
    double grossSalary = double.Parse(ReadLine());
    
    double taxAmount = grossSalary * taxRate / 100.00;
    double netPay = grossSalary - taxAmount;
    
    WriteLine("============================================");
    WriteLine(" - Amazing DeltaX - State Income Tax -");
    WriteLine("--------------------------------------------");
    WriteLine($"Gross Salary: {grossSalary:f}");
    WriteLine($"Tax Rate:     {taxRate:f}%");
    WriteLine("--------------------------------------------");
    WriteLine($"Tax Amount:   {taxAmount:f}");
    WriteLine($"Net Pay:      {netPay:f}");
    WriteLine("============================================");
  2. To execute the project, on the main menu, click Debug -> Start Without Debugging:
  3. For the State Abbreviation, type MA
  4. For the Gross Salary, type 2458.95 and press Enter
    ==========================================
     - Amazing DeltaX - State Income Tax -
    ==========================================
    Enter the information for tax preparation
    States
     AK - Alaska
     AR - Arkansas
     CO - Colorado
     FL - Florida
     GA - Georgia
     IL - Illinois
     IN - Indiana
     KY - Kentucky
     MA - Massachusetts
     MI - Michigan
     MO - Missouri
     MS - Mississippi
     NV - Nevada
     NH - New Hampshire
     NC - North Carolina
     PA - Pennsylvania
     SD - South Dakota
     TN - Tennessee
     TX - Texas
     UT - Utah
     WA - Washington
     WY - Wyoming
    Enter State Abbreviation: MA
    --------------------------------------------
    Gross Salary: 2458.95
    ============================================
     - Amazing DeltaX - State Income Tax -
    --------------------------------------------
    Gross Salary: 2458.95
    Tax Rate:     5.00%
    --------------------------------------------
    Tax Amount:   122.95
    Net Pay:      2336.00
    ============================================
    Press any key to close this window . . .
  5. Press T to close the window and return to your programming environment
  6. To execute the project, on the main menu, click Debug -> Start Without Debugging:
  7. For the State Abbreviation, type TX
  8. For the Gross Salary, type 2458.95 and press Enter
    ==========================================
     - Amazing DeltaX - State Income Tax -
    ==========================================
    Enter the information for tax preparation
    States
     AK - Alaska
     AR - Arkansas
     CO - Colorado
     FL - Florida
     GA - Georgia
     IL - Illinois
     IN - Indiana
     KY - Kentucky
     MA - Massachusetts
     MI - Michigan
     MO - Missouri
     MS - Mississippi
     NV - Nevada
     NH - New Hampshire
     NC - North Carolina
     PA - Pennsylvania
     SD - South Dakota
     TN - Tennessee
     TX - Texas
     UT - Utah
     WA - Washington
     WY - Wyoming
    Enter State Abbreviation: TX
    --------------------------------------------
    Gross Salary: 2458.95
    ============================================
     - Amazing DeltaX - State Income Tax -
    --------------------------------------------
    Gross Salary: 2458.95
    Tax Rate:     0.00%
    --------------------------------------------
    Tax Amount:   0.00
    Net Pay:      2458.95
    ============================================
    Press any key to close this window . . .
  9. Press G to close the window and return to your programming environment

Combining Conjunctions and Disjunctions

You can create a logical expression that combines conjunctions and disjunctions. Here is an example:

using static System.Console;

WriteLine("Electric Bill Evaluation");
WriteLine("======================================================");
WriteLine("Enter the following pieces of information");
WriteLine("------------------------------------------------------");
Write("Customer ZIP Code:               ");
int zipCode      = int.Parse(ReadLine());
Write("Counter Start:                   ");
int counterStart = int.Parse(ReadLine());
Write("Counter End:                     ");
int counterEnd   = int.Parse(ReadLine());
Write("Number of Days:                  ");
int days         = int.Parse(ReadLine());
WriteLine("======================================================");

const double custChargeRate = 0.49315;
const double nrgChargeRate  = 0.16580;
const double envChargeRate  = 0.00043;

int   electricUse           = counterEnd - counterStart;

double custCharge           = days        * custChargeRate;
double energyCharge         = electricUse * nrgChargeRate;
double envControlCharge     = electricUse * envChargeRate;

double serviceTotal         = 0;

/* Let's imagine a fictitious scenario where the government 
 * wants to assist people in a certain group of ZIP codes by 
 * giving them a small discount ($3.15) in their electric bill. */
if( zipCode is (>= 20628 and <= 20685) ||
    zipCode is (>= 21110 and <= 21120) ||
    zipCode is (>= 21735 and <= 21795))
    serviceTotal            = custCharge + energyCharge + envControlCharge - 3.15;
else
    serviceTotal            = custCharge + energyCharge + envControlCharge;

WriteLine("Electric Bill Summary:");
WriteLine("======================================================");
WriteLine("Counter Reading Start:        {0, 8}", counterStart);
WriteLine("Counter Reading End:          {0, 8}", counterEnd);
WriteLine("Total Electric Use:           {0, 8}", electricUse);
WriteLine("Number of Days:               {0, 8}", days);
WriteLine("------------------------------------------------------");
WriteLine("Energy Charge Credit");
WriteLine("------------------------------------------------------");
WriteLine("Customer Chargee:             {0, 8:f}", custCharge);
WriteLine("Energy Charge:                {0, 8:f}", energyCharge);
WriteLine("Environmental Control Charge: {0, 8:f}", envControlCharge);
WriteLine("------------------------------------------------------");
WriteLine("Electric Servive Total:       {0, 8:f}", serviceTotal);
WriteLine("======================================================");

Here is an example of running the application:

Electric Bill Evaluation
======================================================
Enter the following pieces of information
------------------------------------------------------
Customer ZIP Code:               20620
Counter Start:                   10089
Counter End:                     11126
Number of Days:                  29
======================================================
Electric Bill Summary:
======================================================
Counter Reading Start:           10089
Counter Reading End:             11126
Total Electric Use:               1037
Number of Days:                     29
------------------------------------------------------
Energy Charge Credit
------------------------------------------------------
Customer Chargee:                14.30
Energy Charge:                  171.93
Environmental Control Charge:     0.45
------------------------------------------------------
Electric Servive Total:         186.68
======================================================

Press any key to close this window . . .

Here is another example of running the program:

Electric Bill Evaluation
======================================================
Enter the following pieces of information
------------------------------------------------------
Customer ZIP Code:               20680
Counter Start:                   10089
Counter End:                     11126
Number of Days:                  29
======================================================
Electric Bill Summary:
======================================================
Counter Reading Start:           10089
Counter Reading End:             11126
Total Electric Use:               1037
Number of Days:                     29
------------------------------------------------------
Energy Charge Credit
------------------------------------------------------
Customer Chargee:                14.30
Energy Charge:                  171.93
Environmental Control Charge:     0.45
------------------------------------------------------
Electric Servive Total:         183.53
======================================================

Press any key to close this window . . .

Here is another example of the program:

Electric Bill Evaluation
======================================================
Enter the following pieces of information
------------------------------------------------------
Customer ZIP Code:               20910
Counter Start:                   10089
Counter End:                     11126
Number of Days:                  29
======================================================
Electric Bill Summary:
======================================================
Counter Reading Start:           10089
Counter Reading End:             11126
Total Electric Use:               1037
Number of Days:                     29
------------------------------------------------------
Energy Charge Credit
------------------------------------------------------
Customer Chargee:                14.30
Energy Charge:                  171.93
Environmental Control Charge:     0.45
------------------------------------------------------
Electric Servive Total:         186.68
======================================================

Press any key to close this window . . .

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2024, FunctionX Monday 17 April 2023 Next