1 / 9

Complex data types

Complex data types. Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings, array, records and objects.

kelvin
Download Presentation

Complex data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Complex data types • Complex data types: a data type made of a complex of smaller pieces. Pascal has four very commonly used complex data types: strings, array, records and objects. • We have already covered strings, will cover arrays in the next few classes and will not cover records or objects.

  2. TYPE • The reserved word TYPE introduces new types in Pascal. One new type you can define is an array. • The format of an array type definition is:TYPE IntArray = ARRAY[1..5] OF integer;This definition would produce a new type ‘IntArray’ which would consist of an array of memory locations (5 of them) which store integers.

  3. TYPE IntArray = ARRAY[1..5] OF integer; • TYPE: tells compiler we are defining a new type • IntArray: The name of the new type (name must conform to rules for naming an identifier) • ARRAY: tells compiler the new type will be an array • [1..5]: 1 will be lowest subscript; 5 will be highest. Both numbers must be same ordinal type. (Called dimensioning an array) • OF integer: means the array will store values of type integer (an array can only store one type).

  4. Once a new type is defined, you can declare a variable to hold the type like you do for a simple type. For example:VAR MyArray, YourArray: IntArray;would create two new identifiers (MyArray and YourArray) both of type IntArray. Each identifier can now hold 5 integers.

  5. How do we access the values? • To access the first value of MyArray, we would write: MyArray[1] • This would be used as any other simple variable would be used. • The 1 between the brackets is called a subscript (or index).

  6. How do we access the values (con’t) • You can also use variables or complex expressions as subscripts.For Example: MyArray[X] or MyArray[X+2] or MyArray[MyArray[2]] • This makes the array data type very powerful

  7. Out-of-range error • An out-of-range error is when you try to access an element of an array that is outside the range indicated in the declaration. • When using arrays, the compiler may not pick up on an out-of-range error.

  8. Difference between TYPE and VAR • TYPE declares a new type that you can use in the VAR section. • VAR declares a new variable of a certain type. It can be a predefined type (integer, char, etc.) or a user defined type (MyArray)

  9. Placement of type definition • In Turbo Pascal, the TYPE definition can be placed before or after the CONST definition and the VAR declaration unless an item in one of these depends on a definition in the others.CONST MaxSubscr = 20;TYPE IntArray = ARRAY [1..MaxSubsc] OF integer;VAR MarkArray: IntArray;

More Related