Records
Different types under one name
- AS topic 10.1 asks you to define a record: one identifier holding fields of different types.
TYPE…ENDTYPEnames the record and declares each field.- This is not an A2 class. There are no methods here.
ชนิดต่างกันภายใต้ชื่อเดียว
- AS topic 10.1 ต้องการให้คุณนิยาม record: identifier เดียวที่เก็บ fields ของหลายชนิด
TYPE…ENDTYPEชื่อบันทึกและประกาศแต่ละ field- นี่ไม่ใช่ A2 class. ไม่มี methods ตรงนี้
TYPE Student
DECLARE Name : STRING
DECLARE Mark : INTEGER
ENDTYPE
DECLARE S : Student
S.Name ← "Ada"
S.Mark ← 91
OUTPUT S.Name & " " & S.Mark
A field is read with a dot
S.Nameis theNamefield ofS.- You may copy a whole record:
Pupil2 ← Pupil1copies every field. - An array of records uses one index, then the field:
Form[1].Mark.
Field อ่านด้วยจุด
S.Nameคือ fieldNameของS- คุณสามารถคัดลอก record ทั้งหมด:
Pupil2 ← Pupil1คัดลอกทุก field - Array ของ records ใช้ index เดียวแล้วตามด้วย field:
Form[1].Mark
TYPE Student
DECLARE Name : STRING
DECLARE Mark : INTEGER
ENDTYPE
DECLARE Form : ARRAY[1:2] OF Student
Form[1].Name ← "Ada"
Form[1].Mark ← 91
Form[2].Name ← "Bo"
Form[2].Mark ← 80
OUTPUT Form[2].Name
Watch out
- Declare the
TYPEbefore youDECLAREa variable of that type. - Enumerated types and pointers are A2 (topic 13.1). They are not in this AS course.
ระวัง
- ประกาศ
TYPEก่อนที่จะDECLAREตัวแปรชนิดนั้น - Enumerated types และ pointers เป็น A2 (topic 13.1). พวกมันไม่มีในหลักสูตร AS นี้
Common mistakes
- Each field has its own
DECLAREinside theTYPE. ENDTYPEcloses the definition. Forgetting it leaves the type unfinished.
ข้อผิดพลาดที่พบบ่อย
- แต่ละ field มี
DECLAREของมันเองภายในTYPE ENDTYPEปิดการนิยาม. การลืมมันจะทำให้ type ไม่สมบูรณ์
Now you try
- Define a record, store two fields, and output them.
ลองดูเลย
- นิยามเรคอร์ด เก็บข้อมูลสองฟิลด์ และแสดงผลออก
Define a Student record with Name and Mark. Store Ada and 91, and output Ada 91. · นิยาม record Student ที่มี Name และ Mark. เก็บ Ada และ 91, และเอาต์พุต Ada 91
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
An array Form of two students is partly filled below. Store Bo as the second name and output it. · Array Form ของนักเรียนสองคนถูกเติมบางส่วนด้านล่าง. เก็บ Bo เป็นชื่อที่สองและเอาต์พุตมัน
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The array Form of two students is filled in below. Add up Form[1].Mark and Form[2].Mark with a FOR loop and output the total (171). · Array Form ของนักเรียนสองคนถูกเติมด้านล่าง. รวม Form[1].Mark และ Form[2].Mark ด้วยลูป FOR และเอาต์พุตผลรวม (171)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่