A Boolean variable is one that holds a true or a false value. As seen in our introduction, when initialyzing the variable, you can assign it true or false. As an alternative, you can assign a comparison operation to it. The result of the operation can be stored in a variable. The formula to follow would be: let variable-name = operand1 operator operand2 To make your code easy to read, you can (should) include the Boolean operation in parentheses. The formula to follow would be: let variable-name = (operand1 operator operand2) Here is an example that compares the values of two variables: open System
open System.Windows.Forms
let exercise = new Form()
exercise.Width <- 280
exercise.Height <- 77
exercise.Text <- "Boolean Operation"
let lblMessage = new Label()
lblMessage.Left <- 21
lblMessage.Top <- 18
lblMessage.Width <- 258
let number1 = 8
let number2 = 12
let result = (number1 = number2)
lblMessage.Text <- "The Equality between 8 and 12 produces " + (string result)
exercise.Controls.Add(lblMessage)
Application.Run(exercise)
In this case, if the variables hold the same value, a value of true is stored in the Boolean variable. If not, a value of false is stored in the Boolean variable. The above program would produce:
Of course, you can use any of the Boolean operators we reviewed. Here is an example that uses the inequality operator: open System
open System.Windows.Forms
let exercise = new Form()
exercise.Width <- 280
exercise.Height <- 77
exercise.Text <- "Boolean Operation"
let lblMessage = new Label()
lblMessage.Left <- 21
lblMessage.Top <- 18
lblMessage.Width <- 258
let number1 = 8
let number2 = 12
let result = (number1 <> number2)
lblMessage.Text <- "The Equality between 8 and 12 produces " + (string result)
exercise.Controls.Add(lblMessage)
Application.Run(exercise)
This would produce:
|