Home

Boolean Values in F#

   

Introduction

A value is said to be Boolean if it can hold a value as (only) being either true or false. Such a value is handled by a data type named bool

When declaring a variable for Boolean values, assign it a true or a false value. Here is an example:

let youAreTheMan = true

To support such values, the data type to use is named bool. You can apply it when declaring the variable. Here is an example:

let youAreTheMan : bool = true

To include such a value in a call to sprintf, use %b in the placeholder.

To convert a value to Boolean, call the bool() function.

Boolean Operations

Boolean variables use their own operators. Those operators are used to compare two values and produce true or false as a result of the comparison.

The formula used for a Boolean operation is:

operand1 operator operand2

The comparison is performed on the operands on both sides of the operator. The operands must be of the same type of values. They can be constant values or one or both can be the name(s) of (a) variable(s).

Boolean Operators

Boolean values use operators different from those of numeric values. The supported operators are:

Equality =: Two values are said to be equal if they have the same value. To perform this comparison, you can use the equality operator represented with =. It would be formulated as follows:

Value1 = Value2

If both values are the same, the comparison produces a true result. If the values are different, the comparison produces false.

Inequality <>: If you have two values and you want to find out whether they are different, you can use an operator as <> to compare the vales. This would be formulated as follows:

Value1 <> Value2

If the values are different, the comparison produces a true result. If the values are equal, the comparison produces false.

Less Than <: If you want to know whether one value is lower than another, to compare them, use the < operator. This would be formulated as follows:

Value1 < Value2

If the value on the left of the operator is lower than the value on the right side, the operation produces a true value. Otherwise, the result is false.

Less Than Or Equal To <=: If one value is lower than or equal to another, to compare them, use the <= operator. This would be formulated as follows:

Value1 <= Value2

If the left value is lower than the value on the right side or both values are the same, the comparison produces a true result. If the value on the left is greater than the other and they are not equal, the comparison produces false.

Greater Than >: To find out if one value is greater than another value, to compare them, use an operator as >. It can be used as follows:

Value1 > Value2

If the value on the left of the operator is greater than the value on the right side, the operation produces a true value. Otherwise, it produces a false result.

Greater Than Or Equal To >=: If one value is greater than or equal to another, to compare them, use an operator as >=. It can be used as follows:

Value1 >= Value2

If the value on the left side of the operator is greater than the value on the right side or if both values are equal, the comparison produces a result as true. If the value on the left is greater than the value on the right and the values are also different, the comparison produces a result as false.

     
 

Boolean Variables

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:

Boolean Operators

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:

Boolean Operators

   
   
 

Home Copyright © 2015, FunctionX Home