While simple variables hold one piece of data, Arrays and Records allow you to structure and store related pieces of data efficiently, a crucial step toward complex programming.
1. Arrays (Ordered Lists)
An Array is a fixed-size, ordered collection of elements of the same data type. Arrays are accessed using an index (usually an integer).
A. Declaration
Array types must be declared in the type section first, although you can declare them directly in the var section. Pascal arrays use square brackets [ ] to define the index range.
program ArrayDemo;
type
// Declare a type for an array of 50 integers, indexed from 1 to 50
TScoreArray = array[1..50] of Integer;
var
// 1. Declare variables using the custom type
TestScores: TScoreArray;
// 2. Direct declaration of a variable array (indexed from 0 to 9)
WeekDays: array[0..9] of String;
B. Accessing and Manipulating Arrays
You access or modify individual elements using the index number.
var
i: Integer;
begin
// Set values
TestScores[1] := 95;
TestScores[2] := 88;
WeekDays[0] := 'Monday';
// Get values
Writeln('First Score: ', TestScores[1]);
// Arrays are often processed using a 'for' loop
for i := 1 to 50 do
begin
TestScores[i] := i * 2; // Assigns values 2, 4, 6, ...
end;
Writeln('Last Score: ', TestScores[50]); // Should print 100
end.
Index Bounds: It is a common mistake to try accessing an index outside the defined range (e.g., accessing
WeekDays[10]). This is called an “index out of bounds” error and will lead to unpredictable behavior or a runtime crash.
C. Multi-Dimensional Arrays
Arrays can have multiple dimensions, useful for representing grids, matrices, or tables (rows and columns).
type
// An array representing a 10x5 grid of characters
TGrid = array[1..10, 1..5] of Char;
var
GameGrid: TGrid;
begin
// Accessing an element at row 3, column 4
GameGrid[3, 4] := 'X';
end.
2. Records (Custom Structures)
A Record is a user-defined data structure that groups together a fixed number of elements (called fields) that may be of different data types. This is analogous to a “struct” in C/C++ or an object without methods.
A. Declaration
Records must be defined in the type section.
program RecordDemo;
type
TDate = record
Day: Integer;
Month: Integer;
Year: Integer;
end;
TPerson = record
Name: String;
Age: Integer;
Birthday: TDate; // Records can contain other records
end;
var
Student: TPerson; // Declare a variable of the TPerson record type
B. Accessing Record Fields
You access fields using the dot operator (.).
begin
// Accessing and setting fields directly
Student.Name := 'Helen';
Student.Age := 22;
// Accessing a nested record field
Student.Birthday.Day := 15;
Student.Birthday.Month := 10;
Student.Birthday.Year := 2003;
Writeln('Name: ', Student.Name, ' (Age: ', Student.Age, ')');
end.
C. The with Statement
Accessing nested fields can become verbose. The with statement provides a shorthand way to access the fields of a record (or object) without repeating the record name.
// Example without 'with'
// Student.Birthday.Day := 1;
// Student.Birthday.Month := 1;
// Example with 'with' (Cleaner)
with Student.Birthday do
begin
Day := 1; // Automatically refers to Student.Birthday.Day
Month := 1; // Automatically refers to Student.Birthday.Month
Year := 2000;
end;
withCaution: Usewithjudiciously. If you have variables with the same name as record fields, thewithstatement prioritizes the field name, which can lead to ambiguity and hard-to-find bugs.
