Home > General > Operators – an Introduction

Operators – an Introduction

This article is aimed at both those new to programming and merely new to Pascal’s syntax. If you are already familiar with the basic definition of a programming operator, please skip to the sub-heading: ‘Implementing Operators in Pascal’

What is an Operator?

Although you may not realise it, you have probably already been acquainted with operators for a number of years. An operator in mathematics (from which a computing operator is derived) is a ‘function which operates on or modifies another function’.

What is meant by this? Looking at it from a basic mathematical perspective, an operator performs on an operand to produce a result. The easiest way to demonstrate this is through an example:

Operand Operator Operand Result
1 + 2 3

The operator here indicates that the function of addition is to be performed with the operands ‘1’ and ‘2’, and a result of 3 is produced.

In programming, an operator does not have to be for arithmetic purposes. An operator can be an instruction to move data from one piece of memory to another, for example.

Implementing Operators in Pascal

Arithmetic Operators

As with any programming language, the most common and important operators within Pascal are arithmetic operators, as these are used to perform all calculations. A list basic of arithmetic operations in Pascal is provided below:

Operator Operation Operand Data Types Result Data Types E.g(result)
+ Addition Integer, real Integer, real 1 + 2 (3)
- Subtraction Integer, real Integer, real 2 – 1 (1)
* Multiplication Integer, real Integer, real 2 * 2 (4)
/ Division Real Real 10.5 / 2.0 (5.25)
DIV Integer division; Integer Integer 10 DIV 2 (5)
MOD Find remainder Integer Integer 11 MOD 2 (1)

Assignment

Assignment is an operation used to store a value in a variable. In Pascal the notation ‘:=’ is used. If, for example, we wanted to assign the value 10 to an integer variable called ‘Number’ we would use the following:

Number := 10;

The operator can also be an expression, for example:

Number := 10 + 4

Will result in the value 14 being assigned to the variable ‘Number’.

Comparison Operators

Comparison operators, as the name suggests, are used to perform a comparison between two operands:

Operator Operation Operand Data Types Result Data Types
< Less than Integer, real Integer, real
> Greater than Integer, real Integer, real
<= Less than or equal to Integer, real Integer, real
>= Greater than or equal to Integer, real Integer, real
= Equal to - -

Putting it all together

We can use all the operators provide to write a program designed to take two variables that have been input, add them together, and multiply this by another input, and if it is greater than or equal to 10 output the answer, otherwise state that it is less than 10:

program OperatorsProgram;

{$APPTYPE CONSOLE}

uses SysUtils;

var number1, number2, number3, Result : integer;

begin
       Writeln ('Please type in your first number');
       Readln (number1);
       Writeln ('Please type in your second number');
       Readln (number2);
       Writeln ('Please type in your multiplier');
       Readln (number3);

       Result := (number1 + number2) * number3;
       If Result >= 10
          then
                Writeln ('Your result is ' + IntToStr(Result))
          else
                Writeln ('Your result is less than 10');
       Readln;
end.

Try running the program in your Pascal compiler to see what happens!

© James Harland, 2009

Categories: General Tags: