Pascal - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Pascal Operators

Pascal Operators

shape Description

Generally Operator is a symbol that tells the compiler to perform mathematical or logical operation on the variables or a value. The pascal language has rich set of built-in operators. The following are the operators of pascal language.

Arithmetic operators

shape Description

Arithmetic operators performs mathematical calculations like addition, subtraction, multiplication, division, and modulus.
Operators Operation
+ Addition
- Subtraction
* Multiplication
Mod Remainder
/ Division
Div Integer Division
The following is an example. SPlessoncalculator.pas [c] program SPlessoncalculator; var a,b,c : integer; d: real; begin a:=16; b:=11; c := a + b; writeln(' Value of c is ', c ); c := a - b; writeln('Value of c is ', c ); c := a * b; writeln(' Value of c is ', c ); d := a / b; writeln('Value of d is ', d:3:2 ); c := a mod b; writeln('Value of c is ' , c ); c := a div b; writeln('Value of c is ', c ); end. [/c] Output: Now compile the code result will be as follows. [c]Value of c is 27 Value of c is 5 Value of c is 176 Value of d is 1.45 Value of c is 5 Value of c is 1[/c]

Boolean operators

shape Description

The Boolean operators are used to give the result as true or false.
Operators Operation
or Logical or
xor Logical xor
and Logical and
not Logical negation
The following is an example. [c]program beLogical; var a, b: boolean; begin a := true; b := false; if (a and b) then writeln('Line 1 - Condition is true' ) else writeln('Line 1 - Condition is not true'); if (a or b) then writeln('Line 2 - Condition is true' ); (* lets change the value of a and b *) a := false; b := true; if (a and b) then writeln('Line 3 - Condition is true' ) else writeln('Line 3 - Condition is not true' ); if not (a and b) then writeln('Line 4 - Condition is true' ); end.[/c] Output:Now compile the code result will be as follows. [c]Line 1 - Condition is not true Line 2 - Condition is true Line 3 - Condition is not true Line 4 - Condition is true[/c]

Logical operators

shape Description

Logical operators performs logical operations on given expressions. The following are the various operations.
Operators Operation
or Bitwise or
xor Bitwise xor
and Bitwise and
not Bitwise negation
<< Bitwise shift to the left
>> Bitwise shift to the right

Relational operators

shape Description

Relational operators can be used to compare two variables. The following are the various operators.
Operators Operation
= Equal
< Stricty less than
> Stricty greater than
Not equal
<= Less than or equal
>= Greater than or equal
in Elements of
The following is an example. [c]program relational; var a, b: integer; begin a := 20; b := 11; if a = b then writeln('Line 1 - a is equal to b' ) else writeln('Line 1 - a is not equal to b' ); if a < b then writeln('Line 2 - a is less than b' ) else writeln('Line 2 - a is not less than b' ); if a > b then writeln('Line 3 - a is greater than b' ) else writeln('Line 3 - a is greater than b' ); (* Lets change value of a and b *) a := 5; b := 20; if a <= b then writeln('Line 4 - a is either less than or equal to b' ); if ( b >= a ) then writeln('Line 5 - b is either greater than or equal to ' ); end.[/c] Output:Now compile the code result will be as follows. [c]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 Line 5 - b is either greater than or equal to [/c]

String operators

In pascal language there is only one string operator that is + used to concatenate two strings values.

Set operators

shape Description

The following are the set operations that are useful widely in pascal.
Operators Operation
* Intersection
+ Union
- Difference
<= Contains
in To check an element is in a set or not
include To include an element.
exclude To exclude an element
The following is an example. [c]program SetOpDemo; type TCharSet = set of Char; var S1, S2, S3: TCharSet; Result: Boolean; begin S1 := ['a', 'b', 'c']; S2 := ['c', 'd', 'e']; S3 := S1 + S2; { S3 = ['a', 'b', 'c', 'd', 'e'] } S3 := S1 * S2; { S3 = ['c'] } S3 := S1 - S2; { S3 = ['a', 'b'] } S3 := S1 >< S2; { S3 = ['a', 'b', 'd', 'e'] } S1 := ['c', 'd', 'e']; Result := S1 = S2; { False } Result := S1 < S2; { False } Result := S1 <= S2; { True } S1 := ['c', 'd']; Result := S1 <> S2; { True } Result := S2 > S1; { True } Result := S2 >= S1 { True } end.[/c]

Summary

shape Key Points

  • The string is nothing but a sequence of characters.
  • The string will have only one operator + that used to concatenate.
  • The in is the set operator that is used check whether an element is existed or not.