ประเภทข้อมูลและเรคอร์ด
แนะนำใหม่| English | ความหมาย |
|---|---|
| data type/ˈdeɪtə taɪp/ | ชนิดข้อมูล |
| field/fiːld/ | ฟิลด์ |
| record/ˈrekɔːd/ | บันทึก |
| record structure/ˈrekɔːd ˈstrʌktʃə/ | โครงสร้างบันทึก |
| dot notation/dɒt nəʊˈteɪʃn/ | Programming dot notation (การระบุสมาชิกด้วยจุด) |
The most expensive data-type decision in history
- In the 1960s memory cost more than programmers. To save two characters per date, years were stored as two digits:
99meant 1999. - Nobody expected the code to outlive the century. It did, and on 1 January 2000 every one of those fields would read as 1900. Governments and companies spent an estimated $300 billion checking and fixing it.
- The bug was not a typo. It was a data type 数据类型 chosen for the memory of 1965 and kept for the dates of 2000.
- This lesson is how to pick the type that fits the data, and how to bundle values that belong together into a record 记录.
The six basic types
| Type | Holds | Use it for |
|---|---|---|
INTEGER |
a whole number, 42, -7 |
counts, indexes, IDs |
REAL |
a number with a fractional part, 3.14 |
measurements, averages |
CHAR |
a single character, 'A' |
a grade letter, a menu choice |
STRING |
characters in quotes, "Hello" |
text |
BOOLEAN |
TRUE or FALSE |
flags |
DATE |
a calendar date | dates of birth, deadlines |

Pseudocode also names two structured types: ARRAY and FILE
ประเภทข้อมูลใดเหมาะที่สุดสำหรับเก็บสถานะใช่/ไม่ เช่น "มีสินค้า"?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
เฟลล์มีสองสถานะ ดังนั้น BOOLEAN (TRUE/FALSE) คือตัวเลือกที่แม่นยำ — ไม่ใช่สตริง "yes"/"no"
ประเภทจำนวนทศนิยม (real/floating-point) ควรใช้สำหรับเก็บค่าวัดเช่นอุณหภูมิ
Difficulty 1/5 · 100 coins + a small bonus on first completion today
นับจำนวนเต็มใช้ INTEGER; ค่าวัดใช้ REAL
Choose by how the value is used
- A type is decided by what the program will do with the value, not by how it looks.
- The number of students is
INTEGER; the class's average mark isREAL, because it has a fractional part; whether a student has paid isBOOLEAN. - An array index is always
INTEGER. A single grade letter isCHAR; an email address isSTRING. - A phone number is a
STRING: it starts with0, and nobody ever adds two phone numbers.
จับคู่แต่ละค่ากับประเภทข้อมูลที่เหมาะสมที่สุดสำหรับค่านั้น
Difficulty 2/5 · 140 coins + a small bonus on first completion today
เลือกประเภทที่แม่นยำ: BOOLEAN สำหรับเฟลล์, REAL สำหรับค่าเศษส่วน, INTEGER สำหรับจำนวนนับเต็ม, STRING สำหรับข้อความ
ประเภทข้อมูลใดควรเก็บหมายเลขโทรศัพท์เช่น 07700 900123?
Difficulty 1/5 · 100 coins + a small bonus on first completion today
INTEGER จะตัดเลข 0 หน้าออก และไม่มีการโปรแกรมที่บวกหรือคูณหมายเลขโทรศัพท์ ตัวเลขเหล่านี้คือข้อความ
Worked example: give the appropriate data type
| Data | Type | Because |
|---|---|---|
| number of items in stock | INTEGER |
a whole count |
| price of an item | REAL |
has pence, a fractional part |
| whether the item is on offer | BOOLEAN |
only two states |
the item's category code, 'F' |
CHAR |
exactly one character |
| the supplier's name | STRING |
text |
| the date the item was delivered | DATE |
a calendar date |
- Give the pseudocode name, not the language you happen to know:
BOOLEAN, notbool;REAL, notfloat.
ประเภทข้อมูลของดัชนี arreglo (array index) เป็น ______ เสมอ
Difficulty 1/5 · 100 coins + a small bonus on first completion today
ดัชนีนับตำแหน่ง จึงเป็นจำนวนเต็ม Names[2.5] ไม่มีความหมาย
Records
- A record (a record structure 记录结构) holds a set of data items of different data types under one identifier. Each item is a field 字段.
- Use one when several values always describe one thing: a stock item, a customer, a student.
TYPE TStockItem
DECLARE ItemID : INTEGER
DECLARE Category : STRING
DECLARE ItemCost : REAL
DECLARE InStock : BOOLEAN
ENDTYPE

One identifier, four fields, four types
เรคอร์ดใช้สำหรับ:
Difficulty 1/5 · 100 coins + a small bonus on first completion today
เรคอร์ดรวมฟิลด์ที่มี (อาจ) ประเภทต่างกันซึ่งอธิบายสิ่งหนึ่ง Arreglo เก็บค่าหลายค่าที่มีประเภทเดียวกัน
Declaring and using a record
TYPE … ENDTYPEdefines the type. Then declare variables of it, singly or as an array:
DECLARE Item1 : TStockItem
DECLARE Items : ARRAY[1:100] OF TStockItem
- Reach a field with dot notation 点号表示法, to write and to read:
Item1.Category ← "Fruit"
OUTPUT Item1.Category, " costs ", Item1.ItemCost
Items[7].InStock ← FALSE
เรคอร์ดรวมกลุ่มฟิลด์ภายใต้ชื่อเดียว
เรคอร์ดรวมฟิลด์ที่เกี่ยวข้องเข้าด้วยกัน ฟิลด์แต่ละอันเป็นป้ายชื่อที่คุณเข้าถึงได้ด้วยจุดProgramming — Item1.Category — ไม่ใช่ด้วยดัชนีเลข
เพื่อกำหนดค่าฟิลด์ Category ของตัวแปรเรคอร์ด Item1 คุณเขียน:
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Programming ด้วยจุด record.field เข้าถึงฟิลด์ของเรคอร์ด
Worked example: the club membership file
- A club stores, for each student, a student ID (a string), a name, a date of birth and up to three club numbers. Declare the record type, an array for 3000 students, and store a name in the first element.
TYPE Student
DECLARE StudentID : STRING
DECLARE Name : STRING
DECLARE DateOfBirth : DATE
DECLARE Club : ARRAY[1:3] OF INTEGER
ENDTYPE
DECLARE Membership : ARRAY[1:3000] OF Student
Membership[1].Name ← "Li Wei"
- The marks:
TYPEwith the identifier andENDTYPE; every field declared with a suitable type; the array with its bounds andOF Student; the field reached with the index and the dot.
เรียงลำดับบรรทัดของการประกาศสมาชิกชมรมให้ถูกต้อง
Difficulty 3/5 · 200 coins + a small bonus on first completion today
นิยามประเภท ปิดประเภท ประกาศตัวแปรหรือArreglo ของ类型นั้น จากนั้นใช้ดัชนีและProgramming ด้วยจุดเพื่อเข้าถึงฟิลด์
Worked example: state the error in the declaration
TYPE TBooking
DECLARE Seats : INTEGER
DECLARE Price
DECLARE Total : STRING
- Three errors, each a mark: the type has no
ENDTYPE; the fieldPricehas no data type; andTotalwill holdSeats * Price, an arithmetic result, so it cannot be aSTRING. - The examiner's usual three: a missing
ENDTYPE, a field without a type, a field of the wrong type for what is done with it.
ข้อใดเป็นข้อผิดพลาดในการประกาศเรคอร์ด? เลือก ทุก ข้อที่ถูกต้อง
Difficulty 2/5 · 140 coins + a small bonus on first completion today
ประเภทต่างกันภายในเรคอร์ดเดียวคือจุดประสงค์หลักของเรคอร์ด สามข้อที่เหลือคือข้อผิดพลาดที่ผู้ตั้งข้อสอบวางไว้
Record, array, or plain variables
- Values that always belong together and have different types: a record.
- Many values of the same type under one name: an array (next lesson).
- A table of rows where each row has mixed fields: an array of records,
Members[i].Name. - Unrelated values: separate variables. Bundling them gains nothing.
Arreglo ของเรคอร์ดช่วยให้ตัวระบุเดียวจัดเก็บตารางซึ่งแต่ละแถวมีฟิลด์ที่มีประเภทต่างกัน
Difficulty 1/5 · 100 coins + a small bonus on first completion today
Members[i] คือแถวหนึ่ง (re record); Members[i].Name คือเซลล์หนึ่ง Arregloให้แถว Re recordให้คอลัมน์ผสม
Marks that slip away
- A yes/no value is a
BOOLEAN, not the strings"yes"and"no". - A phone number or an ID that starts with
0is aSTRING, never anINTEGER. - Money in a
REALpicks up rounding errors; many systems store whole pennies as anINTEGER. - Every field is a
DECLAREline with a type, and the type ends withENDTYPE. The index goes on the array, the dot on the field:Members[3].Name.
You've got it
- six basic types,
INTEGER,REAL,CHAR,STRING,BOOLEAN,DATE, chosen by what the program does with the value - a record holds fields of different types under one identifier:
TYPE … DECLARE field : type … ENDTYPE - declare variables or arrays of the type, and reach a field with dot notation:
Membership[1].Name ← "Li Wei" - the errors the examiner plants: no
ENDTYPE, a field with no type, a field typed wrongly for its use