44 total
By the end of this topic, you should be able to:
understand why user-defined data types are necessary
define and use non-composite data types, including:
define and use composite data types, including:
choose and design an appropriate user-defined data type for a given problem
A user-defined data type is a data type created by the programmer. In simple words, it is a type that does not already exist as a built-in type like INTEGER, STRING, or BOOLEAN. The programmer makes it to suit the needs of the program.
Built-in data types are useful, but they are not always enough on their own. Many real problems need data to be organised in a more meaningful way. This is why user-defined data types are needed.
For example, a program may need to store:
Using only basic types for all of these can make programs messy and harder to understand. A user-defined type lets the programmer describe the data more clearly.
User-defined data types are necessary because they help a programmer match the data structure to the real problem.
They are useful for several reasons.
First, they make a program clearer. If a variable is declared as a type called TDays, it is immediately obvious that it stores a day of the week. This is clearer than using a plain integer and trying to remember that 1 means Monday, 2 means Tuesday, and so on.
Second, they make a program more organised. Related pieces of data can be grouped together instead of being stored in separate, unconnected variables.
Third, they can reduce errors. If a variable is allowed to store only one value from a fixed list, invalid values are less likely to be used.
Fourth, they make programs easier to design, read, and maintain. When the structure of the data matches the structure of the problem, the program becomes easier to understand.
So, user-defined types are not just extra features. They are important tools for writing correct and sensible programs.
A non-composite data type is a type that is defined without using other data types as parts inside it. In this topic, the two non-composite types you need to know are enumerated types and pointer types.
An enumerated data type is a type made by listing all possible values that a variable can take.
This is useful when there is a small, fixed set of valid values.
The general form is:
TYPE <identifier> = (value1, value2, value3, ...)
TYPE Tmonth = (January, February, March, April, May, June, July, August, September, October, November, December)
DECLARE thisMonth : Tmonth
DECLARE nextMonth : Tmonth
thisMonth ← January
nextMonth ← thisMonth + 1
Here, Tmonth is the user-defined type. A variable of this type can only store one of the named month values.
An enumerated type has named values. These values may look a bit like strings, but they are not strings. They are not written in quotation marks.
For example:
today ← Wednesday
is correct, but:
today ← "Wednesday"
would not be correct for an enumerated value.
The values also have an implied order. This means they are in a sequence. For example, Monday comes before Tuesday, and January comes before February.
Because of this order, values can sometimes be compared or moved through in sequence.
TYPE TDays = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday)
DECLARE Day : TDays
DECLARE Weekend : BOOLEAN
Day ← Saturday
Weekend ← TRUE IF Day > Friday
This works because the values have an order. Saturday and Sunday come after Friday in the list.
Use an enumerated type when:
For example, an enumerated type is a good choice for:
An enumerated type makes the program safer and clearer. Instead of storing random words or numbers, the variable can only hold one of the allowed values.
That means the programmer controls what is valid.
Sign in to view full notes