Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Syntax of the RECORD declaration

TYPE identifier = RECORD list of fields END;
Here is a code fragment, as an example.

TYPE FILE_CARD = RECORD name : STRING[30]; address : STRING[50]; phone_number : STRING[15]; age : INTEGER; birthday : STRING[20]; END;


Note:- Like CONST, and other type definitions there is an equals sign not a colon, used with the RECORD. Also I have used a string for the phone number because something like (031) 231-4635 would not be accepted as an INTEGER for , hopefully by now, obvious reasons. The indentation in the RECORD structure is not required, though it is pretty standard, and helps with the program's readability. Also, the fields phone_number and age do not need to be of type INTEGER since no calculations are planned in the program. They could have been declared as type string. Once this new type is declared, we need to let the program know that we would like to use this new type so we must include this request in the variable list.

VAR address_book_entry : FILE_CARD;


It is a good idea to think of the ':', (colon) as reading is a so that the line,

address_book_entry : FILE_CARD;


would read, address_book_entry is a FILE_CARD.
Lets go back to the compartmentalised box analogy of the type RECORD. In the data structure, each compartment must be referred to separately. Just as the type STRING has the subscript method, the type RECORD has a way of reaching each individual compartment. This is done by using the identifier. The character '.' means that a fieldname is to follow. For example: to output the fields name and address in the data-structure, would require the lines

WRITELN(address_book.name); WRITELN(address_book.address);


In the next program, you can see the use of the assignment operator ':=' to assign values to each field, and the use of the identifier.fieldname method to access the required field.

Program 9

PROGRAM record_demo(INPUT_OUTPUT); { a program demonstrating the use of the type record } TYPE A_NAME = STRING[20]; AN_ADDRESS = STRING[50]; A_PHONE_NUMBER = STRING[15]; A_BIRTHDAY = STRING[20]; FILE_CARD = RECORD name : A_NAME; address : AN_ADDRESS; phone_number : A_PHONE_NUMBER; age : INTEGER; birthday : A_BIRTHDAY; END; VAR address_book_entry_one : FILE_CARD; address_book_entry_two : FILE_CARD; BEGIN address_book_entry_one.name := 'Smith, John'; address_book_entry_one.address := '42 Hill St, Edinburgh'; address_book_entry_one.phone_number := '231-4635'; address_book_entry_one.age := 30; address_book_entry_one.birthday := '14th March, 1964'; address_book_entry_two.name := 'Jones, Henry'; address_book_entry_two.address := '37 Park Cres, Edinburgh'; address_book_entry_two.phone_number := '421-7640'; address_book_entry_two.age := 32; address_book_entry_two.birthday := '14th March, 1962'; WRITELN('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); WRITELN('Name : ',address_book_entry_one.name); WRITELN('Address : ',address_book_entry_one.address); WRITELN('Phone Number : ',address_book_entry_one.phone_number); WRITELN('Age : ',address_book_entry_one.age); WRITELN('Birthday : ',address_book_entry_one.birthday); WRITELN('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'); WRITELN('Name : ',address_book_entry_two.name); WRITELN('Address : ',address_book_entry_two.address); WRITELN('Phone Number : ',address_book_entry_two.phone_number); WRITELN('Age : ',address_book_entry_two.age); WRITELN('Birthday : ',address_book_entry_two.birthday); END. { program record_demo }

Date: 2016-03-03; view: 612


<== previous page | next page ==>
NOT a boolean operator | Subranges of enumerated types
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.007 sec.)