Last Updated:

Basic operators of the Pascal language

Basic operators of the Pascal

Pascal is one of the programming languages, appeared in 1970. Despite its decent age, it is still used as an educational programming language. It became the progenitor for some modern languages.

Throughout its long history, the language has undergone a large number of changes. New languages and dialects appeared. For example, in '78, the UCSD Pascal language was developed. It contained a large number of additions. The most important thing is the emergence of p-code, which allowed you to quickly transfer the language compiler from one machine to another. Another implementation of Pascal, ObjectPascal, was performed by Apple in 1986. Its main difference is the presence of object-oriented programming.

The language continues to be maintained to this day. Pascal has already become the first programming language for many novice developers.

To successfully write programs in this language, you need to have a good understanding of Pascal's basic constructs – operators. An operator is a programming language sentence that ends with a semicolon. There are quite a few of them in Pascal. Among them are the assignment, comparison, loop, and so on.

Assignment statement

 

In mathematics, we are used to denoting this operator with the symbol equal, but in Pascal it is denoted differently: ":=". For example, a programmer wants to assign the number 5 to the variable X. Using mathematical symbols, this is written as "X=5". But in Pascal, the expression looks different: "X:=5".

In general, the assignment construct looks like this: <name of a variable>:=<new value of a variable>. To the right of the "assign" sign, it is allowed to write whole expressions, the value of which will be calculated when the program is executed, or other variables. For example, a programmer might write:

"X:=5*10/3""X:=5*Y", where Y is the other variable.

It is worth noting here that Pascal is a strongly typed language. This means that we cannot assign a fractional or Boolean value to an integer variable and vice versa. Therefore, it is important to ensure that X and Y are variables of the same type, otherwise an exception will occur.

Example:

Var x : integer;
Begin
X:=5/3;
End.

An exception will be thrown in the row where the value is assigned to X. Another example with an exception:

Varx : integer;
Y : real;
Begin
Y:=56.78;
X:=Y;
End.

The string in which the value is assigned to the game will run normally, but in the next one an error will occur. Also, novice Pascal programmers are faced with such a problem - when assigning a value to a variable, the program will crash.

Example:

Varx : integer;
Begin
X:=1000000;
Write(x);
End.

In this example, the variable X is set to 1 million, and the integer type can contain numbers ranging from -32768 to 32767. This limitation is due to the fact that the compiler allocates 2 bytes in the computer memory for the integer type. That is, there can be a total of 65536 different values.

Separately, it is worth mentioning the assignment of value to variables of the Boolean type. For example, in Pascal we might write like this: test := 10>1; where test is a Boolean variable. This sets the variable to true because the number 10 is greater than 1.

In addition to simple expressions, a programmer can assign a value to a variable that the function will return.

VarY : real;
Begin
Y:=sin(30)*10;
End.

The sin function will return some value, which will be multiplied by 10. The result of the calculation will be written to the variable Y.

Conditional

 

For a complete picture of the operators, it is worth considering the conditional operator, without which no program can do. In general terms, the pascal condition is written as follows:

if<conditioned expression>then< action>
else< action>

Any condition in the pascal consists of three blocks. A conditional expression refers to any expression that can be true or false. For example, 10=5 is a conditional expression whose result is false, and the expression x:=10 is not conditional.

The second block is the action to be performed if the condition is true. The last block is the else block. It is not mandatory, so if your program does not imply actions in case of a false outcome, you do not need to write a block else.

Example of a condition:

program example;
var x: integer;
begin
x:=4*7;
if (x>=7)and(x<=100) then writeln('x is in range')
else writeln ('X is not in the range');
end.

The result of the program: X is included in the range.

In this program, we determine whether the number 4* 7 is in the range from 7 to 100. To do this, you need a conditional if statement. After it there are two conditions, which are combined by the operator and. And indicates that the condition in the first and second brackets must be met.

 

The word then denotes the end of the condition, followed by commands. If the condition is met and its result is true, the writeln command ('X enters range') will be executed. Note that writeln() is not followed by a semicolon. A semicolon is needed only when there is no block else.

 

The next line is else. It denotes the logic that holds if the condition is false. In this case, a string is displayed.

 

Other spellings of the condition in Pascal are possible. For example, if you need to execute more than one command, then after then and else the operator brackets begin and end are placed.

if<conditioned expression>thenbegin
<activity 1>;
<activity 2>
end
else begin
<activity 3>
<activity 4>
end;

Note that the first statement brackets are not followed by a semicolon. There may be other options:

if<conditioned expression>thenbegin
<activity 1>;
<activity 2>
end
else<activity 3>

In the else block, statement brackets are not needed because the block contains only one action.

Comparison operator

 

Another operator that inexperienced programmers often confuse with assignment is the comparison operator. It looks like a regular sign equal to "=". If in mathematical records it is used to denote equality, then in Pascal programming the operator denotes comparison. For example, the expression a = b will be read as "a equals b?". The comparison operator is used primarily in conditional constructs.

Example of use:

Varx, y : integer;
Begin
X:=5;
Y:=10;
If x = y then write("X equals igrek")else write("X is not equal to igrek");
End.

The result of the program: X is not equal to the game.

For successful programming, you just need to be able to work with operators. One of the most common in the Pascal language is the assignment operator ":=". Do not confuse it with the condition operator "=", used in conditional constructions.