RECORDS (Records/Structs) are programmer-defined data types that allow encapsulating related information under a single unit. Discover how to improve the readability and structure of your algorithms with this fundamental tool.
# Introduction to Records
Records are structures that group elements of different types (unlike arrays). They allow handling related data under a single identity, facilitating abstraction and the path to Object-Oriented Programming.
Structure of a Record:
RECORD record_name
START
data_type field1
data_type field2
END
# Example: Salary Calculation
We define a EmployeeRec record with ID, name, and amount. This allows accessing all employee
information through a single variable.
# Field Access
To access the fields, we use the dot operator
.:
# Array of Records
Each position in the array corresponds to a complete record.
# Practice Exercises
Register title, author, year, and color of 3 books.
RECORD Book
STRING title, author, color; INTEGER year
END_RECORD
FOR i = 0 TO 2
READ books[i].title
Define and load data for a product (code, description, price).
RECORD Product
INTEGER code; STRING desc; DECIMAL price
END_RECORD
Management of availability and settlement of 50 rooms.
IF (hotel[h].status == "Available") THEN
PRINT hotel[h].number
Load 50 employees associating branches and pre-defined areas.
RECORD Employee
Branch branch
Area area
END_RECORD
In the next article we will see recursion, an important topic for dealing with problems of a recursive nature.