Home

Conditional Statements: Conjunctions

   

Description

When you nest one condition in another condition as in:

if Condition1 then	// The first or external condition
    if Condition2 then	// The second or internal condition
	Statement(s)

you are in fact saying that "if Condition1 verifies, Then if Condition2 verifies, do this...". The external condition must be verifies first as being true or false (depending on how you wrote it). If that first (the external) condition is true, the second (the internal) condition is verified. If the first (the external) condition is not verified, the second (the external) condition is not verified.. To support  a simplified technique to apply this scenario, the F# language provides the Boolean "And" operator, represented as &&. Its primary formula is:

Condition1 && Condition2

You must formula each condition to produce a true or a false result. The result is as follows:

  • If Condition1 is true and Condition2 is true, the whole expression produces a true result. Here is an example:
    open System
    open System.Windows.Forms
    
    let brokerage = new Form()
    brokerage.Text   <- "Brokerage Company"
    brokerage.Width  <- 230
    brokerage.Height <- 260
    
    let lblNumberOfShares = new Label()
    lblNumberOfShares.Left  <- 18
    lblNumberOfShares.Top   <- 18
    lblNumberOfShares.Width <- 100
    lblNumberOfShares.Text  <- "Number of Shares:"
    brokerage.Controls.Add(lblNumberOfShares)
    
    let txtNumberOfShares = new TextBox()
    txtNumberOfShares.Left  <- 122
    txtNumberOfShares.Top   <- 16
    txtNumberOfShares.Width <- 75
    txtNumberOfShares.Text  <- "0"
    brokerage.Controls.Add(txtNumberOfShares)
    
    let lblPricePerShare = new Label()
    lblPricePerShare.Left  <- 18
    lblPricePerShare.Top   <- 46
    lblPricePerShare.Width <- 90
    lblPricePerShare.Text  <- "Price Per Share:"
    brokerage.Controls.Add(lblPricePerShare)
    
    let txtPricePerShare = new TextBox()
    txtPricePerShare.Left  <- 122
    txtPricePerShare.Top   <- 44
    txtPricePerShare.Width <- 75
    txtPricePerShare.Text  <- "0"
    brokerage.Controls.Add(txtPricePerShare)
    
    let btnCalculate = new Button()
    btnCalculate.Left  <- 122
    btnCalculate.Top   <-  72
    btnCalculate.Text  <- "Calculate"
    
    let lblPrincipal = new Label()
    lblPrincipal.Left  <- 18
    lblPrincipal.Top   <- 102
    lblPrincipal.Width <- 80
    lblPrincipal.Text  <- "Principal:"
    brokerage.Controls.Add(lblPrincipal)
    
    let txtPrincipal = new TextBox()
    txtPrincipal.Left  <-  122
    txtPrincipal.Top   <- 100
    txtPrincipal.Width <-  75
    txtPrincipal.Text  <- "0.00"
    brokerage.Controls.Add(txtPrincipal)
    
    let lblCommission = new Label()
    lblCommission.Left  <- 18
    lblCommission.Top   <- 132
    lblCommission.Width <- 80
    lblCommission.Text  <- "Commission:"
    brokerage.Controls.Add(lblCommission)
    
    let txtCommission = new TextBox()
    txtCommission.Left  <- 122
    txtCommission.Top   <- 128
    txtCommission.Width <- 75
    txtCommission.Text  <- "0.00"
    brokerage.Controls.Add(txtCommission)
    
    let lblTotalInvestment = new Label()
    lblTotalInvestment.Left  <- 18
    lblTotalInvestment.Top   <- 160
    lblTotalInvestment.Width <- 94
    lblTotalInvestment.Text  <- "Total Investment:"
    brokerage.Controls.Add(lblTotalInvestment)
    
    let txtTotalInvestment = new TextBox()
    txtTotalInvestment.Left  <- 122
    txtTotalInvestment.Top   <- 156
    txtTotalInvestment.Width <-  75
    txtTotalInvestment.Text  <- "0.00"
    brokerage.Controls.Add(txtTotalInvestment)
    
    let btnCalculateClick(e) =
        let mutable principal = 0.00
        let mutable commission = 0.00
        let numberOfShares = float txtNumberOfShares.Text
        let pricePerShare  = float txtPricePerShare.Text
    
        principal <- numberOfShares * pricePerShare
    
        if principal >= 0.00 && principal <= 2500.00 then
            commission <- 26.25 + (principal * 0.0014)
        if principal > 2500.00 && principal <= 6000.00 then
            commission <- 45.00 + (principal * 0.0054)
        if principal > 6000.00 && principal <= 20000.00 then
            commission <- 60.00 + (principal * 0.0028)
        if principal > 20000.00 && principal <= 50000.00 then
            commission <- 75.00 + (principal * 0.001875)
        if principal > 50000.00 && principal <= 500000.00 then
            commission <- 131.25 + (principal * 0.0009)
        if principal > 500000.00 then
            commission <- 206.25 + (principal * 0.000075)
    
        txtPrincipal.Text  <- sprintf "%0.02f" principal
        txtCommission.Text <- sprintf "%0.02f" commission
        txtTotalInvestment.Text <- sprintf "%0.02f" (principal + commission)
    
    btnCalculate.Click.Add(btnCalculateClick)
    brokerage.Controls.Add(btnCalculate)
    
    let btnClose = new Button()
    btnClose.Left  <- 122
    btnClose.Top   <- 192
    btnClose.Width <-  75
    btnClose.Text  <- "Close"
    let btnCloseClick(e) = brokerage.Close()
    btnClose.Click.Add(btnCloseClick)
    brokerage.Controls.Add(btnClose)
    
    Application.Run(brokerage)

    Here is an example of running the program:

    Boolean Conjunction Boolean Conjunction
  • If either Condition1 or Condition2 is false (including if both are false), the whole expression produces a false result

To make your code easier to read, it is a good idea to include each Boolean operation in its own parentheses. Here are examples:

let btnCalculateClick(e) =
    let mutable principal = 0.00
    let mutable commission = 0.00
    let numberOfShares = float txtNumberOfShares.Text
    let pricePerShare  = float txtPricePerShare.Text

    principal <- numberOfShares * pricePerShare

    if (principal >= 0.00) && (principal <= 2500.00) then
        commission <- 26.25 + (principal * 0.0014)
    if (principal > 2500.00) && (principal <= 6000.00) then
        commission <- 45.00 + (principal * 0.0054)
    if (principal > 6000.00) && (principal <= 20000.00) then
        commission <- 60.00 + (principal * 0.0028)
    if (principal > 20000.00) && (principal <= 50000.00) then
        commission <- 75.00 + (principal * 0.001875)
    if (principal > 50000.00) && (principal <= 500000.00) then
        commission <- 131.25 + (principal * 0.0009)
    if principal > 500000.00 then
        commission <- 206.25 + (principal * 0.000075)

    txtPrincipal.Text  <- sprintf "%0.02f" principal
    txtCommission.Text <- sprintf "%0.02f" commission
    txtTotalInvestment.Text <- sprintf "%0.02f" (principal + commission)

Depending on your program, if two conditions are not enough, you can create as many conjunctions as you want. The formula to follow is:

Condition1 && Condition2 && Condition3 && . . . && Condition_n

When the expression is checked, if any of the operations is false, the whole operation is false. The only time the whole operation is true is if all of the operations are true.

Of course, you can nest a Boolean condition inside another conditional statement.

     
     
 

Home Copyright © 2015, FunctionX Home