More Types - Pascal programming

Big Types in Pascal

So far, the only numeric types that we have discussed have been INTEGER, and REAL. These types describe numbers of a very common range. For example, values of type INTEGER include all whole numbers between –32,768 and 32,767 (represented in 16 bits). Variables of this INTEGER type are however insufficient for many purposes, such as counting the population in even a small town!

In order to be able to represent larger integer values in Pascal, another type, LONGINT, is available. This type uses 32 bits and covers the range going from –2,147,483,648 to 2,147,483,647. The usual Integer operations (Read, Write, +, -, *, DIV, MOD, etc.) apply to these long integers as well. Similarly, for numbers beyond the range of REAL numbers, –3.38*1038 to 3.38*1038, there is a DOUBLE data type that covers the range –1.79*10308 to 1.79*10308.

Even Bigger Types

If the extended type LONGINT is still insufficient for our needs, then it is possible to create even larger types. As an example, we will create a new type called ELINTEGER (Extra Long Integer), which will express integers of one hundred or even more digits. Later, we will create an entire Library, called ELinteger Lib, with various arithmetic operations on these very long integers. The operations will not be in their usual form (+, -, *, /), but rather in the form of procedures such as:

ELintegerAdd(I, J, K);

which adds the ELINTEGER I to ELINTEGER J, and produces another ELINTEGER K. The symbols for comparison (<, =, <=, etc.) of ELINTEGERs would also be in the form of procedures such as:

Equals(I, J), or IsLessThan(K, 2)

Smaller Types

Often, some variables need only take only a few values. For example, there are only two sexes (male, female), four directions (North, East, South and West) seven days of the week (Sunday, Monday, etc.) and twelve months of the year (January, February, etc.) In the older programming languages, these were often coded as small INTEGER values. For example, Sunday was assigned a value of 0, Monday was 1, Tuesday was 2, etc. and Saturday was assigned a 6. This led to more readable statements. For example, instead of writing

Pascal programs could still use such a mechanism, but it has problems. For example, we could define the following constants:

All of these are of the same numeric type, and so if an error is made by comparing two different types, for example:

IF Date = Male...

it cannot be detected by the system. This particular error of comparing Date with Male is also very difficult for humans to detect. The English language and its two meanings for “Date” could cause considerable confusion. Pascal, with its emphasis on type -checking, has an alternative mechanism that allows a programmer to create different types for each different kind of data being worked with. The next section shows how to create types for working with the above kinds of data, including SexType, DirectionType, and WeekDayType.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Pascal programming Topics