Skip to content · ⁨Lompat ke konten⁩
Subjects · ⁨Materi⁩
  • 1 Style and layout · ⁨Gaya & tata letak⁩
    1.1

    Kata kunci dan huruf besar/kecil

    English

    Pseudocode 伪代码 is the language Cambridge writes algorithms 算法 in. It is not a real programming language — no computer runs it — but the exam marks it as if it were, so the form matters.

    Every keyword 关键字 is written in upper case: IF, REPEAT, PROCEDURE, OUTPUT.

    • Write IF, never if or If.
    • A keyword can never be used as a name of your own: you may not call a variable FOR.

    Common mistakes

    • Lower-case keywords. The mark scheme expects upper case.
    • Inventing keywords. Only the words in this handout are in the syllabus.
    Bahasa Indonesia

    Pseudocode adalah bahasa yang digunakan Cambridge dalam menulis algoritma. Ini bukan bahasa pemrograman sungguhan — tidak ada komputer yang menjalankannya — tetapi ujian memberinya nilai seolah-olah itu nyata, sehingga bentuknya sangat penting.

    Setiap kata kunci ditulis dengan huruf kapital: IF, REPEAT, PROCEDURE, OUTPUT.

    DECLARE Count : INTEGER
    Count ← 3
    IF Count > 0
      THEN
        OUTPUT "Ready"
    ENDIF
    
    • Tulis IF, jangan pernah if atau If.
    • Kata kunci tidak boleh digunakan sebagai nama sendiri: Anda tidak boleh menyebut variabel FOR.

    Kesalahan umum

    • Kata kunci huruf kecil. Skema penilaian mengharapkan huruf kapital.
    • Menciptakan kata kunci. Hanya kata-kata dalam lembar kerja ini yang termasuk dalam silabus.
    1.2

    Identifikator

    English

    An identifier 标识符 is the name you give a variable 变量, a constant 常量, a procedure 过程 or a function 函数.

    Four rules, all from the syllabus:

    • It must start with a capital letter: Count, not count.
    • It may hold only letters and digits — no spaces, no punctuation.
    • It is not case-sensitive 不区分大小写, so Count and COUNT are the same name. Pick one spelling and keep it.
    • Make it mean something. NumberOfPlayers earns the reader's time; N does not.

    Common mistakes

    • count ← 0 — a lower-case start. 0478 requires the capital.
    • Number of players — spaces are not allowed inside a name.
    Bahasa Indonesia

    Sebuah identifikator adalah nama yang Anda berikan kepada variabel, konstanta, prosedur atau fungsi.

    DECLARE NumberOfPlayers : INTEGER
    NumberOfPlayers ← 4
    OUTPUT NumberOfPlayers
    

    Empat aturan, semuanya berasal dari silabus:

    • Harus dimulai dengan huruf kapital: Count, bukan count.
    • Hanya boleh berisi huruf dan angka — tanpa spasi, tanpa tanda baca.
    • Tidak peka huruf besar/kecil, jadi Count dan COUNT adalah nama yang sama. Pilih satu ejaan dan pertahankan itu.
    • Berikan makna pada nama tersebut. NumberOfPlayers menghargai waktu pembaca; N tidak.

    Kesalahan umum

    • count ← 0 — awal huruf kecil. 0478 mengharuskan huruf kapital.
    • Number of players — spasi tidak diperbolehkan di dalam nama.
    1.3

    Komentar dan indentasi

    English

    A comment 注释 starts with // and runs to the end of the line. It is for the human reader; the algorithm ignores it.

    Indentation 缩进 shows which statements are inside a structure. The syllabus asks for four spaces per level, with one exception you must know:

    • THEN and ELSE are indented two spaces from their IF.
    • The statements under them are indented four.
    • ENDIF lines up with its own IF.

    Common mistakes

    • Putting THEN on the IF line. That is the A-Level 9618 layout — see topic 11.
    • No indentation at all. A marker has to see the structure to award the structure marks.
    Bahasa Indonesia

    Sebuah komentar dimulai dengan // dan berjalan hingga akhir baris. Ini untuk pembaca manusia; algoritma mengabaikannya.

    // Swap the values of X and Y
    DECLARE X, Y, Temp : INTEGER
    X ← 1
    Y ← 2
    Temp ← X   // temporarily store X
    X ← Y
    Y ← Temp
    OUTPUT X, " ", Y
    

    Indentasi menunjukkan pernyataan mana yang berada di dalam struktur. Silabus meminta empat spasi per tingkat, dengan satu pengecualian yang harus Anda ketahui:

    DECLARE X, Y : INTEGER
    X ← 7
    Y ← 3
    IF X > Y
      THEN
        OUTPUT "X is bigger"
      ELSE
        OUTPUT "Y is bigger or equal"
    ENDIF
    
    • THEN dan ELSE diindentasi dua spasi dari IF mereka.
    • Pernyataan di bawahnya diindentasi empat spasi.
    • ENDIF sejajar dengan IF miliknya sendiri.

    Kesalahan umum

    • Menaruh THEN pada baris IF. Itu adalah tata letak A-Level 9618 — lihat topik 11.
    • Tidak ada indentasi sama sekali. Penilai harus melihat strukturnya untuk memberikan nilai struktur.
  • 2 Variables, constants and types · ⁨Variabel, konstanta & tipe⁩
    2.1

    Lima tipe data

    English

    0478 pseudocode has exactly five data types 数据类型. Learn the list — naming a type that is not on it loses the mark.

    Type Holds Example
    INTEGER a whole number 整数 42, -7
    REAL a number with a decimal part 小数 3.14, -0.5
    CHAR a single character 单个字符 'A'
    STRING a sequence of characters 字符串 "Hello"
    BOOLEAN TRUE or FALSE 布尔值 TRUE
    • A STRING is written in double quotes, a CHAR in single quotes.
    • TRUE and FALSE are keywords, so they are upper case and carry no quotes.

    Common mistakes

    • Writing FLOAT, DOUBLE or BOOL. Those are real languages, not this syllabus.
    • 'Hello' for a string — single quotes hold one character only.
    Bahasa Indonesia

    Pseudocode 0478 memiliki tepat lima tipe data. Hafalkan daftarnya — menyebut tipe yang tidak ada di dalamnya akan kehilangan nilai.

    Tipe Menyimpan Contoh
    INTEGER bilangan bulat 42, -7
    REAL bilangan dengan bagian desimal 3.14, -0.5
    CHAR satu karakter tunggal 'A'
    STRING sekuens karakter "Hello"
    BOOLEAN TRUE atau FALSE TRUE
    DECLARE Age : INTEGER
    DECLARE Price : REAL
    DECLARE Grade : CHAR
    DECLARE Name : STRING
    DECLARE Passed : BOOLEAN
    Age ← 17
    Price ← 2.50
    Grade ← 'A'
    Name ← "Mei"
    Passed ← TRUE
    OUTPUT Name, " is ", Age, " and passed: ", Passed
    
    • STRING ditulis dalam tanda kutip ganda, CHAR dalam tanda kutip tunggal.
    • TRUE dan FALSE adalah kata kunci, sehingga ditulis huruf kapital dan tanpa tanda kutip.

    Kesalahan umum

    • Menulis FLOAT, DOUBLE atau BOOL. Itu adalah bahasa sungguhan, bukan silabus ini.
    • 'Hello' untuk string — tanda kutip tunggal hanya memuat satu karakter.
    2.2

    DECLARE (Deklarasikan)

    English

    DECLARE names a variable 变量 and fixes its type before you use it.

    Several names of one type go on a single line, separated by commas:

    • Declare a variable before its first use, near the top of the algorithm.
    • A variable keeps its type for the whole algorithm — an INTEGER never holds "Mei".

    Common mistakes

    • Leaving out the colon: DECLARE Counter INTEGER.
    • Using a variable that was never declared. The exam expects the declaration.
    Bahasa Indonesia

    DECLARE memberi nama variabel dan menetapkan tipenya sebelum Anda menggunakannya.

    DECLARE <identifier> : <data type>
    
    DECLARE Counter : INTEGER
    DECLARE TotalToPay : REAL
    Counter ← 1
    TotalToPay ← 19.99
    OUTPUT "Item ", Counter, " costs ", TotalToPay
    

    Beberapa nama dengan satu tipe dapat diletakkan dalam satu baris, dipisahkan oleh koma:

    DECLARE Length, Width, Area : INTEGER
    Length ← 6
    Width ← 4
    Area ← Length * Width
    OUTPUT "Area = ", Area
    
    • Deklarasikan variabel sebelum penggunaan pertamanya, di dekat atas algoritma.
    • Variabel mempertahankan tipenya sepanjang algoritma — sebuah INTEGER tidak pernah menyimpan "Mei".

    Kesalahan umum

    • Melewatkan titik dua: DECLARE Counter INTEGER.
    • Menggunakan variabel yang tidak pernah dideklarasikan. Ujian mengharapkan deklarasinya.
    2.3

    CONSTANT (Konstanta)

    English

    A constant 常量 is a value that is fixed for the whole algorithm — a tax rate, π, the size of a class. Naming it once means a change is made in one place.

    • In 0478 the arrow is used, exactly as for an assignment.
    • The value must be a literal 字面量 — a number, a string, TRUE — never a calculation and never another variable.
    • A constant can never be given a new value later. That is the whole point of it.

    Common mistakes

    • CONSTANT TaxRate = 0.20. The = form is the A-Level 9618 spelling — see topic 11.
    • Assigning to a constant further down the algorithm.
    Bahasa Indonesia

    Sebuah konstanta adalah nilai yang tetap sepanjang algoritma — tarif pajak, π, ukuran kelas. Memberi nama sekali berarti perubahan dilakukan di satu tempat saja.

    CONSTANT <identifier> ← <value>
    
    CONSTANT TaxRate ← 0.20
    DECLARE Price, Tax : REAL
    Price ← 50.00
    Tax ← Price * TaxRate
    OUTPUT "Tax to pay: ", Tax
    
    • Dalam 0478 panah digunakan, persis seperti untuk penugasan.
    • Nilainya harus berupa literal — angka, string, TRUE — tidak pernah perhitungan dan tidak pernah variabel lain.
    • Konstanta tidak pernah bisa diberi nilai baru kemudian. Itulah tujuan utamanya.

    Kesalahan umum

    • CONSTANT TaxRate = 0.20. Bentuk = adalah ejaan A-Level 9618 — lihat topik 11.
    • Menugaskan ke konstanta lebih jauh di dalam algoritma.
  • 3 Assignment, input and output · ⁨Penugasan, input & output⁩
    3.1

    Panah penugasan

    English

    The assignment 赋值 operator is ←, a left-pointing arrow. It means take the value on the right and put it in the box on the left.

    Counter ← Counter + 1 looks impossible as mathematics and is ordinary as an instruction: work out the right-hand side with the value the box holds now, then store the result back.

    • If you cannot type ←, write <-. Both are accepted, here and in the exam.
    • The left side must be a single identifier or one array element. Never a calculation.

    Common mistakes

    • Counter = 0. In pseudocode = is a comparison, not an assignment — and confusing the two is the single commonest syntax error in this paper.
    • Counter + 1 ← Counter. The arrow points at the thing being changed.
    Bahasa Indonesia

    Operator penugasan adalah ←, panah yang menunjuk ke kiri. Artinya ambil nilai di sisi kanan dan masukkan ke dalam kotak di sisi kiri.

    <identifier> ← <value>
    
    DECLARE Counter : INTEGER
    Counter ← 0
    Counter ← Counter + 1
    Counter ← Counter + 1
    OUTPUT Counter
    

    Counter ← Counter + 1 terlihat mustahil sebagai matematika dan biasa sebagai instruksi: hitung sisi kanan dengan nilai yang sedang dimiliki kotak itu, lalu simpan hasilnya kembali.

    • Jika Anda tidak bisa mengetik ←, tulis <-. Keduanya diterima, di sini maupun dalam ujian.
    • Sisi kiri harus berupa satu identifikator atau satu elemen array. Jangan pernah perhitungan.

    Kesalahan umum

    • Counter = 0. Dalam pseudocode = adalah perbandingan, bukan penugasan — dan bingung keduanya adalah kesalahan sintaks paling umum dalam kertas ini.
    • Counter + 1 ← Counter. Panah menunjuk ke hal yang diubah.
    3.2

    INPUT dan OUTPUT

    English

    INPUT reads a value from the user into a variable. OUTPUT displays one or more values.

    • INPUT takes exactly one variable, and that variable must already be declared.
    • OUTPUT takes any number of items separated by commas; they are printed one after another with nothing between them.
    • A literal 字面量 string in an OUTPUT goes in double quotes; a variable does not.

    Prompting the user

    An INPUT on its own shows nothing on screen, so a well-written algorithm prints a prompt 提示 first:

    Common mistakes

    • INPUT "Enter a number", X — the prompt is a separate OUTPUT statement.
    • OUTPUT "Total: " + Total. 0478 joins output items with a comma; + is arithmetic and & belongs to A-Level 9618 (topic 11).
    • Forgetting the quotes: OUTPUT Hello looks for a variable called Hello.
    Bahasa Indonesia

    INPUT membaca nilai dari pengguna ke dalam variabel. OUTPUT menampilkan satu atau lebih nilai.

    DECLARE Name : STRING
    DECLARE Age : INTEGER
    INPUT Name
    INPUT Age
    OUTPUT "Hello ", Name
    OUTPUT "Next year you will be ", Age + 1
    
    • INPUT menerima tepat satu variabel, dan variabel itu harus sudah dideklarasikan.
    • OUTPUT menerima sejumlah item apa pun yang dipisahkan oleh koma; mereka dicetak satu demi lain dengan tidak ada spasi di antaranya.
    • String literal dalam OUTPUT harus menggunakan tanda kutip ganda; variabel tidak.

    Meminta input pengguna

    Perintah INPUT yang berdiri sendiri tidak menampilkan apa pun di layar, sehingga algoritma yang baik mencetak pesan permintaan terlebih dahulu:

    DECLARE Radius : REAL
    OUTPUT "Enter the radius: "
    INPUT Radius
    OUTPUT "The diameter is ", Radius * 2
    

    Kesalahan umum

    • INPUT "Enter a number", X — pesan permintaan adalah perintah OUTPUT terpisah.
    • OUTPUT "Total: " + Total. 0478 menggabungkan item output dengan koma; + adalah aritmatika dan & termasuk A-Level 9618 (topik 11).
    • Melupakan tanda kutip: OUTPUT Hello mencari variabel bernama Hello.
  • 4 Arithmetic and logic · ⁨Aritmatika & logika⁩
    4.1

    Operator aritmatika

    English

    Five arithmetic 算术 operators, in the order a calculator uses them.

    Operator Meaning A is 7, B is 2
    + add 加 A + B is 9
    - subtract 减 A - B is 5
    * multiply 乘 A * B is 14
    / divide 除 A / B is 3.5
    ^ raise to a power 幂 A ^ B is 49

    Brackets come first, then ^, then * and /, then + and - — the usual precedence 优先级. Use brackets whenever the reader would have to stop and think.

    Common mistakes

    • Expecting / to give a whole number. 7 / 2 is 3.5, not 3; for a whole number use DIV.
    • Writing A x B or A ÷ B. Use * and /.
    Bahasa Indonesia

    Lima operator aritmatika, sesuai urutan yang digunakan kalkulator.

    Operator Makna A adalah 7, B adalah 2
    + tambah A + B adalah 9
    - kurang A - B adalah 5
    * kali A * B adalah 14
    / bagi A / B adalah 3.5
    ^ pangkatkan A ^ B adalah 49
    DECLARE A, B : INTEGER
    A ← 7
    B ← 2
    OUTPUT "Sum: ", A + B
    OUTPUT "Product: ", A * B
    OUTPUT "Quotient: ", A / B
    OUTPUT "Power: ", A ^ B
    

    Tanda kurung muncul pertama, lalu ^, kemudian * dan /, lalu + dan - — tingkat prioritas standar. Gunakan tanda kurung setiap kali pembaca harus berhenti dan berpikir.

    DECLARE Total : REAL
    Total ← (12 + 18) / 2
    OUTPUT "Mean: ", Total
    

    Kesalahan umum

    • Mengharapkan / menghasilkan bilangan bulat. 7 / 2 adalah 3.5, bukan 3; untuk bilangan bulat gunakan DIV.
    • Menulis A x B atau A ÷ B. Gunakan * dan /.
    4.2

    DIV dan MOD

    English

    Integer division 整数除法 answers two different questions, and 0478 writes each of them as a function 函数, with the two values in brackets.

    • DIV(a, b) — how many whole times b goes into a, the quotient 商.
    • MOD(a, b) — what is left over, the remainder 余数.

    Both take integers, and both return an integer.

    What they are for

    MOD is the standard test for divides exactly, which makes it the way to ask whether a number is even 偶数:

    DIV converts a total into whole units — seconds into minutes, pence into pounds:

    Common mistakes

    • 10 MOD 3. That is the A-Level 9618 form; 0478 uses the function form MOD(10, 3).
    • Mixing the two up. DIV gives the whole part; MOD gives what does not fit.
    Bahasa Indonesia

    Pembagian bilangan bulat menjawab dua pertanyaan berbeda, dan 0478 menuliskan masing-masing sebagai sebuah fungsi, dengan dua nilai dalam kurung.

    • DIV(a, b) — berapa kali bilangan bulat b masuk ke dalam a, yaitu hasil bagi.
    • MOD(a, b) — sisa yang tersisa, yaitu sisa hasil bagi.
    OUTPUT DIV(10, 3)
    OUTPUT MOD(10, 3)
    OUTPUT DIV(17, 5)
    OUTPUT MOD(17, 5)
    

    Keduanya menerima bilangan bulat, dan keduanya mengembalikan bilangan bulat.

    Kegunaannya

    MOD adalah uji standar untuk habis dibagi, yang menjadikannya cara menanyakan apakah sebuah bilangan genap:

    DECLARE Number : INTEGER
    Number ← 12
    IF MOD(Number, 2) = 0
      THEN
        OUTPUT Number, " is even"
      ELSE
        OUTPUT Number, " is odd"
    ENDIF
    

    DIV mengonversi total menjadi satuan utuh — detik menjadi menit, sen menjadi pound:

    DECLARE Seconds : INTEGER
    Seconds ← 200
    OUTPUT DIV(Seconds, 60), " minutes and ", MOD(Seconds, 60), " seconds"
    

    Kesalahan umum

    • 10 MOD 3. Itu adalah bentuk A-Level 9618; 0478 menggunakan bentuk fungsi MOD(10, 3).
    • Mencampuradukkan keduanya. DIV memberikan bagian bulat; MOD memberikan sisa yang tidak muat.
    4.3

    Operator relasional dan logika

    English

    A relational operator 关系运算符 compares two values and produces TRUE or FALSE.

    Operator Means
    = is equal to 等于
    <> is not equal to 不等于
    > is greater than 大于
    < is less than 小于
    >= is greater than or equal to 大于等于
    <= is less than or equal to 小于等于

    The three logic operators 逻辑运算符 join conditions:

    • AND — both sides must be TRUE.
    • OR — at least one side must be TRUE.
    • NOT — turns TRUE into FALSE and back.

    Common mistakes

    • <> is the not equal sign here — not !=, which belongs to other languages.
    • IF Age >= 15 AND <= 18. Each side of AND needs its own complete comparison: Age >= 15 AND Age <= 18.
    • Calling these comparison operators in an answer. The syllabus term is relational.
    Bahasa Indonesia

    Sebuah operator relasional membandingkan dua nilai dan menghasilkan TRUE atau FALSE.

    Operator Artinya
    = sama dengan
    <> tidak sama dengan
    > lebih besar dari
    < lebih kecil dari
    >= lebih besar dari atau sama dengan
    <= lebih kecil dari atau sama dengan
    DECLARE Mark : INTEGER
    Mark ← 55
    OUTPUT "Pass: ", Mark >= 50
    OUTPUT "Perfect: ", Mark = 100
    OUTPUT "Not zero: ", Mark <> 0
    

    Tiga operator logika menggabungkan kondisi:

    • AND — kedua sisi harus TRUE.
    • OR — setidaknya satu sisi harus TRUE.
    • NOT — mengubah TRUE menjadi FALSE dan kembali.
    DECLARE Age : INTEGER
    DECLARE HasTicket : BOOLEAN
    Age ← 16
    HasTicket ← TRUE
    IF Age >= 15 AND HasTicket = TRUE
      THEN
        OUTPUT "Admitted"
    ENDIF
    IF NOT (Age > 18)
      THEN
        OUTPUT "Still a student rate"
    ENDIF
    

    Kesalahan umum

    • <> adalah tanda tidak sama di sini — bukan !=, yang merupakan bagian dari bahasa pemrograman lain.
    • IF Age >= 15 AND <= 18. Setiap sisi dari AND memerlukan perbandingan lengkapnya sendiri: Age >= 15 AND Age <= 18.
    • Menyebut ini sebagai operator perbandingan dalam jawaban. Istilah dalam kurikulum adalah relasional.
  • 5 String operations · ⁨Operasi string⁩
    5.1

    LENGTH, UCASE dan LCASE

    English

    0478 publishes four string 字符串 routines and no others. Three of them are here.

    LENGTH(<identifier>) returns how many characters 字符 a string holds — spaces included.

    UCASE(<identifier>) returns the string in upper case 大写, LCASE(<identifier>) in lower case 小写. Neither changes the original 原值 — each hands back a new value.

    That last line still prints Happy: to keep the change you must assign it back.

    Comparing without worrying about case

    UCASE on both sides is the standard way to accept an answer however it was typed:

    Common mistakes

    • Expecting UCASE(Name) to change Name. Assign the result back.
    • LENGTH counts characters, not words. LENGTH("Happy Days") is 10.
    Bahasa Indonesia

    0478 menerbitkan empat rutinitas string dan tidak ada lainnya. Tiga di antaranya ada di sini.

    LENGTH(<identifier>) mengembalikan jumlah karakter yang disimpan sebuah string — termasuk spasi.

    OUTPUT LENGTH("Happy Days")
    OUTPUT LENGTH("")
    

    UCASE(<identifier>) mengembalikan string dalam huruf kapital, LCASE(<identifier>) dalam huruf kecil huruf kecil. Keduanya tidak mengubah asli — masing-masing mengembalikan nilai baru.

    DECLARE Name : STRING
    Name ← "Happy"
    OUTPUT UCASE(Name)
    OUTPUT LCASE(Name)
    OUTPUT Name
    

    Baris terakhir tersebut tetap mencetak Happy: untuk menyimpan perubahan Anda harus assignsnya kembali.

    DECLARE Name : STRING
    Name ← "Happy"
    Name ← UCASE(Name)
    OUTPUT Name
    

    Membandingkan tanpa khawatir tentang huruf besar/kecil

    UCASE pada kedua sisi adalah cara standar untuk menerima jawaban bagaimanapun diketiknya:

    DECLARE Answer : STRING
    Answer ← "yes"
    IF UCASE(Answer) = "YES"
      THEN
        OUTPUT "Accepted"
    ENDIF
    

    Kesalahan umum

    • Mengharapkan UCASE(Name) mengubah Name. Assign hasilnya kembali.
    • LENGTH menghitung karakter, bukan kata. LENGTH("Happy Days") adalah 10.
    5.2

    SUBSTRING

    English

    SUBSTRING(<identifier>, <start>, <length>) pulls a piece out of a string — starting at position start, taking length characters.

    ⚠️ Counting starts at 1, not 0. The first character of "Happy Days" is at position 1.

    Taking one character, and the rest

    LENGTH inside SUBSTRING is what makes the last two lines work for a word of any size — hard-coding 8 would break the moment the word changed.

    Reading a string one character at a time

    Common mistakes

    • Starting at 0. Position 1 is the first character.
    • Reading the third argument as an end position. It is a count — SUBSTRING(W, 2, 3) takes three characters beginning at the second.
    • Asking for more characters than the string has.
    Bahasa Indonesia

    SUBSTRING(<identifier>, <start>, <length>) mengambil sebagian dari string — mulai dari posisi start, mengambil length karakter.

    OUTPUT SUBSTRING("Happy Days", 1, 5)
    OUTPUT SUBSTRING("Happy Days", 7, 4)
    

    ⚠️ Penghitungan dimulai dari 1, bukan 0. Karakter pertama dari "Happy Days" berada di posisi 1.

    Mengambil satu karakter, dan sisanya

    DECLARE Word : STRING
    Word ← "Computer"
    OUTPUT "First letter: ", SUBSTRING(Word, 1, 1)
    OUTPUT "Last letter: ", SUBSTRING(Word, LENGTH(Word), 1)
    OUTPUT "Without the first: ", SUBSTRING(Word, 2, LENGTH(Word) - 1)
    

    LENGTH di dalam SUBSTRING adalah alasan mengapa dua baris terakhir bekerja untuk kata berukuran apa pun — mengkode keras 8 akan merusak saat kata berubah.

    Membaca string satu karakter pada satu waktu

    DECLARE Word : STRING
    DECLARE Index : INTEGER
    Word ← "Cat"
    FOR Index ← 1 TO LENGTH(Word)
        OUTPUT SUBSTRING(Word, Index, 1)
    NEXT Index
    

    Kesalahan umum

    • Dimulai dari 0. Posisi 1 adalah karakter pertama.
    • Membaca argumen ketiga sebagai posisi akhir. Itu adalah jumlah — SUBSTRING(W, 2, 3) mengambil tiga karakter dimulai dari yang kedua.
    • Meminta lebih banyak karakter daripada yang dimiliki string.
  • 6 Selection · ⁨Seleksi⁩
    6.1

    IF, THEN, ELSE, ENDIF

    English

    Selection 选择 runs one group of statements or another, depending on a condition 条件.

    Add ELSE for the other case. Exactly one of the two branches 分支 runs — never both, never neither.

    The layout is the one part students lose marks on, so learn it as a shape:

    • IF and its condition on one line, nothing after the condition.
    • THEN and ELSE on their own lines, indented two spaces.
    • The statements indented four.
    • ENDIF back at the column of its IF.

    Nesting 嵌套

    An IF inside an IF handles more than two outcomes. Each one needs its own ENDIF.

    Common mistakes

    • Forgetting ENDIF. Every IF closes, and a missing one is a syntax error 语法错误 the marker will see at a glance.
    • IF Mark >= 50 THEN OUTPUT "Pass" on one line. No such form exists in either syllabus; write the three lines.
    • IF Mark = 50 THEN written as IF Mark ← 50. The arrow assigns; = compares.
    Bahasa Indonesia

    Pemilihan menjalankan satu kelompok pernyataan atau kelompok lain, tergantung pada suatu kondisi.

    IF <condition>
      THEN
        <statements>
    ENDIF
    
    DECLARE Mark : INTEGER
    Mark ← 72
    IF Mark >= 50
      THEN
        OUTPUT "Pass"
    ENDIF
    

    Tambahkan ELSE untuk kasus lainnya. Tepat salah satu dari dua cabang berjalan — tidak pernah keduanya, tidak pernah tidak satupun.

    DECLARE Mark : INTEGER
    Mark ← 41
    IF Mark >= 50
      THEN
        OUTPUT "Pass"
      ELSE
        OUTPUT "Fail"
    ENDIF
    

    Layout adalah bagian yang membuat siswa kehilangan poin, jadi pelajari sebagai bentuk:

    • IF dan kondisinya pada satu baris, tidak ada apa pun setelah kondisi.
    • THEN dan ELSE pada baris tersendiri, menjorok dua spasi.
    • Pernyataan menjorok empat.
    • ENDIF kembali ke kolom IF-nya.

    Bersarang

    Sebuah IF di dalam IF menangani lebih dari dua hasil. Masing-masing membutuhkan ENDIF-nya sendiri.

    DECLARE Mark : INTEGER
    Mark ← 85
    IF Mark >= 80
      THEN
        OUTPUT "Distinction"
      ELSE
        IF Mark >= 50
          THEN
            OUTPUT "Pass"
          ELSE
            OUTPUT "Fail"
        ENDIF
    ENDIF
    

    Kesalahan umum

    • Melupakan ENDIF. Setiap IF ditutup, dan yang hilang adalah kesalahan sintaks yang penanda akan lihat seketika.
    • IF Mark >= 50 THEN OUTPUT "Pass" pada satu baris. Tidak ada bentuk seperti itu dalam kurikulum mana pun; tulis tiga barisnya.
    • IF Mark = 50 THEN ditulis sebagai IF Mark ← 50. Panah melakukan assign; = membandingkan.
    6.2

    CASE OF

    English

    When one variable is tested against several single values, CASE OF says it far more clearly than a stack of nested IFs.

    • Each branch is a value, a colon, then what to do.
    • OTHERWISE catches everything the listed values missed. It is optional, and writing one is good practice — it is what happens when the user types something unexpected.
    • The first matching branch runs, and the rest are skipped.

    A range, and a character

    Common mistakes

    • Using CASE OF for a condition. It tests one variable against values; Mark >= 50 needs an IF.
    • Forgetting ENDCASE.
    • Leaving out OTHERWISE and then wondering why an unexpected value does nothing at all.
    Bahasa Indonesia

    Ketika satu variabel diuji terhadap beberapa nilai tunggal, CASE OF menyatakannya jauh lebih jelas daripada tumpukan IF yang bersarang.

    CASE OF <identifier>
        <value 1> : <statement>
        <value 2> : <statement>
        OTHERWISE <statement>
    ENDCASE
    
    DECLARE Choice : INTEGER
    Choice ← 2
    CASE OF Choice
        1 : OUTPUT "You chose north"
        2 : OUTPUT "You chose south"
        3 : OUTPUT "You chose east"
        4 : OUTPUT "You chose west"
        OTHERWISE OUTPUT "That is not a direction"
    ENDCASE
    
    • Setiap cabang adalah nilai, titik dua, lalu apa yang harus dilakukan.
    • OTHERWISE menangkap semua yang dilewati oleh nilai yang terdaftar. Itu opsional, dan menulis satu adalah praktik baik — itu yang terjadi ketika pengguna mengetik sesuatu yang tidak terduga.
    • Cabang yang cocok pertama kali berjalan, dan yang lainnya dilewati.

    Rentang, dan karakter

    DECLARE Grade : CHAR
    Grade ← 'B'
    CASE OF Grade
        'A' : OUTPUT "Excellent"
        'B' : OUTPUT "Good"
        'C' : OUTPUT "Satisfactory"
        OTHERWISE OUTPUT "Unclassified"
    ENDCASE
    

    Kesalahan umum

    • Menggunakan CASE OF untuk kondisi. Ia menguji satu variabel terhadap nilai; Mark >= 50 membutuhkan IF.
    • Melupakan ENDCASE.
    • Melewatkan OTHERWISE dan kemudian bertanya-tanya mengapa nilai tak terduga tidak memberikan hasil apa pun.
  • 7 Iteration · ⁨Iterasi⁩
    7.1

    FOR, perulangan yang dihitung

    English

    Iteration 迭代 repeats statements. Use a FOR loop when you know before you start how many times — a count-controlled 计数控制 loop.

    The counter 计数器 starts at the first value, and the loop runs once for every value up to and including the second. 1 TO 5 runs five times.

    STEP

    STEP changes the size of each jump. A negative step counts down.

    Running totals

    The commonest use: a variable outside the loop that the loop adds to.

    Total ← 0 must sit before the loop. Inside, it would reset on every pass.

    Common mistakes

    • NEXT naming the wrong counter. Nested loops must close in the reverse order they opened: the inner NEXT first.
    • Assuming 1 TO 5 runs four times. Both ends are included.
    • Changing the counter inside the loop. Let FOR own it.
    Bahasa Indonesia

    Iterasi mengulang pernyataan. Gunakan loop FOR ketika Anda tahu sebelum memulai berapa kali — sebuah loop terkendali oleh hitungan.

    FOR <identifier> ← <value1> TO <value2>
        <statements>
    NEXT <identifier>
    
    DECLARE Index : INTEGER
    FOR Index ← 1 TO 5
        OUTPUT "Line ", Index
    NEXT Index
    

    Penghitung dimulai dari nilai pertama, dan loop berjalan sekali untuk setiap nilai hingga dan termasuk nilai kedua. 1 TO 5 berjalan lima kali.

    STEP

    STEP mengubah ukuran setiap lompatan. Step negatif menghitung mundur.

    DECLARE Index : INTEGER
    FOR Index ← 2 TO 10 STEP 2
        OUTPUT Index
    NEXT Index
    FOR Index ← 3 TO 1 STEP -1
        OUTPUT "Countdown ", Index
    NEXT Index
    

    Total berjalan

    Penggunaan paling umum: variabel di luar loop yang ditambahkan oleh loop tersebut.

    DECLARE Index, Total : INTEGER
    Total ← 0
    FOR Index ← 1 TO 10
        Total ← Total + Index
    NEXT Index
    OUTPUT "Sum of 1 to 10 is ", Total
    

    Total ← 0 harus berada sebelum loop. Di dalamnya, itu akan direset pada setiap lemparan.

    Kesalahan umum

    • NEXT menamai penghitung yang salah. Loop bersarang harus ditutup dengan urutan terbalik mereka dibuka: NEXT bagian dalam terlebih dahulu.
    • Menganggap 1 TO 5 berjalan empat kali. Kedua ujungnya termasuk.
    • Mengubah penghitung di dalam loop. Biarkan FOR memilikinya.
    7.2

    WHILE, loop pra-kondisi

    English

    A WHILE loop tests before each pass, so it can run zero times. Use it when the number of repeats depends on something that happens while the loop runs.

    Something inside the loop must eventually make the condition false, or the loop never ends — an infinite loop 无限循环.

    Common mistakes

    • Forgetting to change the variable the condition tests, giving an infinite loop.
    • Leaving out DO or ENDWHILE.
    • Using WHILE where the count is known. A FOR says what you mean in one line.
    Bahasa Indonesia

    Loop WHILE menguji sebelum setiap lemparan, sehingga bisa berjalan nol kali. Gunakan jika jumlah pengulangan bergantung pada sesuatu yang terjadi saat loop berjalan.

    WHILE <condition> DO
        <statements>
    ENDWHILE
    
    DECLARE Total : INTEGER
    Total ← 1
    WHILE Total < 100 DO
        Total ← Total * 2
    ENDWHILE
    OUTPUT "First power of two past 100: ", Total
    

    Sesuatu di dalam loop harus akhirnya membuat kondisi menjadi false, atau loop tidak pernah berakhir — sebuah infinite loop (loop tak terhingga).

    DECLARE Count : INTEGER
    Count ← 5
    WHILE Count > 0 DO
        OUTPUT Count
        Count ← Count - 1
    ENDWHILE
    OUTPUT "Lift off"
    

    Kesalahan umum

    • Lupa mengubah variabel yang diuji kondisinya, menyebabkan infinite loop.
    • Melewatkan DO atau ENDWHILE.
    • Menggunakan WHILE di mana hitungan diketahui. A FOR mengatakan maksud Anda dalam satu baris.
    7.3

    REPEAT, loop pasca-kondisi

    English

    A REPEAT loop tests after each pass, so it always runs at least once.

    That "at least once" is the reason to choose it: validating 验证 input, where you must ask before you can judge the answer.

    ⚠️ UNTIL states the condition that stops the loop; WHILE states the one that keeps it going. They are opposites, and swapping them is the classic error.

    Tests Runs at least once? Condition means
    FOR a counter no (an empty range runs zero times) —
    WHILE before no keep going while this is true
    REPEAT after yes stop when this becomes true

    Common mistakes

    • Writing UNTIL Number < 100 when you mean UNTIL Number >= 100.
    • Using REPEAT where the body must be able to run zero times.
    • Forgetting that UNTIL ends the structure — there is no ENDREPEAT.
    Bahasa Indonesia

    Loop REPEAT menguji setelah setiap lemparan, sehingga selalu berjalan setidaknya sekali.

    REPEAT
        <statements>
    UNTIL <condition>
    

    Itu "setidaknya sekali" adalah alasan memilihnya: memvalidasi input, di mana Anda harus meminta sebelum Anda dapat menilai jawabannya.

    DECLARE Number : INTEGER
    Number ← 0
    REPEAT
        Number ← Number + 25
        OUTPUT "Trying ", Number
    UNTIL Number >= 100
    OUTPUT "Reached ", Number
    

    ⚠️ UNTIL menyatakan kondisi yang menghentikan loop; WHILE menyatakan satu yang membiarkannya terus berjalan. Mereka berlawanan, dan menukar keduanya adalah kesalahan klasik.

    Menguji Berjalan setidaknya sekali? Kondisi berarti
    FOR penghitung tidak (rentang kosong berjalan nol kali) —
    WHILE sebelum tidak lanjutkan selama ini true
    REPEAT setelah ya berhenti ketika ini menjadi true

    Kesalahan umum

    • Menulis UNTIL Number < 100 padahal bermaksud UNTIL Number >= 100.
    • Menggunakan REPEAT di mana tubuh harus mampu berjalan nol kali.
    • Lupa bahwa UNTIL mengakhiri struktur — tidak ada ENDREPEAT.
  • 8 Arrays · ⁨Array⁩
    8.1

    Array satu dimensi

    English

    An array 数组 holds many values of one type under one name. Each value sits at an index 下标.

    • The bounds 边界 are written lower:upper, and both are included — [1:3] has three elements 元素.
    • Cambridge arrays normally start at 1, not 0.
    • Every element has the same type, fixed by OF.

    Filling and reading with a FOR loop

    An array and a counted loop belong together: the counter is the index.

    Finding the largest

    Start Largest at the first element, never at 0 — with negative data, 0 would win and the answer would be wrong.

    Common mistakes

    • Reading Scores[6] from an array declared [1:5]. That is out of bounds 越界.
    • Writing Scores(3). Arrays use square brackets; round brackets call a function.
    • Declaring [1:5] and then looping 0 TO 4.
    Bahasa Indonesia

    Sebuah array menyimpan banyak nilai satu tipe di bawah satu nama. Setiap nilai duduk pada indeks.

    DECLARE <identifier> : ARRAY[<lower>:<upper>] OF <data type>
    
    DECLARE Names : ARRAY[1:3] OF STRING
    Names[1] ← "Mei"
    Names[2] ← "Sam"
    Names[3] ← "Ana"
    OUTPUT Names[2]
    
    • Batas ditulis lower:upper, dan keduanya termasuk — [1:3] memiliki tiga elemen.
    • Array Cambridge biasanya dimulai dari 1, bukan 0.
    • Setiap elemen memiliki tipe yang sama, ditetapkan oleh OF.

    Mengisi dan membaca dengan loop FOR

    Array dan loop terhitung saling berkaitan: penghitung adalah indeks.

    DECLARE Scores : ARRAY[1:5] OF INTEGER
    DECLARE Index, Total : INTEGER
    FOR Index ← 1 TO 5
        Scores[Index] ← Index * 10
    NEXT Index
    Total ← 0
    FOR Index ← 1 TO 5
        Total ← Total + Scores[Index]
    NEXT Index
    OUTPUT "Total: ", Total
    OUTPUT "Mean: ", Total / 5
    

    Menemukan nilai terbesar

    DECLARE Scores : ARRAY[1:5] OF INTEGER
    DECLARE Index, Largest : INTEGER
    Scores[1] ← 42
    Scores[2] ← 17
    Scores[3] ← 93
    Scores[4] ← 8
    Scores[5] ← 55
    Largest ← Scores[1]
    FOR Index ← 2 TO 5
        IF Scores[Index] > Largest
          THEN
            Largest ← Scores[Index]
        ENDIF
    NEXT Index
    OUTPUT "Largest: ", Largest
    

    Mulai Largest dari elemen pertama, jangan di 0 — dengan data negatif, 0 akan menang dan jawabannya akan salah.

    Kesalahan umum

    • Membaca Scores[6] dari array yang dideklarasikan [1:5]. Itu di luar batas.
    • Menulis Scores(3). Array menggunakan kurung siku; kurung bulat memanggil fungsi.
    • Mendeklarasikan [1:5] dan kemudian melakukan looping 0 TO 4.
    8.2

    Array dua dimensi

    English

    A 2-D array 二维数组 is a table: rows and columns, two indexes.

    The first index is the row 行, the second the column 列.

    Nested loops walk a table

    One loop per dimension 维度. The inner loop finishes a whole row before the outer moves on.

    ⚠️ The inner NEXT closes the inner counter. NEXT Row before NEXT Col crosses the loops over and is nonsense — a marker spots it instantly.

    Common mistakes

    • Swapping row and column. Grid[2,3] is row 2, column 3.
    • Grid[Row][Col]. Cambridge writes one pair of brackets with a comma inside.
    • Crossing the NEXT lines over, as above.
    Bahasa Indonesia

    A 2-D array adalah tabel: baris dan kolom, dua indeks.

    DECLARE <identifier> : ARRAY[<lower1>:<upper1>, <lower2>:<upper2>] OF <data type>
    
    DECLARE Grid : ARRAY[1:2, 1:3] OF INTEGER
    Grid[1,1] ← 1
    Grid[1,2] ← 2
    Grid[1,3] ← 3
    Grid[2,1] ← 4
    Grid[2,2] ← 5
    Grid[2,3] ← 6
    OUTPUT Grid[2,3]
    

    Indeks pertama adalah baris, yang kedua adalah kolom.

    Loop bersarang menelusuri tabel

    Satu loop per dimensi. Loop dalam menyelesaikan seluruh baris sebelum yang luar berlanjut.

    DECLARE Grid : ARRAY[1:2, 1:3] OF INTEGER
    DECLARE Row, Col : INTEGER
    FOR Row ← 1 TO 2
        FOR Col ← 1 TO 3
            Grid[Row, Col] ← Row * Col
        NEXT Col
    NEXT Row
    FOR Row ← 1 TO 2
        FOR Col ← 1 TO 3
            OUTPUT "Grid[", Row, ",", Col, "] = ", Grid[Row, Col]
        NEXT Col
    NEXT Row
    

    ⚠️ Loop dalam NEXT menutup penghitung dalam. NEXT Row sebelum NEXT Col melintasi loop dan tidak masuk akal — penanda mendeteksinya seketika.

    Kesalahan umum

    • Menukar baris dan kolom. Grid[2,3] adalah baris 2, kolom 3.
    • Grid[Row][Col]. Cambridge menulis satu pasangan kurung dengan koma di dalamnya.
    • Melintasi garis NEXT seperti di atas.
  • 9 Procedures and functions · ⁨Prosedur & fungsi⁩
    9.1

    PROCEDURE dan CALL

    English

    A procedure 过程 is a named block of statements. Writing one once and calling it from several places is decomposition 分解 — the idea the whole of paper 2 rests on.

    A procedure is called by name, with the keyword CALL:

    • The definition 定义 does nothing on its own. Only CALL runs it.
    • Control returns to the line after the CALL when the procedure ends.
    • A procedure does not give a value back. If you need one, you want a function.

    Common mistakes

    • Writing DefaultLine on its own to run it. 0478 requires CALL.
    • Leaving out ENDPROCEDURE.
    • Expecting the procedure's own variables to exist outside it.
    Bahasa Indonesia

    Sebuah procedure adalah blok pernyataan bernama. Menulisnya sekali dan memanggilnya dari beberapa tempat adalah dekomposisi — ide yang mendasari seluruh kertas 2.

    PROCEDURE <identifier>
        <statements>
    ENDPROCEDURE
    

    Prosedur dipanggil dengan nama, dengan kata kunci CALL:

    PROCEDURE DefaultLine
        OUTPUT "------------------------------"
    ENDPROCEDURE
    
    CALL DefaultLine
    OUTPUT "Report"
    CALL DefaultLine
    
    • Definisi itu sendiri tidak melakukan apa-apa. Hanya CALL yang menjalankannya.
    • Kontrol kembali ke baris setelah CALL ketika prosedur berakhir.
    • Prosedur tidak mengembalikan nilai. Jika Anda membutuhkannya, Anda menginginkan fungsi.

    Kesalahan umum

    • Menulis DefaultLine sendirian untuk menjalankannya. 0478 memerlukan CALL.
    • Melewatkan ENDPROCEDURE.
    • Mengharapkan variabel milik prosedur itu ada di luarnya.
    9.2

    FUNCTION dan RETURN

    English

    A function 函数 is a procedure that hands a value back. It is written where that value is needed — inside an OUTPUT, on the right of an assignment, inside a condition.

    • RETURNS <data type> on the header says what type comes back. It is not optional.
    • RETURN ends the function immediately — nothing after it runs.
    • A function is never called with CALL; it is used wherever its value belongs.

    A function inside a condition

    Common mistakes

    • CALL SumSquare(10, 20) — that throws the answer away.
    • A function with no RETURN on some path. Every route out must return a value.
    • Leaving RETURNS INTEGER off the header.
    Bahasa Indonesia

    Sebuah fungsi adalah prosedur yang mengembalikan nilai. Ditulis di mana nilai dibutuhkan — di dalam OUTPUT, di sebelah kanan assignment, di dalam kondisi.

    FUNCTION <identifier>(<parameters>) RETURNS <data type>
        <statements>
        RETURN <value>
    ENDFUNCTION
    
    FUNCTION SumSquare(Number1 : INTEGER, Number2 : INTEGER) RETURNS INTEGER
        RETURN Number1 * Number1 + Number2 * Number2
    ENDFUNCTION
    
    OUTPUT "Sum of squares = ", SumSquare(10, 20)
    
    • RETURNS <data type> pada header mengatakan tipe apa yang kembali. Itu tidak opsional.
    • RETURN mengakhiri fungsi langsung — tidak ada yang setelahnya berjalan.
    • Fungsi tidak pernah dipanggil dengan CALL; digunakan di mana pun nilainya sesuai.

    Fungsi di dalam kondisi

    FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN
        RETURN MOD(Number, 2) = 0
    ENDFUNCTION
    
    DECLARE Index : INTEGER
    FOR Index ← 1 TO 6
        IF IsEven(Index)
          THEN
            OUTPUT Index, " is even"
        ENDIF
    NEXT Index
    

    Kesalahan umum

    • CALL SumSquare(10, 20) — yang membuang jawaban.
    • Fungsi tanpa RETURN pada beberapa jalur. Setiap rute keluar harus mengembalikan nilai.
    • Melewatkan RETURNS INTEGER dari header.
    9.3

    Parameters

    English

    A parameter 参数 is a value a procedure or function is given when it is called. Each one carries its own type.

    Several parameters are separated by commas, and the order at the call must match the order in the definition:

    ⚠️ In 0478 every parameter is passed by value 传值: the procedure works on a copy, so changing it inside changes nothing outside. To get a value back, use a FUNCTION and RETURN it. (A-Level 9618 adds BYREF for the other behaviour — topic 11.)

    Common mistakes

    • PROCEDURE Line(Size) with no type.
    • Calling with the arguments in the wrong order — Greet(2, "Mei") is a type error.
    • Expecting a procedure to change the variable you passed in. In 0478 it cannot.
    Bahasa Indonesia

    Sebuah parameter adalah nilai yang diberikan kepada prosedur atau fungsi saat dipanggil. Masing-masing membawa tipenya sendiri.

    PROCEDURE Line(Size : INTEGER)
        DECLARE Index : INTEGER
        FOR Index ← 1 TO Size
            OUTPUT "-"
        NEXT Index
    ENDPROCEDURE
    
    CALL Line(5)
    CALL Line(20)
    

    Beberapa parameter dipisahkan oleh koma, dan urutan pada pemanggilan harus sesuai dengan urutan di definisi:

    PROCEDURE Greet(Name : STRING, Times : INTEGER)
        DECLARE Index : INTEGER
        FOR Index ← 1 TO Times
            OUTPUT "Hello ", Name
        NEXT Index
    ENDPROCEDURE
    
    CALL Greet("Mei", 2)
    CALL Greet("Sam", 1)
    

    ⚠️ Di 0478 setiap parameter diteruskan oleh nilai: prosedur bekerja pada salinan, jadi mengubahnya di dalam tidak mengubah apa pun di luar. Untuk mendapatkan nilai kembali, gunakan FUNCTION dan RETURN it. (A-Level 9618 menambahkan BYREF untuk perilaku lainnya — topik 11.)

    FUNCTION Doubled(Number : INTEGER) RETURNS INTEGER
        RETURN Number * 2
    ENDFUNCTION
    
    DECLARE Value : INTEGER
    Value ← 7
    Value ← Doubled(Value)
    OUTPUT Value
    

    Kesalahan umum

    • PROCEDURE Line(Size) tanpa tipe.
    • Memanggil dengan argumen dalam urutan yang salah — Greet(2, "Mei") adalah kesalahan tipe.
    • Mengharapkan prosedur untuk mengubah variabel yang Anda berikan. Di 0478, hal itu tidak dapat dilakukan.
  • 10 File handling · ⁨Penanganan file⁩
    10.1

    Menulis ke fail

    English

    A file 文件 keeps data after the program ends. 0478 uses four commands, and every one names the file it works on.

    Three file modes 文件模式:

    Mode What it does
    WRITE start a new file — anything already there is lost
    APPEND add to the end of what is there 追加
    READ read from the start
    • One WRITEFILE writes one line.
    • CLOSEFILE is not optional. An unclosed file can lose the data still waiting to be written, and that is a stock exam answer.

    Common mistakes

    • FOR WRITE when you meant FOR APPEND, which silently destroys the old contents.
    • Forgetting CLOSEFILE.
    • Opening a file that is already open.
    Bahasa Indonesia

    Sebuah file menyimpan data setelah program berakhir. 0478 menggunakan empat perintah, dan setiap satu dinamai file yang bekerja dengannya.

    OPENFILE <file identifier> FOR WRITE
    WRITEFILE <file identifier>, <data>
    CLOSEFILE <file identifier>
    
    OPENFILE "names.txt" FOR WRITE
    WRITEFILE "names.txt", "Mei"
    WRITEFILE "names.txt", "Sam"
    CLOSEFILE "names.txt"
    OUTPUT "Saved"
    

    Tiga mode file:

    Mode Apa yang dilakukannya
    WRITE mulai file baru — apa pun yang sudah ada hilang
    APPEND tambahkan ke akhir apa yang ada
    READ baca dari awal
    OPENFILE "log.txt" FOR WRITE
    WRITEFILE "log.txt", "first"
    CLOSEFILE "log.txt"
    OPENFILE "log.txt" FOR APPEND
    WRITEFILE "log.txt", "second"
    CLOSEFILE "log.txt"
    OUTPUT "Both lines saved"
    
    • Satu WRITEFILE menulis satu baris.
    • CLOSEFILE tidak opsional. File yang tidak ditutup dapat kehilangan data yang masih menunggu untuk ditulis, dan itu adalah jawaban ujian standar.

    Kesalahan umum

    • FOR WRITE ketika Anda bermaksud FOR APPEND, yang secara diam-diam menghancurkan isi lama.
    • Melupakan CLOSEFILE.
    • Membuka file yang sudah terbuka.
    10.2

    Membaca file hingga akhir

    English

    READFILE takes one line into a variable. The variable must be declared first.

    Normally you do not know how many lines there are, so you read until the end of the file — which is what the EOF function reports.

    EOF("names.txt") is TRUE once every line has been read, so WHILE NOT EOF(...) means while there is still something to read.

    Counting while reading

    Common mistakes

    • A WHILE EOF(...) loop — that runs only when there is nothing left. You want NOT.
    • Reading past the end because the loop has no EOF guard.
    • Opening FOR READ a file that was never written.
    Bahasa Indonesia

    READFILE mengambil satu baris ke dalam variabel. Variabel harus dideklarasikan terlebih dahulu.

    OPENFILE <file identifier> FOR READ
    READFILE <file identifier>, <variable>
    CLOSEFILE <file identifier>
    

    Biasanya Anda tidak tahu berapa banyak barisnya, jadi Anda membaca hingga akhir dari file — yang merupakan apa yang dilaporkan oleh fungsi EOF.

    OPENFILE "names.txt" FOR WRITE
    WRITEFILE "names.txt", "Mei"
    WRITEFILE "names.txt", "Sam"
    WRITEFILE "names.txt", "Ana"
    CLOSEFILE "names.txt"
    
    DECLARE Line : STRING
    OPENFILE "names.txt" FOR READ
    WHILE NOT EOF("names.txt") DO
        READFILE "names.txt", Line
        OUTPUT Line
    ENDWHILE
    CLOSEFILE "names.txt"
    

    EOF("names.txt") menjadi TRUE sekali setiap baris telah dibaca, sehingga WHILE NOT EOF(...) berarti selama masih ada sesuatu untuk dibaca.

    Menghitung saat membaca

    OPENFILE "marks.txt" FOR WRITE
    WRITEFILE "marks.txt", 40
    WRITEFILE "marks.txt", 65
    WRITEFILE "marks.txt", 88
    CLOSEFILE "marks.txt"
    
    DECLARE Mark, Count, Total : INTEGER
    Count ← 0
    Total ← 0
    OPENFILE "marks.txt" FOR READ
    WHILE NOT EOF("marks.txt") DO
        READFILE "marks.txt", Mark
        Count ← Count + 1
        Total ← Total + Mark
    ENDWHILE
    CLOSEFILE "marks.txt"
    OUTPUT Count, " marks, mean ", Total / Count
    

    Kesalahan umum

    • Perulangan WHILE EOF(...) — yang hanya berjalan ketika tidak ada lagi yang tersisa. Anda menginginkan NOT.
    • Membaca melebihi akhir karena perulangan tidak memiliki pengaman EOF.
    • Membuka FOR READ file yang belum pernah ditulis.
  • 11 A-Level 9618: what changes · ⁨A-Level 9618: perubahan apa saja⁩
    11.1

    Tata letak dan operator

    English

    Cambridge publishes two pseudocode specifications, and they disagree. Everything in topics 1-10 is IGCSE 0478. A-Level 9618 changes the details below — and a student who studies both loses marks by writing one in the other's paper.

    IGCSE 0478 A-Level 9618
    THEN on its own line, indented 2 on the IF line
    constants CONSTANT Pi ← 3.142 CONSTANT Pi = 3.142
    DIV / MOD functions: DIV(10, 3) operators: 10 DIV 3
    joining strings commas in OUTPUT the & operator
    identifiers must start with a capital mixed case allowed 混合大小写
    NEXT must name its counter good practice
    ROUND, RANDOM() both published neither exists

    This is the 0478 form, and it runs here:

    The 9618 form of the same algorithm is written like this — note THEN on the IF line, = in the CONSTANT, and MOD between its two values:

    ⚠️ Which one is right depends only on which paper you are sitting. Neither is a mistake; each is a mistake in the other's exam. The playground on this page runs the 0478 rules, so a 9618 construct is refused with a message naming the syllabus it belongs to — which is a quick way to check your own habits.

    Common mistakes

    • Writing THEN on the IF line in an 0478 answer, because that is what a textbook or a website showed. It is the single commonest cross-syllabus slip.
    • 10 MOD 3 in 0478. There it is MOD(10, 3).
    • ROUND(3.14159, 2) in a 9618 answer. 9618 has no ROUND at all.
    Bahasa Indonesia

    Cambridge menerbitkan dua spesifikasi pseudocode, dan mereka berbeda. Segalanya dalam topik 1-10 adalah IGCSE 0478. A-Level 9618 mengubah detail di bawah ini — dan seorang siswa yang mempelajari keduanya kehilangan nilai dengan menulis satu di kertas ujian yang lain.

    IGCSE 0478 A-Level 9618
    THEN pada baris sendiri, menjorok 2 pada IF baris
    konstanta CONSTANT Pi ← 3.142 CONSTANT Pi = 3.142
    DIV / MOD fungsi: DIV(10, 3) operator: 10 DIV 3
    menggabungkan string koma dalam OUTPUT operator &
    pengenal harus dimulai dengan huruf besar huruf campuran diperbolehkan
    NEXT harus memberi nama pencacahnya praktik baik
    ROUND, RANDOM() keduanya diterbitkan tidak ada yang ada

    Ini adalah bentuk 0478, dan ini berjalan di sini:

    DECLARE Total : REAL
    CONSTANT Rate ← 0.5
    Total ← 10
    IF MOD(7, 2) = 1
      THEN
        OUTPUT "Odd, half rate ", Total * Rate
    ENDIF
    

    Bentuk 9618 dari algoritma yang sama ditulis seperti ini — perhatikan THEN pada IF baris, = di dalam CONSTANT, dan MOD di antara dua nilainya:

    DECLARE Total : REAL
    CONSTANT Rate = 0.5
    Total ← 10
    IF 7 MOD 2 = 1 THEN
        OUTPUT "Odd, half rate " & STR(Total * Rate)
    ENDIF
    

    ⚠️ Mana yang benar hanya bergantung pada kertas ujian mana yang Anda kerjakan. Tidak ada yang merupakan kesalahan; setiapnya merupakan kesalahan dalam ujian yang lain. Playground di halaman ini menjalankan aturan 0478, sehingga konstruk 9618 ditolak dengan pesan yang menyebutkan kurikulum yang dimilikinya — yang merupakan cara cepat untuk memeriksa kebiasaan Anda sendiri.

    Kesalahan umum

    • Menulis THEN pada IF baris dalam jawaban 0478, karena itulah yang ditunjukkan buku teks atau situs web. Ini adalah kesalahan lintas-kurikulum tunggal yang paling umum.
    • 10 MOD 3 dalam 0478. Di sana itu adalah MOD(10, 3).
    • ROUND(3.14159, 2) dalam jawaban 9618. 9618 tidak memiliki ROUND sama sekali.
    11.2

    Parameter: BYVAL dan BYREF

    English

    0478 passes every parameter by value 传值 (topic 9). 9618 lets you choose, and says so in the header.

    • BYVAL — the procedure gets a copy. Changing it changes nothing outside. This is the default when neither word is written.
    • BYREF — the procedure works on the caller's own variable 引用, so a change is visible after the call.

    This is 9618 only. It does not run here, and it is not accepted in an 0478 answer:

    The 0478 way to get a value back

    Use a FUNCTION and assign what it returns. This is the whole of the 0478 answer, and it runs:

    Common mistakes

    • Writing BYREF in an 0478 answer. The keyword is not in that syllabus.
    • Expecting a plain 9618 parameter to behave like BYREF. Without the word it is BYVAL.
    Bahasa Indonesia

    0478 meneruskan setiap parameter berdasarkan nilai (topik 9). 9618 membiarkan Anda memilih, dan mengatakannya di bagian header.

    • BYVAL — prosedur mendapatkan salinan. Mengubahnya tidak mengubah apa pun di luar. Ini adalah default ketika tidak ada kata yang ditulis.
    • BYREF — prosedur bekerja pada variabel pemanggil sendiri, sehingga perubahan terlihat setelah panggilan.

    Ini hanya 9618. Ini tidak berjalan di sini, dan tidak diterima dalam jawaban 0478:

    PROCEDURE Increase(BYREF Value : INTEGER)
        Value ← Value + 1
    ENDPROCEDURE
    
    DECLARE Count : INTEGER
    Count ← 5
    CALL Increase(Count)
    OUTPUT Count      // prints 6
    

    Cara 0478 untuk mendapatkan nilai kembali

    Gunakan FUNCTION dan tetapkan apa yang dikembalikan. Ini adalah keseluruhan jawaban 0478, dan ini berjalan:

    FUNCTION Increased(Value : INTEGER) RETURNS INTEGER
        RETURN Value + 1
    ENDFUNCTION
    
    DECLARE Count : INTEGER
    Count ← 5
    Count ← Increased(Count)
    OUTPUT Count
    

    Kesalahan umum

    • Menulis BYREF dalam jawaban 0478. Kata kunci ini tidak ada dalam kurikulum tersebut.
    • Mengharapkan parameter 9618 biasa berperilaku seperti BYREF. Tanpa kata itu, ia adalah BYVAL.
    11.3

    Record dan tipe lainnya

    English

    9618 has data structures 数据结构 that 0478 does not. You will meet them only at A-Level, and none of them runs under the 0478 rules of this page.

    A record 记录 groups fields of different types under one name (9618 section 4):

    An enumerated type 枚举类型 lists the only values a variable may take:

    9618 also adds a DATE data type, random-access files (OPENFILE … FOR RANDOM), pointers, and classes with CLASS … ENDCLASS for its object-oriented paper.

    ⚠️ 0478 has five data types and arrays, and nothing else (topic 2). A 0478 answer that declares a record is answering a different syllabus.

    Common mistakes

    • Using a record in an 0478 answer. Use parallel arrays instead — one array per field, sharing an index.
    • Thinking TYPE means the same as DECLARE. TYPE defines a new type; DECLARE creates a variable of an existing one.
    Bahasa Indonesia

    9618 memiliki struktur data yang tidak dimiliki 0478. Anda akan menemuinya hanya di A-Level, dan tidak satupun dari mereka berjalan di bawah aturan 0478 halaman ini.

    Catatan record mengelompokkan medan dengan tipe berbeda di bawah satu nama (bagian 9618 4):

    TYPE Student
        DECLARE Name : STRING
        DECLARE Mark : INTEGER
    ENDTYPE
    
    DECLARE Candidate : Student
    Candidate.Name ← "Mei"
    Candidate.Mark ← 91
    OUTPUT Candidate.Name, " scored ", Candidate.Mark
    

    Sebuah tipe terenumerasi mencantumkan hanya nilai yang mungkin diambil oleh sebuah variabel:

    TYPE Vehicle = (Car, Bus, Taxi)
    DECLARE MyRide : Vehicle
    MyRide ← Taxi
    

    9618 juga menambahkan tipe data DATE, file akses acak (OPENFILE … FOR RANDOM), pointer, dan kelas dengan CLASS … ENDCLASS untuk kertas berorientasi objeknya.

    ⚠️ 0478 memiliki lima tipe data dan array, dan tidak ada yang lain (topik 2). Jawaban 0478 yang mendeklarasikan record sedang menjawab kurikulum yang berbeda.

    Kesalahan umum

    • Menggunakan record dalam jawaban 0478. Gunakan array paralel sebagai gantinya — satu array per field, berbagi indeks.
    • Berpikir TYPE berarti sama dengan DECLARE. TYPE mendefinisikan tipe baru; DECLARE membuat variabel dari yang sudah ada.

Log in or create account · ⁨Masuk atau buat akun⁩

IGCSE, A-Level & AP