Assignments and Operators |
4 |
![]() |
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
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.
Table 4-2 Fixed- and Variable-Length String Assignments
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
String Constants
When assigning a constant string to a packed array of char, standard Pascal requires that the strings be the same size.
Operators
Pascal supplies six classes of operators:
Arithmetic Operators
Table 4-4
The mod Operator
Pascal extends the standard definition of the mod operator as follows.i mod j
equals:-1 * remainder of |i| divided by |j|
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.
~
bitwise not
integer
integer
&
bitwise and
integer
integer
|
bitwise or
integer
integer
!
bitwise or (same as |)
integer
integer
Table 4-5 Bit Operators
Operator
Operation
Operands
Result
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
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 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.
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
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
Relational Operators on Sets
Use the relational operators to compare sets of identical types. The result is a boolean (true or false) value.
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:
Comparing Records (Screen 1 of 2)
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 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.
~, not,
Highest
*, /, div, mod, and, &,
.
|, !, +, -, or,
.
=, <>, <, <=, >, >=, in,
.
or else, and then
Lowest
Table 4-9 Precedence of Operators
Operators
Precedence