0% found this document useful (0 votes)
39 views8 pages

F# Operators: Arithmetic, Comparison, Boolean

The document provides an overview of operators in F#, including arithmetic, comparison, boolean, and bitwise operators, along with examples of their usage. It explains how each operator functions with sample code snippets and expected outputs. The document serves as a reference for understanding the various operators available in the F# programming language.

Uploaded by

gobobi8535
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views8 pages

F# Operators: Arithmetic, Comparison, Boolean

The document provides an overview of operators in F#, including arithmetic, comparison, boolean, and bitwise operators, along with examples of their usage. It explains how each operator functions with sample code snippets and expected outputs. The document serves as a reference for understanding the various operators available in the F# programming language.

Uploaded by

gobobi8535
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

7.

F# – Operators F#

An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. F# is rich in built-in operators and provides the following types of operators:

 Arithmetic Operators

 Comparison Operators

 Boolean Operators

 Bitwise Operators

Arithmetic Operators
The following table shows all the arithmetic operators supported by F# language. Assume
variable A holds 10 and variable B holds 20 then:

Operator Description Example

+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiplies both operands A * B will give 200

/ Divides numerator by de-numerator B / A will give 2

Modulus Operator and remainder of after an


% B % A will give 0
integer division

Exponentiation Operator, raises an operand


** B**A will give 2010
to the power of another

Example

let a : int32 = 21
let b : int32 = 10
let mutable c = a + b
printfn "Line 1 - Value of c is %d" c
c <- a - b;
printfn "Line 2 - Value of c is %d" c

32
F#

c <- a * b;
printfn "Line 3 - Value of c is %d" c
c <- a / b;
printfn "Line 4 - Value of c is %d" c
c <- a % b;
printfn "Line 5 - Value of c is %d" c

When you compile and execute the program, it yields the following output:

Line 1 - Value of c is 31
Line 2 - Value of c is 11
Line 3 - Value of c is 210
Line 4 - Value of c is 2
Line 5 - Value of c is 1

Comparison Operators
The following table shows all the comparison operators supported by F# language. These
binary comparison operators are available for integral and floating-point types. These
operators return values of type bool.

Assume variable A holds 10 and variable B holds 20, then:

Operator Description Example

Checks if the values of two operands are equal or (A == B) is not


=
not, if yes then condition becomes true. true.

Checks if the values of two operands are equal or


<> not, if values are not equal then condition becomes (A <> B) is true.
true.

Checks if the value of left operand is greater than the


> value of right operand, if yes then condition becomes (A > B) is not true.
true.

Checks if the value of left operand is less than the


< value of right operand, if yes then condition becomes (A < B) is true.
true.

33
F#

Checks if the value of left operand is greater than or


(A >= B) is not
>= equal to the value of right operand, if yes then
true.
condition becomes true.

Checks if the value of left operand is less than or


<= equal to the value of right operand, if yes then (A <= B) is true.
condition becomes true.

Example

let mutable a : int32 = 21


let mutable b : int32 = 10
if (a = b) then
printfn "Line 1 - a is equal to b"
else
printfn "Line 1 - a is not equal to b"

if (a < b) then
printfn "Line 2 - a is less than b"
else
printfn "Line 2 - a is not less than b"

if (a > b) then
printfn "Line 3 - a is greater than b"
else
printfn "Line 3 - a is not greater than b"

(* Lets change value of a and b *)


a <- 5
b <- 20
if (a <= b) then
printfn "Line 4 - a is either less than or equal to b"
else
printfn "Line4 - a is a is greater than b"

34
F#

When you compile and execute the program, it yields the following output:

Line 1 - a is not equal to b


Line 2 - a is not less than b
Line 3 - a is greater than b
Line 4 - a is either less than or equal to b

Boolean Operators
The following table shows all the Boolean operators supported by F# language. Assume
variable A holds true and variable B holds false, then:

Operator Description Example

Called Boolean AND operator. If both the operands


&& (A && B) is false.
are non-zero, then condition becomes true.

Called Boolean OR Operator. If any of the two


|| (A || B) is true.
operands is non-zero, then condition becomes true.

Called Boolean NOT Operator. Use to reverses the


not (A && B) is
not logical state of its operand. If a condition is true then
true.
Logical NOT operator will make false.

Example

let mutable a : bool = true;


let mutable b : bool = true;
if ( a && b ) then
printfn "Line 1 - Condition is true"
else
printfn "Line 1 - Condition is not true"
if ( a || b ) then
printfn "Line 2 - Condition is true"
else
printfn "Line 2 - Condition is not true"

35
F#

(* lets change the value of a *)


a <- false
if ( a && b ) then
printfn "Line 3 - Condition is true"
else
printfn "Line 3 - Condition is not true"
if ( a || b ) then
printfn "Line 4 - Condition is true"
else
printfn "Line 4 - Condition is not true"

When you compile and execute the program, it yields the following output:

Line 1 - Condition is true


Line 2 - Condition is true
Line 3 - Condition is not true
Line 4 - Condition is true

Bitwise Operators
Bitwise operators work on bits and perform bit-by-bit operation. The truth tables for &&&
(bitwise AND), ||| (bitwise OR), and ^^^ (bitwise exclusive OR) are as follows:

p q p &&& q p ||| q p ^^^ q

0 0 0 0 0

0 1 0 1 1

1 1 1 1 0

1 0 0 1 1

Assume if A = 60; and B = 13; now in binary format they will be as follows:

A = 0011 1100

B = 0000 1101

-----------------

36
F#

A&&&B = 0000 1100

A|||B = 0011 1101

A^^^B = 0011 0001

~~~A = 1100 0011

The Bitwise operators supported by F# language are listed in the following table. Assume
variable A holds 60 and variable B holds 13, then:

Operator Description Example

Binary AND Operator copies a bit to (A &&& B) will give 12, which is
&&&
the result if it exists in both operands. 0000 1100

Binary OR Operator copies a bit if it (A ||| B) will give 61, which is


|||
exists in either operand. 0011 1101

Binary XOR Operator copies the bit if it (A ^^^ B) will give 49, which
^^^
is set in one operand but not both. is 0011 0001

Binary Ones Complement Operator is (~~~A) will give -61, which is


~~~ unary and has the effect of 'flipping' 1100 0011 in 2's complement
bits. form.

Binary Left Shift Operator. The left


operands value is moved left by the A <<< 2 will give 240 which is
<<<
number of bits specified by the right 1111 0000
operand.

Binary Right Shift Operator. The left


operands value is moved right by the A >>> 2 will give 15 which is
>>>
number of bits specified by the right 0000 1111
operand.

Example

let a : int32 = 60 // 60 = 0011 1100


let b : int32 = 13 // 13 = 0000 1101
let mutable c : int32 = 0

c <- a &&& b // 12 = 0000 1100


printfn "Line 1 - Value of c is %d" c
37
F#

c <- a ||| b // 61 = 0011 1101


printfn "Line 2 - Value of c is %d" c

c <- a ^^^ b // 49 = 0011 0001


printfn "Line 3 - Value of c is %d" c

c <- ~~~a // -61 = 1100 0011


printfn "Line 4 - Value of c is %d" c

c <- a <<< 2 // 240 = 1111 0000


printfn "Line 5 - Value of c is %d" c

c <- a >>> 2 // 15 = 0000 1111


printfn "Line 6 - Value of c is %d" c

When you compile and execute the program, it yields the following output:

Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is 49
Line 5 - Value of c is 240
Line 6 - Value of c is 15

38
F#

End of ebook preview


If you liked what you saw…
Buy it from our store @ [Link]

39

You might also like