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:
In the same way, you can nest one conditional statement in one, then nest that new one in another conditional statement, and so on.
Conjunctions and disjunctions can be used in the same expression. A conjunction (or disjunction) can be used to evaluate one sub-expression while a disjunction (or conjunction) can be used to evaluate another sub-expression. Consider the following example: (* This program assists a clerk in a hotel to check the availability of rooms. The customer must specify how many people need a stay.*) open System open System.Windows.Forms let hotelManagement = new Form() hotelManagement.Width <- 340 hotelManagement.Height <- 110 hotelManagement.Text <- "Hotel Management" // Label: Number of Guests let lblGuests = new Label() lblGuests.Left <- 23 lblGuests.Top <- 25 lblGuests.Width <- 102 lblGuests.Text <- "Number of Guests:" hotelManagement.Controls.Add lblGuests // Text Box: Guests let txtGuests = new TextBox() txtGuests.Left <- 126 txtGuests.Top <- 22 txtGuests.Width <- 27 txtGuests.Text <- "0" hotelManagement.Controls.Add txtGuests // Label: Daily Rate let lblDailyRate = new Label() lblDailyRate.Left <- 157 lblDailyRate.Top <- 25 lblDailyRate.Width <- 33 lblDailyRate.Text <- "Rate:" hotelManagement.Controls.Add lblDailyRate // Text Box: Daily Rate let txtDailyRate = new TextBox() txtDailyRate.Left <- 192 txtDailyRate.Top <- 22 txtDailyRate.Width <- 42 txtDailyRate.Text <- "0.00" hotelManagement.Controls.Add txtDailyRate // Label: Per Night let lblPerNight = new Label() lblPerNight.Left <- 234 lblPerNight.Top <- 25 lblPerNight.Width <- 37 lblPerNight.Text <- "/Night" hotelManagement.Controls.Add lblPerNight // Label: Available Rooms let lblAvailableRooms = new Label() lblAvailableRooms.Left <- 23 lblAvailableRooms.Top <- 52 lblAvailableRooms.Width <- 95 lblAvailableRooms.Text <- "Available Rooms:" hotelManagement.Controls.Add lblAvailableRooms // Text Box: Available Rooms let txtAvailableRooms = new TextBox() txtAvailableRooms.Left <- 126 txtAvailableRooms.Top <- 50 txtAvailableRooms.Width <- 180 hotelManagement.Controls.Add txtAvailableRooms let txtGuestsLostFocus e = let guests = int txtGuests.Text let singleRate = 98.85 let availableRooms = ref "" // If one or two people (only) will occupy the room, apply the daily/nightly rate if (guests = 1) || (guests = 2) then txtDailyRate.Text <- string singleRate txtAvailableRooms.Text <- "103, 107, 108, 211, 212, 215, 218" // If 3 to 5 people will occupy the room, add half to the daily rate elif (guests >= 3) && (guests <= 5) then let appliedRate = singleRate + (singleRate * 0.50) txtDailyRate.Text <- sprintf "%0.02f" appliedRate txtAvailableRooms.Text <- "104, 106, 210, 214, 216, 313" (* If there are more than 5 people, there is no sigle bedroom t hat can accommodate such a number. As a result, get more rooms. *) else txtDailyRate.Text <- "0.00" txtAvailableRooms.Text <- "Please get more than one room." txtGuests.Leave.Add txtGuestsLostFocus do Application.Run hotelManagement This would produce:
As seen previously, one way you can combine conditional statements is by nesting them. |