Previous Next Contents Index Doc Set Home


Assignments and Operators

4


This chapter describes the different types of assignments and operators in Pascal. It contains the following sections:

Data Type Assignments and Compatibility

page 63

String Assignments

page 64

Operators

page 66

Precedence of Operators

page 76


Data Type Assignments and Compatibility

Table 4-1 lists the assignment compatibility rules for real, integer, boolean, character, enumerated, subrange, record, set, and pointer data types.




Table  4-1 Data Type Assignment

Type of Variable/Parameter
Type of Assignment-Compatible Expression

real, single, shortreal

real, single, shortreal, double, longreal, any integer type

double, longreal

real, single, shortreal, double, longreal, any integer type

integer, integer16,integer32

integer, integer16,integer32

boolean

boolean

char

char

enumerated

Same enumerated type

subrange

Base type of the subrange

record

Record of the same type

array

Array with the same type

set

Set with compatible base type

pointer

Pointer to an identical type, univ_ptr

Pascal implicitly converts the integer to the real type, if necessary.


String Assignments

Pascal has special rules for assigning fixed- and variable-length strings, null strings, and string constants.

Fixed- and Variable-Length Strings

When you make an assignment to a fixed-length string, and the source string is shorter than the destination string, the compiler pads the destination string with blanks. If the source string is larger than the destination string, the compiler truncates the source string to fit the destination.

When you make an assignment to a variable-length string, and the source string is longer than the destination string, the compiler truncates the source to fit the destination.

The valid fixed- and variable-length string assignments are given in Table 4-2.




Table  4-2 Fixed- and Variable-Length String Assignments

Type of String
Type of Assignment-Compatible Expression

array of char

varying string, constant string, and array of char if the arrays have the same length

varying

varying string, constant string, array of char, and char

Null Strings

Pascal treats null strings as constant strings of length zero. Table 4-3 shows the null string assignments.




Table  4-3 Null String Assignments

Assignment
Description

varying := '';

The compiler assigns the null string to the variable-length string. The length of the variable-length string equals zero.

array of char := '';

The compiler assigns a string of blanks to the character array. The length of the resulting string is the number of elements in the source character array.

char := '';

It is illegal to assign a null string to a char variable. Use chr(0) instead.

String concatenation

In a string concatenation expression such as:
S := 'hello' + '' + S;

'' is treated as the additive identity (as nothing).

String Constants

When assigning a constant string to a packed array of char, standard Pascal requires that the strings be the same size.

Pascal allows the constant string and packed array of char to be unequal in size, truncating the constant string if it is longer or padding it with blanks if it is shorter.


Operators

Pascal supplies six classes of operators:

Arithmetic Operators

The arithmetic operators are summarized in Table 4-4.




Table  4-4

Operator
Operation
Operands
Result

+

addition

integer or real

integer or real

-

subtraction

integer or real

integer or real

*

multiplication

integer or real

integer or real

/

division

integer or real

real

div

truncated division

integer

integer

mod

modulo

integer

integer

Arithmetic Operators

The mod Operator

Pascal extends the standard definition of the mod operator as follows.

In the expression i mod j, when i is positive, Pascal and standard Pascal produce the same results. However, when i is negative, and you do not compile your program with a standard option (-s, -s0, -s1, -V0, or -V1), the following is true:

i mod j
equals:

-1 * remainder of |i| divided by |j|

The Pascal program, mod.p, which computes i mod j

program modexample(output);

{ This program demonstrates the nonstandard
  mod function. }

var
    i: integer;
    j: integer;

begin
    for i := -3 to -1 do 
      for j := 1 to 3 do 
          if j <> 0 then 
              writeln(i: 4, j: 4, i mod j: 4)
end. { mod_example }

The commands to compile and execute mod.p without any options

hostname% pc mod.p
hostname% a.out
  -3   1   0
  -3   2  -1
  -3   3   0
  -2   1   0
  -2   2   0
  -2   3  -2
  -1   1   0
  -1   2  -1
  -1   3  -1

The results negative i produces when you compile mod.p with the -s option

hostname% pc -s mod.p
hostname% a.out
  -3   1   0
  -3   2   1
  -3   3   0
  -2   1   0
  -2   2   0
  -2   3   1
  -1   1   0
  -1   2   1
  -1   3   2

Bit Operators

Table 4-5 shows the bit operators. The ~ operator produces the same results as the built-in Pascal function, lnot. Similarly, & is equivalent to the function, land; | and ! are equivalent to lor. See Chapter 7, "Input and Output," for descriptions of these functions and the truth tables that both the functions and the operators use.




Table  4-5 Bit Operators

Operator
Operation
Operands
Result

~

bitwise not

integer

integer

&

bitwise and

integer

integer

|

bitwise or

integer

integer

!

bitwise or (same as |)

integer

integer

boolean Operators

The boolean operators, which include the nonstandard and then and or else operators, are summarized in Table 4-6.




Table  4-6 boolean Operators

Operator
Operation
Operands
Result

and

Conjunction

boolean

boolean

and then

Similar to boolean and

boolean

boolean

not

Negation

boolean

boolean

or

Disjunction

boolean

boolean

or else

Similar to boolean or

boolean

boolean

The and then Operator

The and then operator differs from the standard and operator in that it guarantees the order in which the compiler evaluates the logical expression. Left to right and the right operands are evaluated only when necessary. For example, when you write the following syntax, the compiler may evaluate odd(y) before it evaluates odd(x):

odd(x) and odd(y)
However, when you use the following syntax, the compiler always evaluates odd(x) first:

odd(x) and then odd(y)
If odd(x) is false, odd(y) is not evaluated.


Note - You cannot insert comments between the and and the then operators.

The Pascal program, and_then.p, which uses and then to test if two numbers are odd

program and_then(input, output);

{ This program demonstrates the use
  of the operator and then. }

var
    x, y: integer16;

begin
    write('Please enter two integers:  ');
    readln(x, y);
    if odd(x) and then odd(y) then 
      writeln('Both numbers are odd.')
    else 
      writeln('Both numbers are not odd.');
end. { and_then }

The commands to compile and execute and_then.p. This example shows the output when you input the numbers 45 and 6.

hostname% pc and_then.p
hostname% a.out
Please enter two integers:  45 6
Both numbers are not odd.

The or else Operator

The or else operator is similar to the and then operator. In the following expression, the compiler evaluates odd(x) first, and if the result is true, does not evaluate odd(y):

odd(x) or else odd(y)

Note - You cannot insert comments between the or and the else operators.

The Pascal program, or_else.p, which uses
or else to test if two numbers are less than 10.

program or_else(input, output);

{ This program demonstrates the use
  of the operator or else. }

var
    x, y: integer16;

begin
    write('Please enter two integers:  ');
    readln(x, y);
    if (x < 10) or else (y < 10) then 
      writeln('At least one number is less than 10.')
    else
      writeln('Both numbers are greater than or equal to 10.');
end. { or_else }

The commands to compile and execute or_else.p. This example shows the output when you input the numbers 101 and 3.

hostname% pc or_else.p
hostname% a.out
Please enter two integers: 101 3
At least one number is less than 10.

Set Operators

The set operators in Table 4-7 accept different set types as long as the base types are compatible. The relational operators can also be used to compare
set-type values.




Table  4-7 Set Operators

Operator
Operation
Operands
Result

+

Set union

Any set type

Same as operands

-

Set difference

Any set type

Same as operands

*

Set intersection

Any set type

Same as operands

in

Member of a specified set

2nd arg:any set type
1st arg:base type of 2nd arg

boolean

Relational Operators

The relational operators are given in Table 4-8. In Pascal, you can apply all relational operators to sets and the equality (=) and inequality (<>) operators on records and arrays.




Table  4-8 Relational Operators

Operator
Operation
Operand
Results

=

Equal

Any real, integer, boolean, char, record, array, set, or pointer type

boolean

<>

Not equal

Any real, integer, boolean, char, record, array, set, or pointer type

boolean

<

Less than

Any real, integer, boolean, char, string, or set type

boolean

<=

Less than or equal

Any real, integer, boolean, char, string, or set type

boolean

>

Greater than

Any real, integer, boolean, char, string, or set type

boolean

>=

Greater than or equal

Any real, integer, boolean, char, string, or set type

boolean

Relational Operators on Sets

Use the relational operators to compare sets of identical types. The result is a boolean (true or false) value.

The Pascal program, sets.p, which applies the < and > operators to two sets of colors. The < operator tests if a set is a subset of another set. The > operator tests if a set is a proper subset of another set.

program set_example(output);

{ This program demonstrates the use of relational
  operators on sets. }

var
    set1, set2: set of (red, orange, yellow, green);

begin
    set1 := [orange, yellow];
    set2 := [red, orange, yellow];
    writeln(set1 > set2);
    writeln(set1 < set2)
end. { set_example }

The commands to compile and execute sets.p

hostname% pc sets.p
hostname% a.out
false
true

The = and <> Operators on Records and Arrays

Use the = and <> operators to compare character arrays of the same size. For example:

In making comparisons, between arrays and records, make sure the operands are of the same type.

The Pascal program, compare.p, which makes comparisons among records

program record_example(output);

const
    MAX = 10;

type
    Shape = (Square, Trapezoid, Rectangle);
    variant_record = 
      record 
          case Shape_type: Shape of
              Square: ( side1: real );
              Trapezoid: ( top1: real;
                           bottom: real;
                           height: real );
              Rectangle: ( length: real;
                           width: real )
      end;

    normal_record = 
      record 
          name: array [1..MAX] of char;
          avg: integer;
          grade: char
      end;

var
    class1: normal_record := ['Susan', 100];
    class2: normal_record := ['John', 99];
    shapes1: variant_record;
    shapes2: variant_record;

Comparing Records (Screen 1 of 2)

 begin
    { Should PASS. }
    if class1 <> class2 then 
      writeln('PASSED')
    else 
      writeln('FAIL');

    shapes1.Shape_type := Rectangle;
    shapes2.Shape_type := Square;
    { Should PASS }
    if shapes1 = shapes2 then 
	 writeln('FAIL')
    else
      writeln('PASSED');

    shapes1.Shape_type := Trapezoid;
    shapes2.Shape_type := Trapezoid;

    { Should PASS. }
    if shapes1 = shapes2 then 
      writeln('PASSED')
    else 
      writeln('FAIL')
end. { record_example }

Comparing Records (Screen 2 of 2)

The commands to compile and execute compare.p

hostname% pc compare.p
hostname% a.out
PASSED
PASSED
PASSED

String Operators

With the string concatenation operator, the plus sign (+), you can concatenate any combination of varying, array of char, constant strings, and single characters.

The Pascal program, concate.p, which concatenates four types of strings

program string_example(output);

{ This program demonstrates the use of
  the string concatenation operator. }

var
    col: varying [10] of char := 'yellow';
    fish: array [1..4] of char := 'tail';
    n1: char := 'o';
    n2: char := 'r';

begin
    write(fish + n1 + n2 + 'bird ', col + 'bird ');
    writeln(col + fish)
end. { string_example }

The commands to compile and execute concate.p

hostname% pc concate.p
hostname% a.out
tailorbird yellowbird yellowtail


Precedence of Operators

Table 4-9 lists the order of precedence of Pascal operators, from the highest to the lowest.




Table  4-9 Precedence of Operators

Operators
Precedence

~, not,

Highest

*, /, div, mod, and, &,

.

|, !, +, -, or,

.

=, <>, <, <=, >, >=, in,

.

or else, and then

Lowest


Previous Next Contents Index Doc Set Home