What Else?

Introduction

In the previous lesson, we were introduced to logical operators. We also got introduced to conditional statements with the if operator. The C# language provides more operators to perform more comparisons.

Practical LearningPractical Learning: Introducing Conditions

  1. Start Microsoft Visual Studio and, on the Visual Studio 2022 dialog box, click Create a New Project. If Microsoft Visual Studio was already running, on the main menu, click File -> New -> Project...
  2. On the dialog box, make sure Console App is selected. Click Next
  3. Change the Project Name to StellarWaterPoint1 and change or accept the project Location
  4. Click Next
  5. Accept or change the Framework as .NET 6.0 (Long-Term Support).
    Click Create
  6. Change the document as follows:
    using static System.Console;
    
    WriteLine("Stellar Water Point");
    WriteLine("======================================================");
    WriteLine("To prepare an invoice, enter the following information");
    WriteLine("Types of Accounts");
    WriteLine("1. Residential Household");
    WriteLine("2. Laudromat");
    WriteLine("3. Health Clinic");
    WriteLine("4. Place of Worship/Youth Center");
    WriteLine("5. Government Agency");
    WriteLine("6. Other Business");
    WriteLine("------------------------------------------------------");
    Write("Enter Type of Account:          ");
    string answer = ReadLine();
    WriteLine("------------------------------------------------------");
    
    Write("Counter Reading Start:          ");
    double counterReadingStart = double.Parse(ReadLine());
    Write("Counter Reading End:            ");
    double counterReadingEnd = double.Parse(ReadLine());
    
    double localTaxes, stateTaxes;
    double serviceCharges, environmentCharges;
    double first25Therms, next15Therms, above40Therms;
    
    double gallons = counterReadingEnd - counterReadingStart;
    double HCFTotal = gallons / 748;
    
    first25Therms = 25.00 * 1.5573;
    next15Therms = 15.00 * 1.2264;
    above40Therms = (HCFTotal - 40.00) * 0.9624;
    
    double waterUsageCharges = first25Therms + next15Therms + above40Therms;
    double sewerCharges = waterUsageCharges * 0.252;
    
    environmentCharges = waterUsageCharges * 0.0025;
    serviceCharges = waterUsageCharges * 0.0328;
    
    double totalCharges = waterUsageCharges + sewerCharges + environmentCharges + serviceCharges;
    
    localTaxes = totalCharges * 0.005259;
    stateTaxes = totalCharges * 0.002262;
    
    double amountDue = totalCharges + localTaxes + stateTaxes;
    
    WriteLine("======================================================");
    WriteLine("Stellar Water Point - Customer Invoice");
    WriteLine("======================================================");
    WriteLine("Meter Reading");
    WriteLine("------------------------------------------------------");
    WriteLine("Counter Reading Start:      {0,10}", counterReadingStart);
    WriteLine("Counter Reading End:        {0,10}", counterReadingEnd);
    WriteLine("Total Gallons Consumed:     {0,10}", gallons);
    WriteLine("======================================================");
    WriteLine("Therms Evaluation");
    WriteLine("------------------------------------------------------");
    WriteLine("HCF Total:                  {0,10:n}", HCFTotal);
    WriteLine("First 35 Therms (* 1.5573): {0,10:n}", first25Therms);
    WriteLine("First 20 Therms (* 1.2264): {0,10:n}", next15Therms);
    WriteLine("Above 55 Therms (* 0.9624): {0,10:n}", above40Therms);
    WriteLine("------------------------------------------------------");
    WriteLine("Water Usage Charges:        {0,10:n}", waterUsageCharges);
    WriteLine("Sewer Charges:              {0,10:n}", sewerCharges);
    WriteLine("======================================================");
    WriteLine("Bill Values");
    WriteLine("------------------------------------------------------");
    WriteLine("Environment Charges:        {0,10:n}", environmentCharges);
    WriteLine("Service Charges:            {0,10:n}", serviceCharges);
    WriteLine("Total Charges:              {0,10:n}", totalCharges);
    WriteLine("Local Taxes:                {0,10:n}", localTaxes);
    WriteLine("State Taxes:                {0,10:n}", stateTaxes);
    WriteLine("------------------------------------------------------");
    WriteLine("Amount Due:                 {0,10:n}", amountDue);
    WriteLine("======================================================");
  7. To execute, on the main menu, click Debug -> Start Without Debugging:
  8. When requested, for the type of account, type 4 and press Enter
  9. For the Counter Reading Start, type 92863 and press Enter
  10. For the Counter Reading Start, type 224926 and press Enter
    Stellar Water Point
    ======================================================
    To prepare an invoice, enter the following information
    Types of Accounts
    1. Residential Household
    2. Laudromat
    3. Health Clinic
    4. Place of Worship/Youth Center
    5. Government Agency
    6. Other Business
    ------------------------------------------------------
    Enter Type of Account:          4
    ------------------------------------------------------
    Counter Reading Start:          92863
    Counter Reading End:            224926
    ======================================================
    Stellar Water Point - Customer Invoice
    ======================================================
    Meter Reading
    ------------------------------------------------------
    Counter Reading Start:           92863
    Counter Reading End:            224926
    Total Gallons Consumed:         132063
    ======================================================
    Therms Evaluation
    ------------------------------------------------------
    HCF Total:                      176.55
    First 35 Therms (* 1.5573):      38.93
    First 20 Therms (* 1.2264):      18.40
    Above 55 Therms (* 0.9624):     131.42
    ------------------------------------------------------
    Water Usage Charges:            188.75
    Sewer Charges:                   47.56
    ======================================================
    Bill Values
    ------------------------------------------------------
    Environment Charges:              0.47
    Service Charges:                  6.19
    Total Charges:                  242.98
    Local Taxes:                      1.28
    State Taxes:                      0.55
    ------------------------------------------------------
    Amount Due:                     244.80
    ======================================================
    
    Press any key to close this window . . .
  11. Press Enter to close the window and return to your programming environment

Introduction to if...else Conditions

If you use an if condition to perform an operation and if the result is true, we saw that you could execute the statement. Any other result would be ignored. To let you address an alternative to an if condition, the C# language provides a keyword named else. The formula to use it is:

if(condition)
    statement1;
else
    statement2;

Once again, the condition can be a Boolean operation. If the condition is true, then the statement1 would execute. If the condition is false, then the compiler would execute the statement2 in the else section. Here is an example:

using static System.Console;

int number = 248;

if(number == 248)
    WriteLine("That number is correct.");
else
    WriteLine("That number is not right.");

Here is an example of executing the program:

That number is correct.
Press any key to close this window . . .

Practical LearningPractical Learning: Introducing if...else Conditions

  1. Change the document as follows:
    using static System.Console;
    
    WriteLine("Stellar Water Point");
    WriteLine("======================================================");
    WriteLine("To prepare an invoice, enter the following information");
    WriteLine("Types of Accounts");
    WriteLine("1. Residential Household");
    WriteLine("2. Laudromat");
    WriteLine("3. Health Clinic");
    WriteLine("4. Place of Worship/Youth Center");
    WriteLine("5. Government Agency");
    WriteLine("6. Other Business");
    WriteLine("------------------------------------------------------");
    Write("Enter Type of Account:          ");
    string answer = ReadLine();
    WriteLine("------------------------------------------------------");
    
    Write("Counter Reading Start:          ");
    double counterReadingStart = double.Parse(ReadLine());
    Write("Counter Reading End:            ");
    double counterReadingEnd = double.Parse(ReadLine());
    
    double localTaxes, stateTaxes;
    double serviceCharges, environmentCharges;
    double first25Therms, next15Therms, above40Therms;
    
    double gallons = counterReadingEnd - counterReadingStart;
    double HCFTotal = gallons / 748;
    
    first25Therms = 25.00 * 1.5573;
    next15Therms = 15.00 * 1.2264;
    above40Therms = (HCFTotal - 40.00) * 0.9624;
    
    double waterUsageCharges = first25Therms + next15Therms + above40Therms;
    double sewerCharges = waterUsageCharges * 0.252;
    
    if (answer == "1")
        environmentCharges = waterUsageCharges * 0.0025;
    else
        environmentCharges = waterUsageCharges * 0.0125;
    
    if(answer == "1")
        serviceCharges = waterUsageCharges * 0.0328;
    else
        serviceCharges = waterUsageCharges * 0.11643;
    
    double totalCharges = waterUsageCharges + sewerCharges + environmentCharges + serviceCharges;
    
    if (answer == "1")
        localTaxes = totalCharges * 0.002857;
    else
        localTaxes = totalCharges * 0.005259;
    
    if(answer == "1")
        stateTaxes = totalCharges * 0.000815;
    else
        stateTaxes = totalCharges * 0.002262;
    
    double amountDue = totalCharges + localTaxes + stateTaxes;
    
    WriteLine("======================================================");
    WriteLine("Stellar Water Point - Customer Invoice");
    WriteLine("======================================================");
    WriteLine("Meter Reading");
    WriteLine("------------------------------------------------------");
    WriteLine("Counter Reading Start:      {0,10}", counterReadingStart);
    WriteLine("Counter Reading End:        {0,10}", counterReadingEnd);
    WriteLine("Total Gallons Consumed:     {0,10}", gallons);
    WriteLine("======================================================");
    WriteLine("Therms Evaluation");
    WriteLine("------------------------------------------------------");
    WriteLine("HCF Total:                  {0,10:n}", HCFTotal);
    WriteLine("First 35 Therms (* 1.5573): {0,10:n}", first25Therms);
    WriteLine("First 20 Therms (* 1.2264): {0,10:n}", next15Therms);
    WriteLine("Above 55 Therms (* 0.9624): {0,10:n}", above40Therms);
    WriteLine("------------------------------------------------------");
    WriteLine("Water Usage Charges:        {0,10:n}", waterUsageCharges);
    WriteLine("Sewer Charges:              {0,10:n}", sewerCharges);
    WriteLine("======================================================");
    WriteLine("Bill Values");
    WriteLine("------------------------------------------------------");
    WriteLine("Environment Charges:        {0,10:n}", environmentCharges);
    WriteLine("Service Charges:            {0,10:n}", serviceCharges);
    WriteLine("Total Charges:              {0,10:n}", totalCharges);
    WriteLine("Local Taxes:                {0,10:n}", localTaxes);
    WriteLine("State Taxes:                {0,10:n}", stateTaxes);
    WriteLine("------------------------------------------------------");
    WriteLine("Amount Due:                 {0,10:n}", amountDue);
    WriteLine("======================================================");
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging:
  3. When requested, for the type of account, type 1 and press Enter
  4. For the Counter Reading Start, type 92863 and press Enter
  5. For the Counter Reading Start, type 224926 and press Enter
    Stellar Water Point
    ======================================================
    To prepare an invoice, enter the following information
    Types of Accounts
    1. Residential Household
    2. Laudromat
    3. Health Clinic
    4. Place of Worship/Youth Center
    5. Government Agency
    6. Other Business
    ------------------------------------------------------
    Enter Type of Account:          1
    ------------------------------------------------------
    Counter Reading Start:          92863
    Counter Reading End:            224926
    ======================================================
    Stellar Water Point - Customer Invoice
    ======================================================
    Meter Reading
    ------------------------------------------------------
    Counter Reading Start:           92863
    Counter Reading End:            224926
    Total Gallons Consumed:         132063
    ======================================================
    Therms Evaluation
    ------------------------------------------------------
    HCF Total:                      176.55
    First 35 Therms (* 1.5573):      38.93
    First 20 Therms (* 1.2264):      18.40
    Above 55 Therms (* 0.9624):     131.42
    ------------------------------------------------------
    Water Usage Charges:            188.75
    Sewer Charges:                   47.56
    ======================================================
    Bill Values
    ------------------------------------------------------
    Environment Charges:              0.47
    Service Charges:                  6.19
    Total Charges:                  242.98
    Local Taxes:                      0.69
    State Taxes:                      0.20
    ------------------------------------------------------
    Amount Due:                     243.87
    ======================================================
    
    Press any key to close this window . . .
  6. Press Enter to close the window and return to your programming environment
  7. To execute the application again, on the main menu, click Debug -> Start Without Debugging:
  8. When requested, for the type of account, type 2 and press Enter
  9. For the Counter Reading Start, type 4205 and press Enter
  10. For the Counter Reading Start, type 362877 and press Enter
    Stellar Water Point
    ======================================================
    To prepare an invoice, enter the following information
    Types of Accounts
    1. Residential Household
    2. Laudromat
    3. Health Clinic
    4. Place of Worship/Youth Center
    5. Government Agency
    6. Other Business
    ------------------------------------------------------
    Enter Type of Account:          2
    ------------------------------------------------------
    Counter Reading Start:          4205
    Counter Reading End:            362877
    ======================================================
    Stellar Water Point - Customer Invoice
    ======================================================
    Meter Reading
    ------------------------------------------------------
    Counter Reading Start:            4205
    Counter Reading End:            362877
    Total Gallons Consumed:         358672
    ======================================================
    Therms Evaluation
    ------------------------------------------------------
    HCF Total:                      479.51
    First 35 Therms (* 1.5573):      38.93
    First 20 Therms (* 1.2264):      18.40
    Above 55 Therms (* 0.9624):     422.98
    ------------------------------------------------------
    Water Usage Charges:            480.31
    Sewer Charges:                  121.04
    ======================================================
    Bill Values
    ------------------------------------------------------
    Environment Charges:              6.00
    Service Charges:                 55.92
    Total Charges:                  663.28
    Local Taxes:                      3.49
    State Taxes:                      1.50
    ------------------------------------------------------
    Amount Due:                     668.26
    ======================================================
    
    Press any key to close this window . . .
  11. Press Q to close the window and return to your programming environment

Going To a Statement

In the flow of your code, you can jump from one statement or line to another. To make this possible, the C# language forvides an operator named goto. Before using it, first create or insert a name on a particular section of code or in a method. The name, also called a label, is made of one word and it can be anything. That name is followed by a colon ":". Here is an example:

proposition:

In the same way, you can create as many labels as you want. The name of each label must be unique among the other labels in the same section of code (in the same scope). Here are examples:

proposition:

something:

TimeToLeave:

After creating the label(s), you can create a condition so that, when that condition is true, code execution would jump to a designated label. To do this, in the body of the condition, type goto followed by the label. Here are examples:

int nbr = 248;
double result = 0;

if(nbr is < 200)
    goto proposition;
else
    goto something;

proposition:
    result = 245.55;

something:
    result = 105.75;

The Ternary Operator (?:)

We have seen that an if...else conditional statement can be performed as follows:

if(condition)
    statement1;
else
    statement2;

Here is an example of such a statement:

using static System.Console;

double value;
string choice = "Residential";

if(choice == "Residential")
    value = 16.55;
else
    value = 19.25;

WriteLine("Account Type: Residential");
WriteLine("Value:        {0}", value);
WriteLine("======================================================");

This would produce:

Account Type: Residential
Value:        16.55
======================================================
Press any key to close this window . . .

If you have a condition that can be checked as an if situation with one alternate else, you can use the ternary operator that is a combination of ? and :. Its formula is:

condition ? statement1 : statement2;

The condition would first be checked. If the condition is true, then statement1 would execute. If not, statement2 would execute. Based on this, ehe above code can be written as follows:

using static System.Console;

double value;
string choice = "Residential";

value = choice == "Residential" ? 16.55 : 19.25;

WriteLine("Account Type: Residential");
WriteLine("Value:        {0}", value);
WriteLine("======================================================");

If you want to make your code easy to read, you can (should) include the condition in parentheses. This can be done as follows:

using static System.Console;

double value;
string choice = "Residential";

value = (choice == "Residential") ? 16.55 : 19.25;

WriteLine("Account Type: Residential");
WriteLine("Value:        {0}", value);
WriteLine("======================================================");

One or each of the statements can use an expression. Here are examples:

double environmentCharges = (answer == "1") ? usage * 0.0025 : usage * 0.0125;

double serviceCharges = (answer == "1") ? waterUsageCharges * 0.0328
                                        : waterUsageCharges * 0.11643;

This time too, to make your code easy to read, you can include the statement in parenthese. Here are examples:

double environmentCharges = (answer == "1") ? (usage * 0.0025) : (usage * 0.0125);

double serviceCharges = (answer == "1") ? (waterUsageCharges * 0.0328)
                                        : (waterUsageCharges * 0.11643);

Practical LearningPractical Learning: Using the Ternary Operator

  1. Change the code as follows:
    using static System.Console;
    
    WriteLine("Stellar Water Point");
    WriteLine("======================================================");
    WriteLine("To prepare an invoice, enter the following information");
    WriteLine("Types of Accounts");
    WriteLine("1. Residential Household");
    WriteLine("2. Laudromat");
    WriteLine("3. Health Clinic");
    WriteLine("4. Place of Worship/Youth Center");
    WriteLine("5. Government Agency");
    WriteLine("6. Other Business");
    WriteLine("------------------------------------------------------");
    Write("Enter Type of Account:          ");
    string answer = ReadLine();
    WriteLine("------------------------------------------------------");
    
    Write("Counter Reading Start:          ");
    double counterReadingStart = double.Parse(ReadLine());
    Write("Counter Reading End:            ");
    double counterReadingEnd = double.Parse(ReadLine());
            
    double first25Therms, next15Therms, above40Therms;
    
    double gallons  = counterReadingEnd - counterReadingStart;
    double HCFTotal = gallons / 748;
    
    first25Therms = 25.00 * 1.5573;
    next15Therms  = 15.00 * 1.2264;
    above40Therms = (HCFTotal - 40.00) * 0.9624;
    
    double waterUsageCharges = first25Therms + next15Therms + above40Therms;
    double sewerCharges      = waterUsageCharges * 0.252;
    
    double environmentCharges = (answer == "1") ? (waterUsageCharges * 0.0025) : (waterUsageCharges * 0.0125);
    double serviceCharges = (answer == "1") ? (waterUsageCharges * 0.0328) : (waterUsageCharges * 0.11643);
    
    double totalCharges = waterUsageCharges + sewerCharges + environmentCharges + serviceCharges;
    
    double localTaxes = (answer == "1") ? (totalCharges * 0.002857) : (totalCharges * 0.005259);
    double stateTaxes = (answer == "1") ? (totalCharges * 0.000815) : (totalCharges * 0.002262);
    
    double amountDue = totalCharges + localTaxes + stateTaxes;
    
    WriteLine("======================================================");
    WriteLine("Stellar Water Point - Customer Invoice");
    WriteLine("======================================================");
    WriteLine("Meter Reading");
    WriteLine("------------------------------------------------------");
    WriteLine("Counter Reading Start:      {0,10}", counterReadingStart);
    WriteLine("Counter Reading End:        {0,10}", counterReadingEnd);
    WriteLine("Total Gallons Consumed:     {0,10}", gallons);
    WriteLine("======================================================");
    WriteLine("Therms Evaluation");
    WriteLine("------------------------------------------------------");
    WriteLine("HCF Total:                  {0,10:n}", HCFTotal);
    WriteLine("First 35 Therms (* 1.5573): {0,10:n}", first25Therms);
    WriteLine("First 20 Therms (* 1.2264): {0,10:n}", next15Therms);
    WriteLine("Above 55 Therms (* 0.9624): {0,10:n}", above40Therms);
    WriteLine("------------------------------------------------------");
    WriteLine("Water Usage Charges:        {0,10:n}", waterUsageCharges);
    WriteLine("Sewer Charges:              {0,10:n}", sewerCharges);
    WriteLine("======================================================");
    WriteLine("Bill Values");
    WriteLine("------------------------------------------------------");
    WriteLine("Environment Charges:        {0,10:n}", environmentCharges);
    WriteLine("Service Charges:            {0,10:n}", serviceCharges);
    WriteLine("Total Charges:              {0,10:n}", totalCharges);
    WriteLine("Local Taxes:                {0,10:n}", localTaxes);
    WriteLine("State Taxes:                {0,10:n}", stateTaxes);
    WriteLine("------------------------------------------------------");
    WriteLine("Amount Due:                 {0,10:n}", amountDue);
    WriteLine("======================================================");
  2. To execute the application and make sure it, on the main menu, click Run -> Run Without Debugging
  3. When requested, for the type of account, type 6 and press Enter
  4. For the Counter Reading Start, type 41722 and press Enter
  5. For the Counter Reading Start, type 88061 and press Enter
    Stellar Water Point
    ======================================================
    To prepare an invoice, enter the following information
    Types of Accounts
    1. Residential Household
    2. Laudromat
    3. Health Clinic
    4. Place of Worship/Youth Center
    5. Government Agency
    6. Other Business
    ------------------------------------------------------
    Enter Type of Account:          6
    ------------------------------------------------------
    Counter Reading Start:          41722
    Counter Reading End:            88061
    ======================================================
    Stellar Water Point - Customer Invoice
    ======================================================
    Meter Reading
    ------------------------------------------------------
    Counter Reading Start:           41722
    Counter Reading End:             88061
    Total Gallons Consumed:          46339
    ======================================================
    Therms Evaluation
    ------------------------------------------------------
    HCF Total:                       61.95
    First 35 Therms (* 1.5573):      38.93
    First 20 Therms (* 1.2264):      18.40
    Above 55 Therms (* 0.9624):      21.13
    ------------------------------------------------------
    Water Usage Charges:             78.45
    Sewer Charges:                   19.77
    ======================================================
    Bill Values
    ------------------------------------------------------
    Environment Charges:              0.98
    Service Charges:                  9.13
    Total Charges:                  108.34
    Local Taxes:                      0.57
    State Taxes:                      0.25
    ------------------------------------------------------
    Amount Due:                     109.15
    ======================================================
    
    Press any key to close this window . . .
  6. Press Q to close the window and return to your programming environment
  7. To start a new project, on the main menu of Microsoft Visual Studio, click File -> New -> Project...
  8. In the Create a New Project dialog box, make sure the C# language is selected and Console Application is selected (if not, click it).
    Click Next
  9. Change the Project Name to TaxPreparation01 and change or accept the project Location
  10. Click Next
  11. Make sure the Target Framework combo box is displaying .NET 5.0 (Current). Click Create
  12. Change the document as follows:
    using static System.Console;
    
    WriteLine("============================================");
    WriteLine(" - Mississippi - State Income Tax -");
    WriteLine("============================================");
    
    double taxRate = 0.00;
    
    WriteLine("Enter the information for tax preparation");
    Write("Gross Salary: ");
    double grossSalary = double.Parse(ReadLine());
    
    double taxAmount = grossSalary * taxRate / 100.00;
    double netPay    = grossSalary - taxAmount;
    
    WriteLine("==============================================");
    WriteLine(" - Mississippi - State Income Tax -");
    WriteLine("----------------------------------------------");
    WriteLine($"Gross Salary: {grossSalary:f}");
    WriteLine($"Tax Rate:     {taxRate:f}%");
    WriteLine($"Tax Amount:   {taxAmount:f}");
    WriteLine($"Net Pay:      {netPay:f}");
    WriteLine("==============================================");
  13. To execute, on the main menu, click Debug -> Start Without Debugging
  14. Type the Gross Salary as 582.97 and press Enter
    ============================================
     - Mississippi - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary: 582.97
    ==============================================
     - Mississippi - State Income Tax -
    ----------------------------------------------
    Gross Salary: 582.97
    Tax Rate:     0.00%
    Tax Amount:   0.00
    Net Pay:      582.97
    ==============================================
    Press any key to close this window . . .
  15. To close the window and return to your programming environment, press W

The Bodies of a Composite Conditional Statement

We saw that if the statement of an if condition is long or if it involves many statements, you can create a body for its statement(s). In the same way, if an else condition produces a long or many statements, add a body deliminated by curcly brackets to it. This would be done as follows:

if(condition)
    statement1;
else
{
    statement2;
}

Either the if or the else statement can have a body whether the other has a body or not. You will decide based on your goal:

if(condition){
    if-statement1;
    if-statement2;
    . . .
    if-statement_n;
}
else{
    else-statement1;
    else-statement2;
    . . .
    else-statement_n;
}

Variants of an else Conditional Statement

If...Else If

If you use an if...else situation, you can process only two statements. In some cases, you may deal with more than two conditions. In this case, you can use an if...else if condition. Its formula is:

if(condition1) statement1;
else if(condition2) statement2;

If you want, you can add a body delimited by curly brackets, as follows:

if(condition1) {
    statement1;
}
else if(condition2) {
    statement2;
}

The first condition, condition1, would first be checked. If condition1 is true, then statement1 would execute. If condition1 is false, then condition2 would be checked. If condition2 is true, then statement2 would execute. Any other result would be ignored.

Practical LearningPractical Learning: Using Else If

  1. Change the document as follows:
    using static System.Console;
    
    WriteLine("============================================");
    WriteLine(" - Mississippi - State Income Tax -");
    WriteLine("============================================");
    
    double taxRate = 0.00;
    
    WriteLine("Enter the information for tax preparation");
    Write("Gross Salary: ");
    double grossSalary = double.Parse(ReadLine());
    
    // Mississippi
    if (grossSalary is >= 10_000)
        taxRate = 5.00;
    else if (grossSalary is >= 5_000)
        taxRate = 4.00;
    else if (grossSalary is >= 1_000)
        taxRate = 3.00;
    
    double taxAmount = grossSalary * taxRate / 100.00;
    double netPay    = grossSalary - taxAmount;
    
    WriteLine("==============================================");
    WriteLine(" - Mississippi - State Income Tax -");
    WriteLine("----------------------------------------------");
    WriteLine($"Gross Salary: {grossSalary:f}");
    WriteLine($"Tax Rate:     {taxRate:f}%");
    WriteLine($"Tax Amount:   {taxAmount:f}");
    WriteLine($"Net Pay:      {netPay:f}");
    WriteLine("==============================================");
  2. To execute the application, on the main menu, click Debug -> Start Without Debugging
  3. Type the Gross Salary as 582.97 and press Enter
    ============================================
     - Mississippi - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary: 582.97
    ==============================================
     - Mississippi - State Income Tax -
    ----------------------------------------------
    Gross Salary: 582.97
    Tax Rate:     0.00%
    Tax Amount:   0.00
    Net Pay:      582.97
    ==============================================
    Press any key to close this window . . .
  4. To close the window and return to your programming environment, press S
  5. To execute the application again, on the main menu, click Debug -> Start Without Debugging
  6. Type the Gross Salary as 3582.97 and press Enter
    ============================================
     - Mississippi - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary: 3582.97
    ==============================================
     - Mississippi - State Income Tax -
    ----------------------------------------------
    Gross Salary: 3582.97
    Tax Rate:     3.00%
    Tax Amount:   107.49
    Net Pay:      3475.48
    ==============================================
    Press any key to close this window . . .
  7. To close the window and return to your programming environment, press X
  8. To execute again, on the main menu, click Debug -> Start Without Debugging
  9. Type the Gross Salary as 7582.97 and press Enter
    ============================================
     - Mississippi - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary: 7582.97
    ==============================================
     - Mississippi - State Income Tax -
    ----------------------------------------------
    Gross Salary: 7582.97
    Tax Rate:     4.00%
    Tax Amount:   303.32
    Net Pay:      7279.65
    ==============================================
    Press any key to close this window . . .
  10. To close the window and return to your programming environment, press E
  11. To execute again, on the main menu, click Debug -> Start Without Debugging
  12. Type the Gross Salary as 17582.97 and press Enter
    ============================================
     - Mississippi - State Income Tax -
    ============================================
    Enter the information for tax preparation
    Gross Salary: 17582.97
    ==============================================
     - Mississippi - State Income Tax -
    ----------------------------------------------
    Gross Salary: 17582.97
    Tax Rate:     5.00%
    Tax Amount:   879.15
    Net Pay:      16703.82
    ==============================================
    Press any key to close this window . . .
  13. To close the window and return to your programming environment, press E
  14. 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(" CO - Colorado");
    WriteLine(" IN - Indiana");
    WriteLine(" MI - Michigan");
    WriteLine(" NC - North Carolina");
    WriteLine(" PA - Pennsylvania");
    WriteLine(" TN - Tennessee");
    Write("Enter State Abbreviation: ");
    string abbr = ReadLine();
    WriteLine("--------------------------------------------");
    
    Write("Gross Salary: ");
    double grossSalary = double.Parse(ReadLine());
    
    if(abbr == "CO")
    {
        taxRate   = 4.63;
        stateName = "Colorado";
    }
    else if (abbr == "IN")
    {
        taxRate = 3.23;
        stateName = "Indiana";
    }
    else if (abbr == "MI")
    {
        taxRate = 4.25;
        stateName = "Michigan";
    }
    else if (abbr == "NC")
    {
        taxRate = 5.25;
        stateName = "North Carolina";
    }
    else if (abbr == "PA")
    {
        taxRate = 3.07;
        stateName = "Pennsylvania";
    }
    else if (abbr == "TN")
    {
        taxRate = 1.00;
        stateName = "Tennessee";
    }
    
    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("============================================");
  15. To execute the application, on the main menu, click Debug -> Start Without Debugging
  16. For the State Abbreviation, type NC and press Enter
  17. For the Gross Salary, type 1497.68 and press Enter:
    ==========================================
     - Amazing DeltaX - State Income Tax -
    ==========================================
    Enter the information for tax preparation
    States
     CO - Colorado
     IN - Indiana
     MI - Michigan
     NC - North Carolina
     PA - Pennsylvania
     TN - Tennessee
    Enter State Abbreviation: NC
    --------------------------------------------
    Gross Salary: 1497.68
    ============================================
     - Amazing DeltaX - State Income Tax -
    --------------------------------------------
    State Name:   North Carolina
    Gross Salary: 1497.68
    Tax Rate:     5.25%
    --------------------------------------------
    Tax Amount:   78.63
    Net Pay:      1419.05
    ============================================
    Press any key to close this window . . .
  18. Press R to close the window and return to your programming environment
  19. To execute the application again, on the main menu, click Debug -> Start Without Debugging
  20. For the State Abbreviation, type PA and press Enter
  21. For the Gross Salary, type 1497.68 and press Enter:
    ==========================================
     - Amazing DeltaX - State Income Tax -
    ==========================================
    Enter the information for tax preparation
    States
     CO - Colorado
     IN - Indiana
     MI - Michigan
     NC - North Carolina
     PA - Pennsylvania
     TN - Tennessee
    Enter State Abbreviation: PA
    --------------------------------------------
    Gross Salary: 1497.68
    ============================================
     - Amazing DeltaX - State Income Tax -
    --------------------------------------------
    State Name:   Pennsylvania
    Gross Salary: 1497.68
    Tax Rate:     3.07%
    --------------------------------------------
    Tax Amount:   45.98
    Net Pay:      1451.70
    ============================================
    Press any key to close this window . . .
  22. Press D to close the window and return to your programming environment
  23. To open a recent project, on the main menu, click File -> Recent Projects and Solution -> StellarWaterPoint1

If... Else If... Else

Because there can be other alternatives, the C# language provides an alternate else condition as the last resort. Its formula is:

if(condition1)
    statement1;
else if(condition2)
    statement2;
else
    statement-n;

If you want, you can add a body delimited by curly brackets to any of the conditions:

if(condition1){
    statement1;
}
else if(condition2) {
    statement2;
}
else {
    statement-n;
}

Practical LearningPractical Learning: Introducing if...else if...else

  1. Change the document as follows:
    using static System.Console;
    
    WriteLine("Stellar Water Point");
    WriteLine("======================================================");
    WriteLine("To prepare an invoice, enter the following information");
    WriteLine("Types of Accounts");
    WriteLine("1. Residential Household");
    WriteLine("2. Laudromat");
    WriteLine("3. Health Clinic");
    WriteLine("4. Place of Worship/Youth Center");
    WriteLine("5. Government Agency");
    WriteLine("6. Other Business");
    Write("Enter Type of Account:         ");
    string answer = ReadLine();
    WriteLine("------------------------------------------------------");
    
    Write("Counter Reading Start:         ");
    double counterReadingStart = double.Parse(ReadLine());
    Write("Counter Reading End:           ");
    double counterReadingEnd = double.Parse(ReadLine());
    
    double first25Therms, next15Therms, above40Therms;
    
    double gallons = counterReadingEnd - counterReadingStart;
    double HCFTotal = gallons / 748;
    
    if (HCFTotal is > 40.00)
    {
        first25Therms = 25.00 * 1.5573;
        next15Therms = 15.00 * 1.2264;
        above40Therms = (HCFTotal - 40.00) * 0.9624;
    }
    else if (HCFTotal is > 25.00)
    {
        first25Therms = 25.00 * 1.5573;
        next15Therms = (HCFTotal - 25.00) * 1.2264;
        above40Therms = 0.00;
    }
    else // if (HCFTotal > 40.00)
    {
        first25Therms = HCFTotal * 1.5573;
        next15Therms = 0.00;
        above40Therms = 0.00;
    }
    
    double waterUsageCharges = first25Therms + next15Therms + above40Therms;
    double sewerCharges      = waterUsageCharges * 0.252;
    
    double environmentCharges = (answer == "1") ? (waterUsageCharges * 0.0025) : (waterUsageCharges * 0.0125);
    
    double serviceCharges     = (answer == "1") ? (waterUsageCharges * 0.0328)
                                                        : (waterUsageCharges * 0.11643);
    
    double totalCharges = waterUsageCharges + sewerCharges + environmentCharges + serviceCharges;
    
    double localTaxes   = (answer == "1") ? (totalCharges * 0.035) : (totalCharges * 0.235);
    double stateTaxes   = (answer == "1") ? (totalCharges * 0.115) : (totalCharges * 0.815);
    
    double amountDue    = totalCharges + localTaxes + stateTaxes;
    
    WriteLine("======================================================");
    WriteLine("Stellar Water Point - Customer Invoice");
    WriteLine("======================================================");
    WriteLine("Meter Reading");
    WriteLine("------------------------------------------------------");
    WriteLine("Counter Reading Start:      {0,10}", counterReadingStart);
    WriteLine("Counter Reading End:        {0,10}", counterReadingEnd);
    WriteLine("Total Gallons Consumed:     {0,10}", gallons);
    WriteLine("======================================================");
    WriteLine("Therms Evaluation");
    WriteLine("------------------------------------------------------");
    WriteLine("HCF Total:                  {0,10:n}", HCFTotal);
    WriteLine("First 35 Therms (* 1.5573): {0,10:n}", first25Therms);
    WriteLine("First 20 Therms (* 1.2264): {0,10:n}", next15Therms);
    WriteLine("Above 40 Therms (* 0.9624): {0,10:n}", above40Therms);
    WriteLine("------------------------------------------------------");
    WriteLine("Water Usage Charges:        {0,10:n}", waterUsageCharges);
    WriteLine("Sewer Charges:              {0,10:n}", sewerCharges);
    WriteLine("======================================================");
    WriteLine("Bill Values");
    WriteLine("------------------------------------------------------");
    WriteLine("Environment Charges:        {0,10:n}", environmentCharges);
    WriteLine("Service Charges:            {0,10:n}", serviceCharges);
    WriteLine("Total Charges:              {0,10:n}", totalCharges);
    WriteLine("Local Taxes:                {0,10:n}", localTaxes);
    WriteLine("State Taxes:                {0,10:n}", stateTaxes);
    WriteLine("------------------------------------------------------");
    WriteLine("Amount Due:                 {0,10:n}", amountDue);
    WriteLine("======================================================");
  2. To execute, on the main menu, click Debug -> Start Without Debugging:
  3. When requested, for the type of account, type 1 and press Enter
  4. For the Counter Reading Start, type 11207 and press Enter
  5. For the Counter Reading Start, type 26526 and press Enter
    Water Stellar Point
    ======================================================
    To prepare an invoice, enter the following information
    Types of Accounts
    1. Residential Household
    2. Laudromat
    3. Health Clinic
    4. Place of Worship/Youth Center
    5. Government Agency
    6. Other Business
    Enter Type of Account:         1
    ------------------------------------------------------
    Counter Reading Start:         11207
    Counter Reading End:           26526
    ======================================================
    Water Stellar Point - Customer Invoice
    ======================================================
    Meter Reading
    ------------------------------------------------------
    Counter Reading Start:           11207
    Counter Reading End:             26526
    Total Gallons Consumed:          15319
    ======================================================
    Therms Evaluation
    ------------------------------------------------------
    HCF Total:                       20.48
    First 35 Therms (* 1.5573):      31.89
    First 20 Therms (* 1.2264):       0.00
    Above 55 Therms (* 0.9624):       0.00
    ------------------------------------------------------
    Water Usage Charges:             31.89
    Sewer Charges:                    8.04
    ======================================================
    Bill Values
    ------------------------------------------------------
    Environment Charges:              0.08
    Service Charges:                  1.05
    Total Charges:                   41.06
    Local Taxes:                      1.44
    State Taxes:                      4.72
    ------------------------------------------------------
    Amount Due:                      47.21
    ======================================================
    Press any key to close this window . . .
  6. Press A to close the window and return to your programming environment
  7. To execute, on the main menu, click Debug -> Start Without Debugging:
  8. When requested, for the type of account, type 4 and press Enter
  9. For the Counter Reading Start, type 92863 and press Enter
  10. For the Counter Reading Start, type 224926 and press Enter
    Water Stellar Point
    ======================================================
    To prepare an invoice, enter the following information
    Types of Accounts
    1. Residential Household
    2. Laudromat
    3. Health Clinic
    4. Place of Worship/Youth Center
    5. Government Agency
    6. Other Business
    Enter Type of Account:         4
    ------------------------------------------------------
    Counter Reading Start:         92863
    Counter Reading End:           224926
    ======================================================
    Water Stellar Point - Customer Invoice
    ======================================================
    Meter Reading
    ------------------------------------------------------
    Counter Reading Start:           92863
    Counter Reading End:            224926
    Total Gallons Consumed:         132063
    ======================================================
    Therms Evaluation
    ------------------------------------------------------
    HCF Total:                      176.55
    First 35 Therms (* 1.5573):      38.93
    First 20 Therms (* 1.2264):      18.40
    Above 55 Therms (* 0.9624):     131.42
    ------------------------------------------------------
    Water Usage Charges:            188.75
    Sewer Charges:                   47.56
    ======================================================
    Bill Values
    ------------------------------------------------------
    Environment Charges:              2.36
    Service Charges:                 21.98
    Total Charges:                  260.65
    Local Taxes:                     61.25
    State Taxes:                    212.43
    ------------------------------------------------------
    Amount Due:                     534.33
    ======================================================
    Press any key to close this window . . .
  11. Press Z to close the window and return to your programming environment

If...Else If ... Else If and Else

The if...else conditional statement allows you to process many conditions. The formula to follow is:

if(condition1) statement1;
else if(condition2) statement2;
. . .
else if(condition_n) statement_n;

If you are writing the code in a webpage, each statement must be included in curly brackets. The formula to follow is:

if(condition1) { statement1; }
else if(condition2) { statement2; }
. . .
else if(condition_n) { statement_n; }

The conditions would be checked from the beginning one after another. If a condition is true, then its corresponding statement would execute.

If there is a possibility that none of the conditions would respond true, you can add a last else condition and its statetement. The formula to follow is:

if(condition1)
    statement1;
else if(condition2)
    statement2;
. . .
else if(condition_n)
    statement_n;
else
    statement-n;

In a webpage, the formula to follow is:

if(condition1) {
    statement1;
}
else if(condition2) {
    statement2;
}
. . .
else if(condition_n) {
    statement_n;
}
else {
    statement-n;
}

A condition may need many statements to execute. In this case, you must delimit the statements by curly brackets:

if(condition1)
{
    if-statement_1;
    if-statement_2;
    . . .
    if-statement_n;
}
else if(condition2)
{
    else-if-1-statement_1;
    else-if-1-statement_2;
    . . .
    else-if-1-statement_n;
}
. . .
else if(condition_n)
{
    else-if-n-statement_1;
    else-if-n-statement_2;
    . . .
    else-if-n-statement_n;
}
else
{
    else-statement_1;
    else-statement_2;
    . . .
    else-statement_n;
}

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2022, FunctionX Monday 06 December 2021 Next