Conditional Statements: Else


If Something Else is True

When used by itself, the if condition does not specify what to do if its comparison produces a false result. To let you take care this, you can add the else keyword to an if condition. The formula to follow is:

if VariableOrValue1 Operator VariableOrValue2 then Statement1 else Statement2

This time, the if statement is processed by Statement1. If the condition is false, add the else. Statement2 would be processed if the comparison of the Operator produces a false resust. Here is an example:

open System
open System.Windows.Forms

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

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

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

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

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

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

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

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

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

let btnCalculateClick(e) =
    let weeklySalary = ref 0.00
    let hourlySalary = float txtHourlySalary.Text
    let weeklyTime   = float txtWeeklyTime.Text
    if weeklyTime > 40.00 then
        let overtime = weeklyTime - 40.00
        let overtimePay = hourlySalary * 1.50 * overtime

        weeklySalary := (hourlySalary * 40.00) + overtimePay
        weeklySalary := hourlySalary * weeklyTime
    let strWeeklySalary = sprintf "%0.02f" !weeklySalary
    txtWeeklySalary.Text <- strWeeklySalary


let btnCloseClick(e) = payroll.Close()


Here is an example of running the program:

If Something Else is True

If Something Else is True


Windows Controls and Conditional Statements


Introduction to Boolean Values and Windows Controls

The .NET Framework provides two Windows controls that are ready for Boolean values.

The Check Box Control: The most fundamental control for Boolean values is the check box. It is created using the CheckBox class. Here is an example of creating a check box:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text <- "Exercise"

let isVisible = new CheckBox()

isVisible.Left  <- 12
isVisible.Top   <- 18
isVisible.Width <- 120
isVisible.Text  <- "Control is Visible"



The CheckBox class is equipped with a property named Checked. If/when the control is checked, its Checked property holds a value of true. If not, the property is set to false. At any time, you can programmatically find out what value that property is holding. Here is an example:

open System
open System.Windows.Forms

let exercise = new Form()
exercise.Text   <- "Exercise"
exercise.Width  <- 250
exercise.Height <- 80
// Check Box: Is Visible

let chkIsVisible = new CheckBox()
chkIsVisible.Left  <- 12
chkIsVisible.Top   <- 18
chkIsVisible.Width <- 110
chkIsVisible.Text  <- "Control is Visible"

// Text Box
let txtContinent = new TextBox()
txtContinent.Left    <- 130
txtContinent.Top     <- 18
txtContinent.Visible <- false

let chkIsVisibleClick(e) =
    if chkIsVisible.Checked then
        txtContinent.Visible <- true
        txtContinent.Visible <- false


Here is an example of running the program:

Introduction to Boolean Values and Windows Controls

Introduction to Boolean Values and Windows Controls

Radio Buttons: The radio button is another valuable control for Boolean values. Like the check box, the radio button is equipped with the Checked property. If/when the radio button is filled, the property holds a true value. If/when it is empty, the property holds a false value.

Unlike the check box, the radio button is hardly used by itself. It is usually accompanied by other radio buttons and they function has a group. Consequently, you can use a variable, usually integral-based so that, when a radio button is clicked, you set the value of that variable. If another radio button is clicked, you update the value of that variable. Then, when it is time, you can get the value of that variable to find out what radio button is currently checked in the group.

Introduction to Boolean Properties

A property is said to be Boolean if it can be true or false. For example, controls have a property named Visible that specifies whether a control can be seen or hidden.

Introduction to Conditional Statements Used on Windows Controls

Practically all Windows controls can be used in conditional statements. Some controls are text based. You can enquire about the contents of such controls and validate something about their values or behaviors.

Many controls display text. These include the text box, the masked text box, etc. By using a conditional statement, you can enquire about the text of a control to compare it to some value and decide what to do.

Some controls display text but their items are stored in a list. These include the combo box, the list box, the domain up-down control, etc. Those controls have a Text property whose value can be enquired the same way it is done for a text-based control.

Collection-based controls are not directly made to display text. Instead, they include objects that subsequently display text. These include the list view, the tree view, the data grid control, etc. The items of those controls have a Text property. That property may not be directly available from the control itself. This means that you must first get to the desired item. You can then apply a conditional statement on that property the same way you would on a text-based property.

Although the items in a combo box or a list box are text, their positions, called indexes, are stored in a 0-based list. The first item on top of the list has an index of 0. The second from top has an index of 1, and so on. Based on this, you can use a conditional statement to find out what item is currently selected. This means that you can base your statement on either text or index of the item and decide what to do.


Home Copyright © 2015 FunctionX Home