Related Tags

pascal

# What are relational operators in Pascal?

Kainat Asif

Relational operators are used to test the relation between two values. They compare two values in terms of equality or inequality.

## Syntax

Two variables are compared using relational operators, as shown below:

foo = variable1 _operator_ variable2;

• variable1: The first variable to be compared.
• variable2: The second variable to be compared.
• _operator_: The relational operator that will compare variable1 and variable2.
• foo: The return value of the comparison.

## Return value

The relational operators return a Boolean based on the result of the comparison:

• true is returned if the result of the comparison is true.
• false is returned if the result of the comparison is false.

## Relational operators

Relational operators in Pascal are as follows:

 Operator Symbol Description Example Equal to = Checks if the first variable is equal to the second variable. 5 = 5 (true)5 = 7 (false) Not equal to <> Checks if the first variable is not equal to the second variable. 1 <> 2 (true)2 <> 2 (false) Less than < Checks if the first variable is less than the second variable. 5 < 10 (true)20 < 15 (false) Greater than > Checks if the first variable is greater than the second variable. 3 > 1 (true)1 > 5 (false) Less than or equal to <= Checks if the first variable is less than or equal to the second variable. 1 <= 1 (true)9 <= 6 (false) Greater than or equal to >= Checks if the first variable is greater than or equal to the second variable. 5 >= 4 (true)4 >= 9 (false)

## Example

Consider the code snippet below, which demonstrates the use of relational operators:

program SetOperator;

var
num1: Integer = 5;
num2: Integer = 10;
foo: Boolean;

begin

//equal to
foo := num1 = num2;
writeln('5 = 10: ', foo);

//not equal to
foo := num1 <> num2;
writeln('5 <> 10: ', foo);

//less than
foo := num1 < num2;
writeln('5 < 10: ', foo);

//greater than
foo := num1 > num2;
writeln('5 > 10: ', foo);

//less than or equal to
foo := num1 <= num2;
writeln('5 <= 10: ', foo);

//greater than or equal to
foo := num1 >= num2;
writeln('5 >= 10: ', foo);

end.

## Explanation

Two variables, num1 and num2, are declared in lines 4-5. num1 and num2 are compared using the relational operators =, <>, <, >, <=, and >=, explained in the table above.

RELATED TAGS

pascal

CONTRIBUTOR

Kainat Asif