Despite various advantages and disadvantages of Pascal, it's undeniable that this language is fundamental for any programmer. Pascal's data types define the meaning, constraints, possible values, operators, functions, and methods associated with them.
Integer (integer), Real (real type), Boolean, and Character (character type) are referred to as standard data types. Data types in Pascal can be classified as scalar types, pointer types, and structured data types.
Examples of scalar data types in Pascal include integer (integer type), real (real type), Boolean, character (character type), subrange (subrange type), and enumerate (enumerated type). Structured data types are created from scalar data types, such as array, record, file, and set. Pointer data type will be discussed later.
Data types in Pascal
Data types in Pascal are summarized in the diagram below:
1. Type declaration
Type declaration is used to declare the data type of an identifier. The syntax for type declaration:
type-identifier-1, type-identifier-2 = type-specifier;
The following declaration example defines variables for date and age as integer type, yes and true as Boolean type, name and city as string type, fees and expenses as real type:
type
days, age = integer;
yes, true = boolean;
name, city = string;
fees, expenses = real;
Integer Type (integer type)
The table below details standard integer types with storage size and value ranges used in Object Pascal:
2. Constants
Constants are used so that the program can read more and have values that do not change throughout the program execution. Pascal allows constants to be numbers, Boolean strings, strings, and characters.
Constants can be declared in the declaration part of the program by specifying the const declaration.
Syntax for declaring function types:
const
Identifier = constant_value;
Below are examples of constant declarations:
VELOCITY_LIGHT = 3.0E=10;
PIE = 3.141592;
NAME = 'Stuart Little';
CHOICE = yes;
OPERATOR = '+';
All constant declarations must precede variable declarations.
3. Enumerated Data Type
Enumerated data type is user-defined data type. This type allows values to be specified in a list. Only assignment operators and relational operators are allowed on enumerated data types. Enumerated data type is declared as follows:
type
enum-identifier = (item1, item2, item3, ... )
Below is an example of enumerated type declaration:
type
SUMMER = (April, May, June, July, September);
COLORS = (Red, Green, Blue, Yellow, Magenta, Cyan, Black, White);
TRANSPORT = (Bus, Train, Airplane, Ship);
In the example above, the items listed within the domain of an enumerated type determine the order of the items. For instance, in the enumerated data type SUMMER, April comes before May, May comes before June, … . The domain of enumerated type identifiers cannot include constants or character constants.
4. Subrange Data Type
Subrange data type allows a variable to assume values within a certain range. For example, if the age of voters ranges from 18 to 100, the variable representing age is:
var
age: 18 ... 100;
In the following sections, Mytour will introduce you in detail on how to declare variables. Additionally, you can also specify subrange declaration types by using type declarations. Syntax to classify declaration types:
type
subrange-identifier = lower-limit ... upper-limit;
Below are some examples of subrange declaration types:
const
P = 18;
Q = 90;
type
Number = 1 ... 100;
Value = P ... Q;
Subrange declaration types can be created from a subset of an enumerated data type already defined.
For example:
type
months = (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);
Summer = Apr ... Aug;
Winter = Oct ... Dec;
In summary, data types in Pascal are classified as scalar types, pointer data types, and structured data types. In the upcoming articles, Mytour will delve into variables and how to declare them in Pascal.