Home

Conditional Statements: IF

 

If a Condition is True

 

Introduction

The above comparison operators allow you to formulate conditions to find out if a comparison produces a true or false result. To get that result, you can use the if keyword in a formula as follows:

if VariableOrValue1 Operator VariableOrValue2 then Statement

The if and the then keywords are required. VariableOrValue1 corresponds to Value1 we used in the previous formulas. VariableOrValue2 corresponds to Value2 we used in the previous formulas. Operator is one of the operators we reviewed previously. After the then keyword, you should (must) specify what to do if the comparison produces a true result. Here is an example:

open System
open System.Windows.Forms

let gasUtilityCompanyBillPreparation = new Form()
gasUtilityCompanyBillPreparation.Text   <- "Gas Utility Company"
gasUtilityCompanyBillPreparation.Width  <- 230
gasUtilityCompanyBillPreparation.Height <- 195

let lblConsumption = new Label()
lblConsumption.Left  <- 18
lblConsumption.Top   <- 18
lblConsumption.Width <- 100
lblConsumption.Text  <- "Comsumption:"
gasUtilityCompanyBillPreparation.Controls.Add(lblConsumption)

let txtConsumption = new TextBox()
txtConsumption.Left  <- 122
txtConsumption.Top   <- 16
txtConsumption.Width <- 75
txtConsumption.Text  <- "0.00"
gasUtilityCompanyBillPreparation.Controls.Add(txtConsumption)

let btnCalculate = new Button()
btnCalculate.Left  <- 122
btnCalculate.Top   <-  44
btnCalculate.Text  <- "Calculate"

let lblPricePerCCF = new Label()
lblPricePerCCF.Left  <- 18
lblPricePerCCF.Top   <- 78
lblPricePerCCF.Width <- 90
lblPricePerCCF.Text  <- "Price Per CCF:"
gasUtilityCompanyBillPreparation.Controls.Add(lblPricePerCCF)

let txtPricePerCCF = new TextBox()
txtPricePerCCF.Left  <- 122
txtPricePerCCF.Top   <- 74
txtPricePerCCF.Width <- 75
txtPricePerCCF.Text  <- "0.00"
gasUtilityCompanyBillPreparation.Controls.Add(txtPricePerCCF)

let lblMonthlyCharges = new Label()
lblMonthlyCharges.Left  <- 18
lblMonthlyCharges.Top   <- 104
lblMonthlyCharges.Width <- 100
lblMonthlyCharges.Text  <- "Monthly Charges:"
gasUtilityCompanyBillPreparation.Controls.Add(lblMonthlyCharges)

let txtMonthlyCharges = new TextBox()
txtMonthlyCharges.Left  <-  122
txtMonthlyCharges.Top   <- 102
txtMonthlyCharges.Width <-  75
txtMonthlyCharges.Text  <- "0.00"
gasUtilityCompanyBillPreparation.Controls.Add(txtMonthlyCharges)

let btnCalculateClick(e) =
    let mutable pricePerCCF = 0.00
    let mutable monthlyCharges = 0.00
    let consumption = float txtConsumption.Text

    pricePerCCF <- 50.00

    if consumption >= 0.50 then
        pricePerCCF <- 35.00

    txtPricePerCCF.Text <- sprintf "%0.02f" pricePerCCF

    pricePerCCF <- float txtPricePerCCF.Text

    monthlyCharges <- consumption * pricePerCCF

    txtMonthlyCharges.Text  <- sprintf "%0.02f" monthlyCharges

btnCalculate.Click.Add(btnCalculateClick)
gasUtilityCompanyBillPreparation.Controls.Add(btnCalculate)

let btnClose = new Button()
btnClose.Left  <- 122
btnClose.Top   <- 130
btnClose.Width <-  75
btnClose.Text  <- "Close"
let btnCloseClick(e) = gasUtilityCompanyBillPreparation.Close()
btnClose.Click.Add(btnCloseClick)
gasUtilityCompanyBillPreparation.Controls.Add(btnClose)

Application.Run(gasUtilityCompanyBillPreparation)

Here is an example of running the program:

Conditional Statements

Conditional Statements

Nesting a Conditional Statement

A conditional statement has a body, which is from where the condition is defined to where its behavior ends. In the body of the conditional statement, you can create another conditional statement. This is referred to as nesting the condition. Here is an example:

open System
open System.Windows.Forms

let payroll = new Form()
payroll.Text   <- "Payroll Preparation"
payroll.Width  <- 290
payroll.Height <- 140

let lblHourlySalary = new Label()
lblHourlySalary.Left  <- 18
lblHourlySalary.Top   <- 18
lblHourlySalary.Width <- 82
lblHourlySalary.Text  <- "Hourly Salary:"
payroll.Controls.Add(lblHourlySalary)

let txtHourlySalary = new TextBox()
txtHourlySalary.Left  <- 102
txtHourlySalary.Top   <- 16
txtHourlySalary.Width <- 64
txtHourlySalary.Text  <- "0.00"
payroll.Controls.Add(txtHourlySalary)

let chkPaidOvertime = new CheckBox()
chkPaidOvertime.Left <- 175
chkPaidOvertime.Top  <-  14
chkPaidOvertime.Text <- "Paid Overtime"
payroll.Controls.Add(chkPaidOvertime)

let lblWeeklyTime = new Label()
lblWeeklyTime.Left  <- 18
lblWeeklyTime.Top   <- 46
lblWeeklyTime.Width <- 82
lblWeeklyTime.Text  <- "Weekly Time:"
payroll.Controls.Add(lblWeeklyTime)

let txtWeeklyTime = new TextBox()
txtWeeklyTime.Left  <- 102
txtWeeklyTime.Top   <-  44
txtWeeklyTime.Width <-  64
txtWeeklyTime.Text  <- "0.00"
payroll.Controls.Add(txtWeeklyTime)

let btnCalculate = new Button()
btnCalculate.Left  <- 176
btnCalculate.Top   <-  43
btnCalculate.Width <-  88
btnCalculate.Text  <- "Calculate"

let lblWeeklySalary = new Label()
lblWeeklySalary.Left  <- 18
lblWeeklySalary.Top   <- 76
lblWeeklySalary.Width <- 82
lblWeeklySalary.Text  <- "Weekly Salary:"
payroll.Controls.Add(lblWeeklySalary)

let txtWeeklySalary = new TextBox()
txtWeeklySalary.Left  <- 102
txtWeeklySalary.Top   <-  74
txtWeeklySalary.Width <-  64
txtWeeklySalary.Text  <- "0.00"
payroll.Controls.Add(txtWeeklySalary)

let btnClose = new Button()
btnClose.Left  <- 176
btnClose.Top   <-  73
btnClose.Width <-  88
btnClose.Text  <- "Close"

let btnCalculateClick(e) =
    let weeklySalary = ref 0.00
    let hourlySalary = float txtHourlySalary.Text
    let weeklyTime   = float txtWeeklyTime.Text
    
    weeklySalary := hourlySalary * weeklyTime

    if chkPaidOvertime.Checked then
        if weeklyTime > 40.00 then
            let overtime = weeklyTime - 40.00
            let overtimePay = hourlySalary * 1.50 * overtime
        
            weeklySalary := (hourlySalary * 40.00) + overtimePay
    
    let strWeeklySalary = sprintf "%0.02f" !weeklySalary
    txtWeeklySalary.Text <- strWeeklySalary

btnCalculate.Click.Add(btnCalculateClick)
payroll.Controls.Add(btnCalculate)

let btnCloseClick(e) = payroll.Close()
btnClose.Click.Add(btnCloseClick)
payroll.Controls.Add(btnClose)

Application.Run(payroll)

Here is an example of running the program:

Nesting a Conditional Statement Nesting a Conditional Statement

In the same way, you can nest one conditional statement in one, then nest that new one in another conditional statement, and so on.

     
     
 

Home Copyright © 2015 FunctionX Home