What Else?

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 F# language provides a keyword named else. The formula to use it is:

if condition then
    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. Remember that you must indent each statement_x. Here is an example:

let mutable age = 12

if age < 18 then
    printfn "The person is less than 18 years old."
else
    printfn "This person is not younger than 18."

age <- 36

if age < 18 then
    printfn "The person is less than 18 years old."
else
    printfn "This person is not younger than 18."

This would produce:

The person is less than 18 years old.
This person is not younger than 18.
Press any key to continue . . .

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 7.0 (Standard Term Support).
    Click Create
  6. Change the document as follows:
    printfn "Stellar Water Point"
    printfn "======================================================"
    printfn "To prepare an invoice, enter the following information"
    printfn "Types of Accounts"
    printfn "1. Residential Household"
    printfn "2. Laudromat"
    printfn "3. Health Clinic"
    printfn "4. Place of Worship/Youth Center"
    printfn "5. Government Agency"
    printfn "6. Other Business"
    printfn "------------------------------------------------------"
    printf "Enter Type of Account:          "
    let answer = stdin.ReadLine()
    printfn "------------------------------------------------------"
    
    printf "Counter Reading Start:          "
    let counterReadingStart = float(stdin.ReadLine())
    printf "Counter Reading End:            "
    let counterReadingEnd   = float(stdin.ReadLine())
    
    let gallons = counterReadingEnd - counterReadingStart
    let HCFTotal = gallons / 748.00
    
    let mutable localTaxes         = 0.00
    let mutable stateTaxes         = 0.00
    let mutable serviceCharges     = 0.00
    let mutable environmentCharges = 0.00
    let mutable first25Therms      = 0.00
    let mutable next15Therms       = 0.00
    let mutable above40Therms      = 0.00
    
    first25Therms        <- 25.00 * 1.5573
    next15Therms         <- 15.00 * 1.2264
    above40Therms        <- (HCFTotal - 40.00)  * 0.9624
    
    let waterUsageCharges = first25Therms       + next15Therms + above40Therms;
    let sewerCharges      = waterUsageCharges   * 0.252;
    
    environmentCharges   <- waterUsageCharges   * 0.0025;
    serviceCharges       <- waterUsageCharges   * 0.0328;
    
    let totalCharges      = waterUsageCharges   + sewerCharges + environmentCharges + serviceCharges;
    
    localTaxes           <- totalCharges        * 0.005259;
    stateTaxes           <- totalCharges        * 0.002262;
    
    let amountDue         = totalCharges + localTaxes + stateTaxes;
    
    printfn "======================================================"
    printfn "Stellar Water Point - Customer Invoice"
    printfn "======================================================"
    printfn "Meter Reading"
    printfn "------------------------------------------------------"
    printfn "Counter Reading Start:      %10.2f" counterReadingStart
    printfn "Counter Reading End:        %10.2f" counterReadingEnd
    printfn "Total Gallons Consumed:     %10.2f" gallons
    printfn "======================================================"
    printfn "Therms Evaluation"
    printfn "------------------------------------------------------"
    printfn "HCF Total:                  %10.2f" HCFTotal
    printfn "First 35 Therms (* 1.5573): %10.2f" first25Therms
    printfn "First 20 Therms (* 1.2264): %10.2f" next15Therms
    printfn "Above 55 Therms (* 0.9624): %10.2f" above40Therms
    printfn "------------------------------------------------------"
    printfn "Water Usage Charges:        %10.2f" waterUsageCharges
    printfn "Sewer Charges:              %10.2f" sewerCharges
    printfn "======================================================"
    printfn "Bill Values"
    printfn "------------------------------------------------------"
    printfn "Environment Charges:        %10.2f" environmentCharges
    printfn "Service Charges:            %10.2f" serviceCharges
    printfn "Total Charges:              %10.2f" totalCharges
    printfn "Local Taxes:                %10.2f" localTaxes
    printfn "State Taxes:                %10.2f" stateTaxes
    printfn "------------------------------------------------------"
    printfn "Amount Due:                 %10.2f" amountDue
    printfn "======================================================"
  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
  12. Change the document as follows:
    printfn "Stellar Water Point"
    printfn "======================================================"
    printfn "To prepare an invoice, enter the following information"
    printfn "Types of Accounts"
    printfn "1. Residential Household"
    printfn "2. Laudromat"
    printfn "3. Health Clinic"
    printfn "4. Place of Worship/Youth Center"
    printfn "5. Government Agency"
    printfn "6. Other Business"
    printfn "------------------------------------------------------"
    printf "Enter Type of Account:          "
    let answer = stdin.ReadLine()
    printfn "------------------------------------------------------"
    
    printf "Counter Reading Start:          "
    let counterReadingStart = float(stdin.ReadLine())
    printf "Counter Reading End:            "
    let counterReadingEnd   = float(stdin.ReadLine())
    
    let gallons = counterReadingEnd - counterReadingStart
    let HCFTotal = gallons / 748.00
    
    let mutable localTaxes         = 0.00
    let mutable stateTaxes         = 0.00
    let mutable serviceCharges     = 0.00
    let mutable environmentCharges = 0.00
    let mutable first25Therms      = 0.00
    let mutable next15Therms       = 0.00
    let mutable above40Therms      = 0.00
    
    first25Therms            <- 25.00 * 1.5573
    next15Therms             <- 15.00 * 1.2264
    above40Therms            <- (HCFTotal - 40.00)  * 0.9624
    
    let waterUsageCharges    = first25Therms       + next15Therms + above40Therms;
    let sewerCharges         = waterUsageCharges   * 0.252;
    
    if answer = "1" then
        environmentCharges   <- waterUsageCharges * 0.0025
    else
        environmentCharges   <- waterUsageCharges * 0.0125
    
    if answer = "1" then
        serviceCharges       <- waterUsageCharges * 0.0328
    else
        serviceCharges       <- waterUsageCharges * 0.11643
    
    let totalCharges = waterUsageCharges  + sewerCharges + environmentCharges + serviceCharges
    
    if answer = "1" then
        localTaxes           <- totalCharges      * 0.005259
    else
        localTaxes           <- totalCharges      * 0.005259
    
    if answer = "1" then
        stateTaxes           <- totalCharges      * 0.002262
    else
        stateTaxes           <- totalCharges      * 0.002262
    
    let amountDue            = totalCharges + localTaxes + stateTaxes
    
    printfn "======================================================"
    printfn "Stellar Water Point - Customer Invoice"
    printfn "======================================================"
    printfn "Meter Reading"
    printfn "------------------------------------------------------"
    printfn "Counter Reading Start:      %10.2f" counterReadingStart
    printfn "Counter Reading End:        %10.2f" counterReadingEnd
    printfn "Total Gallons Consumed:     %10.2f" gallons
    printfn "======================================================"
    printfn "Therms Evaluation"
    printfn "------------------------------------------------------"
    printfn "HCF Total:                  %10.2f" HCFTotal
    printfn "First 35 Therms (* 1.5573): %10.2f" first25Therms
    printfn "First 20 Therms (* 1.2264): %10.2f" next15Therms
    printfn "Above 55 Therms (* 0.9624): %10.2f" above40Therms
    printfn "------------------------------------------------------"
    printfn "Water Usage Charges:        %10.2f" waterUsageCharges
    printfn "Sewer Charges:              %10.2f" sewerCharges
    printfn "======================================================"
    printfn "Bill Values"
    printfn "------------------------------------------------------"
    printfn "Environment Charges:        %10.2f" environmentCharges
    printfn "Service Charges:            %10.2f" serviceCharges
    printfn "Total Charges:              %10.2f" totalCharges
    printfn "Local Taxes:                %10.2f" localTaxes
    printfn "State Taxes:                %10.2f" stateTaxes
    printfn "------------------------------------------------------"
    printfn "Amount Due:                 %10.2f" amountDue
    printfn "======================================================"
  13. To execute the application, on the main menu, click Debug -> Start Without Debugging:
  14. When requested, for the type of account, type 1 and press Enter
  15. For the Counter Reading Start, type 92863 and press Enter
  16. 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 . . .
  17. Press Enter to close the window and return to your programming environment
  18. To execute the application again, on the main menu, click Debug -> Start Without Debugging:
  19. When requested, for the type of account, type 2 and press Enter
  20. For the Counter Reading Start, type 4205 and press Enter
  21. 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 . . .
  22. Press Q to close the window and return to your programming environment
  23. To start a new project, on the main menu of Microsoft Visual Studio, click File -> New -> Project...
  24. 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
  25. Change the Project Name to TaxPreparation01 and change or accept the project Location
  26. Click Next
  27. Make sure the Target Framework combo box is displaying .NET 7.0 (Standard Term Support). Click Create
  28. Change the document as follows:
    printfn "============================================"
    printfn " - Mississippi - State Income Tax -"
    printfn "============================================"
    
    let taxRate = 0.00
    
    printfn "Enter the information for tax preparation"
    printf "Gross Salary: "
    let grossSalary = float(stdin.ReadLine())
    
    let taxAmount = grossSalary * taxRate / 100.00
    let netPay    = grossSalary - taxAmount;
    
    printfn "=============================================="
    printfn " - Mississippi - State Income Tax -"
    printfn "----------------------------------------------"
    printfn $"Gross Salary: {grossSalary:f}"
    printfn "Tax Rate:     %0.2f%c" taxRate '%'
    printfn $"Tax Amount:   {taxAmount:f}"
    printfn $"Net Pay:      {netPay:f}"
    printfn "=============================================="
  29. To execute, on the main menu, click Debug -> Start Without Debugging
  30. 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 . . .
  31. To close the window and return to your programming environment, press W

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, besides the else, the F# language provides a keyword named elif. Its formula is:

if condition1 then statement-if[;]
elif condition2 then statement-elif[;]

If the statement of the elif section is long, you can write it on its own llne. In this case, you must indent it. The formula becomes:

if condition1 then statement-if[;]
elif statement-elif then
    statement2[;]

Or:

if condition-if then
    statement1[;]
elif statement-elif then
    statement2[;]

The first condition, condition1, would first be checked. If condition1 is true, then statement-if would execute. If condition1 is false, then condition2 would be checked. If condition2 is true, then statement-elif would execute. Any other result would be ignored. Here is an example:

let mutable age = 12

if age <= 17 then
    printfn "Teenager."
elif age < 65 then
    printfn "Adult."
elif age >= 65 then
    printfn "Senior Citizen."

age <- 36

if age <= 17 then
    printfn "Teenager."
elif age < 65 then
    printfn "Adult."
elif age >= 65 then
    printfn "Senior Citizen."

age <- 68;

if age <= 17 then
    printfn "Teenager."
elif age < 65 then
    printfn "Adult."
elif age >= 65 then
    printfn "Senior Citizen."

This would produce:

Teenager.
Adult.
Senior Citizen.

Press any key to continue . . .

Practical LearningPractical Learning: Using Else If

  1. Change the document as follows:
    printfn "============================================"
    printfn " - Mississippi - State Income Tax -"
    printfn "============================================"
    
    let mutable taxRate = 0.00
    
    printfn "Enter the information for tax preparation"
    printf "Gross Salary: "
    let grossSalary = float(stdin.ReadLine())
    
    // Mississippi
    if grossSalary  >= 10_000 then
        taxRate <- 5.00
    elif grossSalary  >= 5_000 then
        taxRate <- 4.00
    elif grossSalary  >= 1_000 then
        taxRate <- 3.00
    
    let taxAmount = grossSalary * taxRate / 100.00
    let netPay    = grossSalary - taxAmount;
    
    printfn "=============================================="
    printfn " - Mississippi - State Income Tax -"
    printfn "----------------------------------------------"
    printfn $"Gross Salary: {grossSalary:f}"
    printfn "Tax Rate:     %0.2f%c" taxRate '%'
    printfn $"Tax Amount:   {taxAmount:f}"
    printfn $"Net Pay:      {netPay:f}"
    printfn "=============================================="
  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:
    printfn "=========================================="
    printfn " - Amazing DeltaX - State Income Tax -"
    printfn "=========================================="
    
    let mutable taxRate = 0.00
    let mutable stateName = ""
    
    printfn "Enter the information for tax preparation"
    printfn "States"
    printfn " CO - Colorado"
    printfn " IN - Indiana"
    printfn " MI - Michigan"
    printfn " NC - North Carolina"
    printfn " PA - Pennsylvania"
    printfn " TN - Tennessee"
    printf "Enter State Abbreviation: "
    let abbr = stdin.ReadLine()
    printfn "--------------------------------------------"
    
    printf "Gross Salary: "
    let grossSalary = float(stdin.ReadLine())
    
    if abbr = "CO" then
        taxRate   <- 4.63
        stateName <- "Colorado"
    elif abbr = "IN" then
        taxRate   <- 3.23
        stateName <- "Indiana"
    elif abbr = "MI" then
        taxRate   <- 4.25
        stateName <- "Michigan"
    elif abbr = "NC" then
        taxRate   <- 5.25;
        stateName <- "North Carolina"
    elif abbr = "PA" then
        taxRate   <- 3.07
        stateName <- "Pennsylvania"
    elif abbr = "TN" then
        taxRate   <- 1.00
        stateName <- "Tennessee"
    
    let taxAmount = grossSalary * taxRate / 100.00
    let netPay    = grossSalary - taxAmount
    
    printfn "============================================"
    printfn " - Amazing DeltaX - State Income Tax -"
    printfn "--------------------------------------------"
    printfn $"State Name:   {stateName}"
    printfn $"Gross Salary: {grossSalary:f}"
    printfn "Tax Rate:     %0.2f%c" taxRate '%'
    printfn "--------------------------------------------"
    printfn $"Tax Amount:   {taxAmount:f}"
    printfn $"Net Pay:      {netPay:f}"
    printfn "============================================"
  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, you can use an alternate else condition as the last resort. Its formula is:

if condition1 then
    statement1[;]
elif condition2 then
    statement2[;]
else
    statement-n[;]

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

  1. Change the document as follows:
    printfn "Stellar Water Point"
    printfn "======================================================"
    printfn "To prepare an invoice, enter the following information"
    printfn "Types of Accounts"
    printfn "1. Residential Household"
    printfn "2. Laudromat"
    printfn "3. Health Clinic"
    printfn "4. Place of Worship/Youth Center"
    printfn "5. Government Agency"
    printfn "6. Other Business"
    printfn "------------------------------------------------------"
    printf "Enter Type of Account:          "
    let answer = stdin.ReadLine()
    printfn "------------------------------------------------------"
    
    printf "Counter Reading Start:          "
    let counterReadingStart = float(stdin.ReadLine())
    printf "Counter Reading End:            "
    let counterReadingEnd   = float(stdin.ReadLine())
    
    let gallons = counterReadingEnd - counterReadingStart
    let HCFTotal = gallons / 748.00
    
    let mutable localTaxes         = 0.00
    let mutable stateTaxes         = 0.00
    let mutable serviceCharges     = 0.00
    let mutable environmentCharges = 0.00
    let mutable first25Therms      = 0.00
    let mutable next15Therms       = 0.00
    let mutable above40Therms      = 0.00
    
    if HCFTotal  > 40.00 then
        first25Therms  <- 25.00 * 1.5573
        next15Therms   <- 15.00 * 1.2264
        above40Therms  <- (HCFTotal - 40.00)  * 0.9624
    elif HCFTotal  > 25.00 then
        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;
    
    let waterUsageCharges    = first25Therms       + next15Therms + above40Therms;
    let sewerCharges         = waterUsageCharges   * 0.252;
    
    if answer = "1" then
        environmentCharges   <- waterUsageCharges * 0.0025
    else
        environmentCharges   <- waterUsageCharges * 0.0125
    
    if answer = "1" then
        serviceCharges       <- waterUsageCharges * 0.0328
    else
        serviceCharges       <- waterUsageCharges * 0.11643
    
    let totalCharges = waterUsageCharges  + sewerCharges + environmentCharges + serviceCharges
    
    if answer = "1" then
        localTaxes           <- totalCharges      * 0.005259
    else
        localTaxes           <- totalCharges      * 0.005259
    
    if answer = "1" then
        stateTaxes           <- totalCharges      * 0.002262
    else
        stateTaxes           <- totalCharges      * 0.002262
    
    let amountDue            = totalCharges + localTaxes + stateTaxes
    
    printfn "======================================================"
    printfn "Stellar Water Point - Customer Invoice"
    printfn "======================================================"
    printfn "Meter Reading"
    printfn "------------------------------------------------------"
    printfn "Counter Reading Start:      %10.2f" counterReadingStart
    printfn "Counter Reading End:        %10.2f" counterReadingEnd
    printfn "Total Gallons Consumed:     %10.2f" gallons
    printfn "======================================================"
    printfn "Therms Evaluation"
    printfn "------------------------------------------------------"
    printfn "HCF Total:                  %10.2f" HCFTotal
    printfn "First 35 Therms (* 1.5573): %10.2f" first25Therms
    printfn "First 20 Therms (* 1.2264): %10.2f" next15Therms
    printfn "Above 55 Therms (* 0.9624): %10.2f" above40Therms
    printfn "------------------------------------------------------"
    printfn "Water Usage Charges:        %10.2f" waterUsageCharges
    printfn "Sewer Charges:              %10.2f" sewerCharges
    printfn "======================================================"
    printfn "Bill Values"
    printfn "------------------------------------------------------"
    printfn "Environment Charges:        %10.2f" environmentCharges
    printfn "Service Charges:            %10.2f" serviceCharges
    printfn "Total Charges:              %10.2f" totalCharges
    printfn "Local Taxes:                %10.2f" localTaxes
    printfn "State Taxes:                %10.2f" stateTaxes
    printfn "------------------------------------------------------"
    printfn "Amount Due:                 %10.2f" amountDue
    printfn "======================================================"
  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...elif ... elif and Else

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

if condition1 then statement1[;]
elif condition2 then statement2[;]
. . .
elif condition_n then 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 then
    statement1[;]
elif condition2 else
    statement2[;]
. . .
elif condition_n then
    statement_n[;]
else
    statement-n[;]

Here is an example:

let mutable age = 14

if age <= 17 then
    printfn "Teenager"
elif age <= 65 then
    printfn "Adult"
else
    printfn "Senior Citizen"

age <- 68;

if age <= 17 then
    printfn "Teenager"
elif age <= 65 then
    printfn "Adult"
else
    printfn "Senior Citizen"

age <- 36

if age <= 17 then
    printfn "Teenager"
elif age <= 65 then
    printfn "Adult"
else
    printfn "Senior Citizen"

This would produce:

Teenager
Senior Citizen
Adult
Press any key to continue . . .

Options on Conditional Statements

The Body of a Conditional Statement

The body of a conditional statement is the section influenced by the comparison. Remember that you can write the whole conditional statement on one line. Here is an example:

let shirtPrice = 34.95

if shirtPrice > 30.00 then printfn "The shirt is too expensive."

If you are writing a condition and its subsequent statement code on one line, the body of the conditional statement is from the then keyword to the end of the line or to the start of a comment. If you write the statement of the condition on the next line, remember that you must indent that next line. Here is an example:

let shirtPrice = 34.95
            
if shirtPrice > 30.00 then
printfn "The shirt is too expensive."

In case the statement is written on the next (indented) line, the body of the conditional statement is the whole indented section. As an option, to delimit the body of the conditional statement, start it with an indented begin and end it with the end keyword indented on the same level as begin. Between the begin and the end lines, write the statement(s) of the conditional statement. Here is an example:

let age = 12;

if age < 18 then
    begin
        printfn "The person is less than 18 years old."
    end

Of course, the statement can use many lines of code. Here is an example:

printfn "FUN DEPARTMENT STORE"
printfn "======================================================="
printfn "Payroll Preparation"
printfn "-------------------------------------------------------"
printfn "Enter the following pieces of information"
printfn "-------------------------------------------------------"
printfn "Employee Information"
printfn "-------------------------------------------------------"
printf "First Name:    "
let firstName : string = stdin.ReadLine()
printf "Last Name:     "
let lastName : string = stdin.ReadLine()
printf "Hourly Salary: "
let hSalary : float = float(stdin.ReadLine())
printfn "-------------------------------------------------------"
printfn "Time worked"
printfn "-------------------------------------------------------"

printf "Monday:        "
let mon : float = float(stdin.ReadLine())

printf "Tuesday:       "
let tue = float(stdin.ReadLine())

printf "Wednesday:     "
let wed = float(stdin.ReadLine())

printf "Thursday:      "
let thu = float(stdin.ReadLine())

printf "Friday:        "
let fri = float(stdin.ReadLine())

let timeWorked = mon + tue + wed + thu + fri

let mutable regTime  = timeWorked
let mutable overtime = 0.00
let mutable overPay  = 0.00
let mutable regPay   = hSalary * timeWorked

if timeWorked > 40.00 then
    begin
        regTime  <- 40.00
        regPay   <- hSalary * 40.00
        overtime <- timeWorked - 40.00
        overPay  <- hSalary * 1.50 * overtime
    end

let netPay : float = regPay + overPay

printfn "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+"
printfn "FUN DEPARTMENT STORE"
printfn "======================================================="
printfn "Payroll Evaluation"
printfn "======================================================="
printfn "Employee Information"
printfn "-------------------------------------------------------"
printfn "Full Name:     %s %s" firstName lastName
printfn "Hourly Salary: %0.2f" hSalary
printfn "======================================================="
printfn "Time Worked Summary"
printfn "--------+---------+-----------+----------+-------------"
printfn " Monday | Tuesday | Wednesday | Thursday | Friday"
printfn "--------+---------+-----------+----------+-------------"
printfn "  %0.2f  |   %0.2f  |    %0.2f   |   %0.2f   |  %0.2f" mon tue wed thu fri
printfn "========+=========+===========+==========+============="
printfn "                      Pay Summary"
printfn "-------------------------------------------------------"
printfn "                                   Time   Pay"
printfn "-------------------------------------------------------"
printfn "                     Regular:    %0.2f   %0.2f" regTime regPay
printfn "-------------------------------------------------------"
printfn "                     Overtime:    %0.2f   %0.2f" overtime overPay
printfn "======================================================="
printfn "                     Net Pay:            %0.2f" netPay
printfn "======================================================="

In the above code, we used only an if conditional statement. In the same way, an else conditional statement has its own body. As seen so far, you can simply indent the statement(s) of its section. Otherwise, to explicitly indicate its body, you can use a begin and an end keywords. This would be done as follows:

if condition then
    statement1[;]
else
    begin
        statement2[;]
    end

Either the if or the else statement can have a body whether the other has a body or not. This can be done as follows:

if condition then
    begin
        if-statement1[;]
        if-statement2[;]
        . . .
        if-statement_n[;]
    end
else
    begin
        else-statement1[;]
        else-statement2[;]
        . . .
        else-statement_n[;]
    end

In the same way, if the series of conditions includes a last else conditional statement, ths statement(s) of that section can be delimited by a body delimited by the begin and an end keywords. This would be done as follows:

if condition1 then
    statement1[;]
elif condition2 then
    statement2[;]
else
    begin
        statement-n[;]
    end

Omitting Checking for Equality

One way to formulate a conditional statement is to find out whether a situation is true or false. In this case, one of the operands must be a Boolean value as true or false. The other operand can be a Boolean variable. One of the operands can also be an expression that holds a Boolean value. Imagine that you want to evaluate the condition as true. Here is an example:

let wasSubmitted = true

if wasSubmitted = true then
    printfn("The time sheet was submitted.")

If a Boolean variable (currently) holds a true value (at the time you are trying to access it), when you are evaluating the expression as being true, you can omit the = true or the true = expression in your statement. Therefore, the above expression can be written as follows:

let wasSubmitted = true

if wasSubmitted then
    printfn("The time sheet was submitted.")

This would produce the same result as previously.

Negating a Condition

If you have a logical expression or value, to let you get its logical opposite, the F# language provides a logical operator named not by. The formula to use it is:

not variable-or-expression

There are various ways the logical not operator can be used. The classic way is to check the state of a variable. To nullify a variable, you can write the not operator to its left. Here is an example:

let employed = true

let validation = not employed

printfn"Employed:   %b" employed
printfn"Validation: %b" validation

This would produce:

Employed:   true
Validation: false

Press any key to close this window . . .

When a variable has been "negated", its logical value changes. If the logical value was true, it would be changed to false. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it.

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2009-2023 FunctionX Monday 14 February 2022 Next