Learn Pascal
1E - Assignment and Operations


Once you have declared a variable, you can store values in it. This is called assignment.

To assign a value to a variable, follow this syntax:
     variable_name := expression;
Note that unlike other languages, whose assignment operator is simply an equals sign, Pascal uses a colon followed by an equals sign.

The expression can either be a single value:
     some_real := 385.385837;
or it can be an arithmetic sequence:
     some_real := 37573.5 * 37593 + 385.8 / 367.1;

The arithmetic operators in Pascal are:

Operator Operation Operands Result
+Addition or unary positive real or integerreal or integer
-Subtraction or unary negative real or integerreal or integer
*Multiplication real or integerreal or integer
/Real division real or integerreal
divInteger division integerinteger
modModulus (remainder division) integerinteger

div and mod only work on integers. / works on both reals and integers but will always yield a real answer. The other operations work on both reals and integers.

For operators that accept both reals and integers, the resulting data type will be integer only if all the operands are integer. It will be real if any of the operands are real.

Therefore,
     3857 + 68348 * 38 div 56834
will be integer, but
     38573 div 34739 mod 372 + 35730 - 38834 + 1.1
will be real because 1.1 is a real value.

Each variable can only be assigned a value that is of the same data type. Thus, you cannot assign a real value to an integer variable. However, certain data types are compatible with others. In these cases, you can assign a value of a lower data type to a variable of a higher data type. This is most often done when assigning integer values to real variables. Suppose you had this variable declaration section:

     var
        some_int : integer;
        some_real : real;
When the following block of statements executes,
     some_int := 375;
     some_real := some_int;
some_real will have a value of 375.0, or 3.75e2.

In Pascal, the minus sign can be used to make a value negative. The plus sign can also be used to make a value positive. This, however, is unnecessary because values default to being positive.

Do not attempt to use two operators side by side!
     some_real := 37.5 * -2;
This may make perfect sense to you, since you're trying to multiply by negative-2. However, Pascal will be confused -- it won't know whether to multiply or subtract. You can avoid this by using parentheses:
     some_real := 37.5 * (-2);
to make it clearer.

The computer follows an order of operations similar to the one that you follow when you do arithmetic:
     * / div mod
     + -

The computer looks at each expression according to these rules:

  1. Evaluate all expressions in parentheses, starting from the innermost set of parentheses and proceeding to the outermost.
  2. Evaluate all multiplication and division from left to right.
  3. Evaluate all addition and subtraction from left to right.

The value of
     3.5 * (2 + 3)
will be 17.5.

Pascal cannot perform standard arithmetic operations on Booleans. There is a special set of Boolean operations. Also, you should not perform standard operations on characters because the results may vary from compiler to compiler.


Previous lesson

Next lesson

Contents

Index

e-mail me


taoyue@mit.edu
Copyright © 1997-2001 Tao Yue. All rights reserved.