Home

Conditional Statements: What Else If a Condition is True

   

Description

The if and else conditions we have used above provide only two options. Sometimes, more alternatives must be considered. This is done using the elif keyword. If you have two combinations of comparisons to perform, the formula to follow is:

if VariableOrValue1 Operator1 VariableOrValue2 then
    Statement1
elif VariableOrValue3 Operator2 VariableOrValue4 then
    Statement2

If you have many combinations of comparisons to perform, you can use and elif comparison for each condition. The formula to follow is:

if VariableOrValue1 Operator1 VariableOrValue2 then
    Statement1
elif VariableOrValue_n1 Operator_n1 VariableOrValue_n2 then
    Statement_n1
elif VariableOrValue_n3 Operator_n2 VariableOrValue_n4 then
    Statement_n2
elif VariableOrValue_n5 Operator_n3 VariableOrValue_n6 then
    Statement_n3

Here is an example:

open System
open System.Windows.Forms 

let exercise = new Form()
exercise.Width  <- 340
exercise.Height <- 142
exercise.Text   <- "Hotel Management"
exercise.StartPosition <- FormStartPosition.CenterScreen

let lblDesiredRoomType = new Label()
lblDesiredRoomType.Left  <-  12
lblDesiredRoomType.Top   <-  17
lblDesiredRoomType.Width <- 122
lblDesiredRoomType.Text  <- "Type of Room Desired:"
exercise.Controls.Add(lblDesiredRoomType)

let cbxRoomTypes   = new ComboBox()
let lblDescription = new Label()
cbxRoomTypes.Left  <- 140
cbxRoomTypes.Top   <-  14
cbxRoomTypes.Width <- 175
cbxRoomTypes.Items.AddRange([| "Standard Bedroom"; "Studio Suite"; "Small Conference Room"; "Large Conference Room" |])

let cbxRoomTypesSelectedIndexChanged e =
    if cbxRoomTypes.Text = "Standard Bedroom" then
        lblDescription.Text <- "Standard Bedroom: 1 Queen bed, 1 Table, 2 chairs, 1 Lazy Boy, 1 Mini-size Refrigerator"
    elif cbxRoomTypes.Text = "Studio Suite" then
        lblDescription.Text <- "Studio Suite: 2 Queen beds, 1 Table, 2 chairs, 1 Couch, 1 Lazy Boy, 1 Full-size Refrigerator"
    elif cbxRoomTypes.Text = "Small Conference Room" then
        lblDescription.Text <- "Small Conference Room: 10-Seat Capacity, Internet Access, and Audio/Video Available"
    elif cbxRoomTypes.Text = "Large Conference Room" then
        lblDescription.Text <- "Large Conference Room: Capacity 600, 550 Seats, Internet Access, and Audio/Video Conference"

cbxRoomTypes.SelectedIndexChanged.Add cbxRoomTypesSelectedIndexChanged
exercise.Controls.Add cbxRoomTypes

let lblAvailability = new Label()
lblAvailability.Left  <-  12
lblAvailability.Top   <-  47
lblAvailability.Width <- 102
lblAvailability.Text  <- "Availability:"
exercise.Controls.Add lblAvailability

lblDescription.Left   <- 140
lblDescription.Top    <-  44
lblDescription.Width  <- 175
lblDescription.Height <-  50
exercise.Controls.Add(lblDescription)

Application.Run exercise   

Here is an example of running the program:

What Else If a Condition is True

What Else If a Condition is True

What Else If a Condition is True

If none of the elif comparisons applies, you can add a last else statement. The formula to follow is:

if VariableOrValue1 Operator1 VariableOrValue2 then
    Statement1
elif VariableOrValue_n1 Operator_n1 VariableOrValue_n2 then
    Statement_n1
elif VariableOrValue_n3 Operator_n2 VariableOrValue_n4 then
    Statement_n2
elif VariableOrValue_n5 Operator_n3 VariableOrValue_n6 then
    Statement_n3
else
    Statement_X

Here is an example:

open System
open System.Windows.Forms 

let exercise = new Form()
exercise.Width  <- 402
exercise.Height <- 80
exercise.Text   <- "Employee Login"

let lblEmployeeNumber = new Label()
let txtEmployeeNumber = new TextBox()
let txtEmployeeName   = new TextBox()

let txtEmployeeNumberLostFocus(e) =
    if txtEmployeeNumber.Text = "4046-8611" then
        txtEmployeeName.Text <- "Jonathan Laughter"
    elif txtEmployeeNumber.Text = "4860-5816" then
        txtEmployeeName.Text <- "Lucy Harsan"
    elif txtEmployeeNumber.Text = "8847-8088" then
        txtEmployeeName.Text <- "Frank Fortune"
    elif txtEmployeeNumber.Text = "7697-2741" then
        txtEmployeeName.Text <- "Elizabeth Parsons"
    else
        txtEmployeeName.Text <- ""

lblEmployeeNumber.Left  <- 25
lblEmployeeNumber.Top   <- 21
lblEmployeeNumber.Width <- 70
lblEmployeeNumber.Text <- "Employee #:"
exercise.Controls.Add(lblEmployeeNumber)

txtEmployeeNumber.Left <- 97
txtEmployeeNumber.Top  <- 18
txtEmployeeNumber.Width <- 78
txtEmployeeNumber.Leave.Add(txtEmployeeNumberLostFocus)
exercise.Controls.Add(txtEmployeeNumber)

txtEmployeeName.Left  <- 181
txtEmployeeName.Top   <-  18
txtEmployeeName.Width <- 195
exercise.Controls.Add(txtEmployeeName)

Application.Run(exercise)

Here is an example of running the program:

What Else If a Condition is True

What Else If a Condition is True

What Else If a Condition is True

     
     
 

Home Copyright © 2015, FunctionX Home