Skip to content · ⁨דלג לתוכן⁩
Subjects · ⁨נושאים⁩
  • 1 Style and layout · ⁨סגנון ותצורה⁩
    1.1

    מילות מפתח וקפסלינג (Case)

    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.
    עברית

    פסאודוקוד היא השפה ש-Cambridge כותבת בה אלגוריתמים. זוהי לא שפה אמיתית שפת תכנות — אף מחשב אינו מיישר אותה — אך המבחן מסמן אותה כאילו כן, ולכן הפורמט חשוב.

    כל מילת מפתח נכתבת באותיות גדולות: IF, REPEAT, PROCEDURE, OUTPUT.

    DECLARE Count : INTEGER
    Count ← 3
    IF Count > 0
      THEN
        OUTPUT "Ready"
    ENDIF
    
    • כתוב IF, לעולם לא if או If.
    • מילת מפתח לעולם לא יכולה לשמש שם משלך: אין לך לקרוא למשתנה FOR.

    שגיאות נפוצות

    • מילות מפתח באותיות קטנות. תוכנית הניקוד מצפה לאותיות גדולות.
    • המצאת מילות מפתח. רק המילים בסיכום חומר זה מופיעות בתוכנית הלימודים.
    1.2

    זיהויים (Identifiers)

    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.
    עברית

    זיהוי הוא השם שמעניקים למשתנה, לקבוע, לפרוצדורה או לפונקציה.

    DECLARE NumberOfPlayers : INTEGER
    NumberOfPlayers ← 4
    OUTPUT NumberOfPlayers
    

    ארבע כללים, כולם מתוכנית הלימודים:

    • הוא חייב להתחיל באות גדולה: Count, לא count.
    • הוא יכול להכיל רק אותיות ומספרים — אין רוחב, אין סימני פיסוק.
    • הוא אינו רגיש לגודל אותיות, ולכן Count ו-COUNT הם אותו שם. בחר ספelling אחת והתמיד בה.
    • גרם לו להיות בעל משמעות. NumberOfPlayers זוכה לתשומת לב הקורא; N לא.

    שגיאות נפוצות

    • count ← 0 — התחלה באות קטנה. 0478 דורשת אות גדולה.
    • Number of players — רוחבות אינן מותרות בתוך שם.
    1.3

    הערות ותחביט (Comments and Indentation)

    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.
    עברית

    הערה מתחילה עם // ונמשכת עד לסוף השורה. היא מיועדת לאדם קורא; האלגוריתם מתעלם ממנו.

    // 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
    

    הזזה מראה אילו פקודות נמצאות בתוך מבנה. הסיילבוס דורש ארבע רוחבים לכל רמה, עם יוצא מכלל אחד שחובה לדעת:

    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 ו-ELSE מוזזים שניים רוחבים מה-IF שלהם.
    • הפקודות מתחת להן מוזזות ארבעה רוחבים.
    • ENDIF מיושר תחת ה-IF שלו.

    שגיאות נפוצות

    • חתימת THEN בשורת ה-IF. זהו סגנון A-Level 9618 — ראה נושא 11.
    • ללא הזזה כלל. המדריך חייב לראות את המבנה כדי לתת נקודות על המבנה.
  • 2 Variables, constants and types · ⁨משתנים, קבועים וסוגים⁩
    2.1

    חמישה סוגי נתונים

    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.
    עברית

    pseudocode 0478 כולל בדיוק חמישה סוגי נתונים. לימוד הרשימה — זיהוי סוג שאינו מצוי בה מאבד את הנקודה.

    סוג אחסון דוגמה
    INTEGER מספר שלם 42, -7
    REAL מספר עם חלק עשרוני 3.14, -0.5
    CHAR תווית בודדת 'A'
    STRING רצף של תוויות "Hello"
    BOOLEAN TRUE או 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 כתוב בתוך תנווי כפולים, ו-CHAR בתוך תנווי יחידים.
    • TRUE ו-FALSE הם מילות מפתח, ולכן נכתבות באותיות גדולות וללא תנוויים.

    שגיאות נפוצות

    • כתיבת FLOAT, DOUBLE או BOOL. אלו שפות אמיתיות, ולא הסיילבוס הזה.
    • 'Hello' למחרוזת — תנווי יחידים מחזיקים רק תווית אחת.
    2.2

    DECLARE

    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.
    עברית

    DECLARE מציין משתנה ומקבע את סוגו לפני השימוש בו.

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

    מספר שמות מאותו סוג נכתבים בשורה אחת, מופרדים בפסיקים:

    DECLARE Length, Width, Area : INTEGER
    Length ← 6
    Width ← 4
    Area ← Length * Width
    OUTPUT "Area = ", Area
    
    • הצהיר משתנה לפני שימושו הראשון, בקצה העליון של האלגוריתם.
    • למשתנה יש סוג קבוע לאורך כל האלגוריתם — INTEGER לעולם אינו מכיל "Mei".

    שגיאות נפוצות

    • השמטת הנקודתיים: DECLARE Counter INTEGER.
    • שימוש במשתנה שלא הוכרז מעולם. בבחינה מצופים ההצהרה.
    2.3

    קבוע

    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.
    עברית

    קבוע הוא ערך הקבוע לכל אורך האלגוריתם — שיעור מס, π, גודל כיתה. היכנות שלו פעם אחת פירושה ששינוי נעשה במקום אחד בלבד.

    CONSTANT <identifier> ← <value>
    
    CONSTANT TaxRate ← 0.20
    DECLARE Price, Tax : REAL
    Price ← 50.00
    Tax ← Price * TaxRate
    OUTPUT "Tax to pay: ", Tax
    
    • ב-0478 משתמשים בחץ, בדיוק כמו בהצבה.
    • הערך חייב להיות ליטרלי — מספר, מחרוזת, TRUE — לעולם לא חישוב ולעולם לא משתנה אחר.
    • לקבוע לעולם לא ניתן להעניק ערך חדש לאחר מכן. זוהי המהות שלו.

    שגיאות נפוצות

    • CONSTANT TaxRate = 0.20. צורת ה-= היא הכתיב המקובל ב-A-Level 9618 — ראו נושא 11.
    • הצבה לקבוע באזור מאוחר יותר באלגוריתם.
  • 3 Assignment, input and output · ⁨הקצה, כניסה ויציאה⁩
    3.1

    חץ ההצבה

    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.
    עברית

    מפעיל ההערכה הוא ←, חץ המצביע שמאלה. הוא פירושו לקחת את הערך שבצד הימין ולהניח אותו בקופסה שבצד שמאל.

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

    Counter ← Counter + 1 נראה בלתי אפשרי כמתמטיקה ורגיל כהוראה: חשב את הצד הימני עם הערך שקופסה מחזיקה כעת, ואז אחסן את התוצאה חזרה.

    • אם אינך יכול להקליד ←, כתוב <-. שניהם מתקבלים, כאן ובמבחן.
    • הצד השמאלי חייב להיות מזהה יחיד או אלמנט בודד במערך. לעולם לא חישוב.

    שגיאות נפוצות

    • Counter = 0. בפסדוקוד = הוא השוואה, ולא הקצה — והוא עלול לבלבל את שני האלמנטים; זהו הטעות הסינטקטית הנפוצה ביותר במבחן זה.
    • Counter + 1 ← Counter. החץ מצביע על הדבר שנשתנה.
    3.2

    קלט ופלט

    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.
    עברית

    INPUT קורא ערך מהמשתמש לתוך משתנה. OUTPUT מציג ערך אחד או יותר.

    DECLARE Name : STRING
    DECLARE Age : INTEGER
    INPUT Name
    INPUT Age
    OUTPUT "Hello ", Name
    OUTPUT "Next year you will be ", Age + 1
    
    • INPUT לוקח בדיוק משתנה אחד, ומשתנה זה חייב להיות כבר מוגדר.
    • OUTPUT לוקח מספר כלשהר של פריטים המופרדים ב-פסיקים; הם מודפעים אחד אחרי השני ללא דבר ביניהם.
    • רצף תווים ליטרלי בתוך OUTPUT נכתב בתוך סימני ציטוט כפולים; למשתנה אין.

    הפעלת משתמש

    הצבת INPUT לבדה אינה מציגה דבר על המסך, ולכן אלגוריתם טוב מדפיס הודעת הבהרה תחילה:

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

    שגיאות נפוצות

    • INPUT "Enter a number", X — ההודעה היא הודעת OUTPUT נפרדת.
    • OUTPUT "Total: " + Total. 0478 מחבר פריטי פלט בפסיק; + הוא ביטוי אריתמטי ו & שייך ל-A-Level 9618 (נושא 11).
    • שכחת סימני ציטוט: OUTPUT Hello מחפש משתנה בשם Hello.
  • 4 Arithmetic and logic · ⁨אריתמטיקה ולוגיקה⁩
    4.1

    算术运算符

    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 /.
    עברית

    חמישה פעולות אריתמטיות, בסדר שבו משתמש בהן מחשבון.

    פעולה משמעות A הוא 7, B הוא 2
    + הוספה A + B הוא 9
    - חיסור A - B הוא 5
    * כפל A * B הוא 14
    / חלוקה A / B הוא 3.5
    ^ העלאה לחזקה A ^ B הוא 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
    

    סוגרי קובייה באים ראשונים, אחר כך ^, אחר כך * ו/, אחר כך + ו- — עדיפויות רגילות . השתמש בסוגרי קובייה כל פעם שהקורא יצטרך לעצור ולחשוב.

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

    שגיאות נפוצות

    • צפוי ש/ ייתן מספר שלם. 7 / 2 הוא 3.5, לא 3; עבור מספר שלם השתמש ב- DIV.
    • כתיבת A x B או A ÷ B. השתמש ב* וב-/.
    4.2

    DIV ו-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.
    עברית

    חלוקה שלמה עונה לשתי שאלות שונות, ו-0478 כותב את אחת מהן כ- פונקציה, עם שני הערכים בתוך סוגרי קובייה.

    • DIV(a, b) — כמה פעמים b נכנס לa במספר שלם, ה-מנה.
    • MOD(a, b) — מה נשאר, ה-שארית.
    OUTPUT DIV(10, 3)
    OUTPUT MOD(10, 3)
    OUTPUT DIV(17, 5)
    OUTPUT MOD(17, 5)
    

    שתי הפונקציות מקבלות שלמים וחוזרות על שלם.

    למה הן מיועדות

    MOD הוא המבחן הסטנדרטי ל-חלוקה מדויקת, מה שהופך אותו לדרך לשאול האם מספר הוא זוגי:

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

    DIV ממיר סך כולל ליחידות שלמות — שניות לדקות, פנסים ללירות:

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

    שגיאות נפוצות

    • 10 MOD 3. זהו הצורה של A-Level 9618; 0478 משתמש בצורת הפונקציה MOD(10, 3).
    • ערבוב השניים. DIV נותן את החלק השלם; MOD נותן את מה שלא התאים.
    4.3

    מופעי יחס ולוגיים

    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.
    עברית

    מופע יחסי משווה שני ערכים ומייצר TRUE או FALSE.

    מופע משמעות
    = שווה ל-
    <> אינו שווה ל-
    > גדול מ-
    < קטן מ-
    >= גדול או שווה ל-
    <= קטן או שווה ל-
    DECLARE Mark : INTEGER
    Mark ← 55
    OUTPUT "Pass: ", Mark >= 50
    OUTPUT "Perfect: ", Mark = 100
    OUTPUT "Not zero: ", Mark <> 0
    

    שלושת מופעי הלוגיקה מחברים תנאים:

    • AND — צריך ששני הצדדים יהיו TRUE.
    • OR — צריך שאחד לפחות מהצדדים יהיה TRUE.
    • NOT — הופך TRUE לFALSE ולהפך.
    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
    

    שגיאות נפוצות

    • <> הוא כאן סימן אי-שוויון — ולא !=, שמיועד לשפות אחרות.
    • IF Age >= 15 AND <= 18. לכל צד של AND יש צורך בהשוואה מלאה משלו: Age >= 15 AND Age <= 18.
    • כינוי זה בפתרון כ-מופעי השוואה. המונח בתוכנית לימודים הוא יחסי.
  • 5 String operations · ⁨פעולות על מחרוזות⁩
    5.1

    אורך, UCASE ו-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.
    עברית

    0478 מפרסם ארבעה פונקציות למחרוזות ואין אחרות. שלושה מהן כאן.

    LENGTH(<identifier>) מחזיר כמה תווים מחרוזת אחת מכילה — כולל רווחים.

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

    UCASE(<identifier>) מחזיר את המחרוזת באותיות גדולות, LCASE(<identifier>) באותיות קטנות אף אחד מהם אינו משנה את המקורי — כל אחד מהם מחזיר ערך חדש.

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

    השורה האחרונה עדיין מדפיסה Happy: כדי לשמור על השינוי חובה להחזיר אותו לאחסון.

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

    השוואה ללא דאגה לגבי סוג האותיות

    UCASE בשני הצדדים היא הדרך הסטנדרטית לקבלת תשובה בכל סגנון הכתיבה:

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

    שגיאות נפוצות

    • לצפות שUCASE(Name) ישנה את Name. החזיר את התוצאה לאחסון.
    • LENGTH סופר תווים, לא מילים. LENGTH("Happy Days") הוא 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.
    עברית

    SUBSTRING(<identifier>, <start>, <length>) שואב חלק ממחרוזת — החל מ- מיקום start, לוקח length תווים.

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

    ⚠️ הספירה מתחילה ב-1, לא ב-0. התוו הראשון של "Happy Days" נמצא במיקום 1.

    לקיחת תוו אחד, והשאר

    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 בתוך SUBSTRING הוא מה שמאפשר לשתי השורות האחרונות לעבוד עם מילת גודל כלשהו — קידוד קשיב (hard-coding) של 8 ישבור את הדבר ברגע שהמילה תשתנה.

    קריאת מחרוזת תוו אחר תוו

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

    שגיאות נפוצות

    • החל מ-0. מיקום 1 הוא התוו הראשון.
    • קריאה של הארגומנט השלישי כ-מיקום סיום. זהו ספירה — SUBSTRING(W, 2, 3) לוקח שלושה תווים החל מהשני.
    • בקשה לתווים יותר ממה שהמחרוזת מכילה.
  • 6 Selection · ⁨בחירה⁩
    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.
    עברית

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

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

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

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

    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.

    DECLARE Mark : INTEGER
    Mark ← 85
    IF Mark >= 80
      THEN
        OUTPUT "Distinction"
      ELSE
        IF Mark >= 50
          THEN
            OUTPUT "Pass"
          ELSE
            OUTPUT "Fail"
        ENDIF
    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.
    6.2

    CASE OF

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

    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
    
    • 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

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

    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.
  • 7 Iteration · ⁨איטרציה ⟦number⟧⁩
    7.1

    FOR, לולאת הספירה

    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.
    עברית

    איטרציה חוזרת על פקודות. השתמש בלולאת FOR כאשר ידוע לך לפני שמתחילים כמה פעמים — לולאת המבוקלת-ספירה.

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

    המונה מתחיל בערך הראשון, והלולאה פועלת פעם אחת עבור כל ערך עד ול-כולל הערך השני. 1 TO 5 רץ חמש פעמים.

    STEP (צעד)

    STEP משנה את גודל הקפיצה בכל פעם. צעד שלילי סופר לאחור.

    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
    

    סכומים מצטברים

    השימוש הנפוץ ביותר: משתנה מחוץ ללולאה אליו הלולאה מחברת.

    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 חייב להימצא לפני הלולאה. בתוכה הוא יאפס בכל מעבר.

    שגיאות נפוצות

    • NEXT שמירת שם שג错误的 למונה. לולאות מקוננות חייבות להסתדר בסדר הפוך מהפתיחה שלהן, כלומר לולאת ה-NEXT הפנימית נסגרת תחילה.
    • הנחה ש-1 TO 5 רץ ארבע פעמים. שני הקצוות כלולים.
    • שינוי המונה בתוך הלולאה. תן ל-FOR להיות האחראי עליו.
    7.2

    WHILE, לולאת תנאי מקדים

    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.
    עברית

    לולאת WHILE בודקת לפני כל מעבר, ולכן יכולה לרוץ אפס פעמים. השתמש בה כאשר מספר ההשניות תלוי בדבר מה שקורה בזמן ביצוע הלולאה.

    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
    

    משהו בתוך הלולאה חייב לבסוף לגרום לתנאי להיות שג错误的, או שהלולאה לעולם לא תסתיים — לולאת אינסופית.

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

    שגיאות נפוצות

    • שוכחים לשנות את המשתנה עליו הבדיקה בודקת, מה שמוביל ללולאה אינסופית.
    • משאירים מחוץ DO או ENDWHILE.
    • משתמשים ב-WHILE כשהספירה ידועה. ה-FOR אומר בדיוק מה intended in one line.
    7.3

    REPEAT, לולאת תנאי סיום

    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.
    עברית

    לולאת REPEAT בודקת אחרי כל עוברת, ולכן היא תמיד רצה לפחות פעם אחת.

    REPEAT
        <statements>
    UNTIL <condition>
    

    ה"לפחות פעם אחת" הוא הסיבה לבחור בה: באימות קלט, שבו חובה לשאול לפני שתוכל להעריך את התשובה.

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

    ⚠️ UNTIL מגדיר את התנאי ש-מפסק את הלולאה; WHILE מגדיר את那个 ש- משאיר אותה פועלת. הם הפוכים זה לזה, והחלפתם היא השגיאה הקלאסית.

    בדיקה רצה לפחות פעם? משמעות התנאי
    FOR מונה לא (טווח ריק רץ אפס פעמים) —
    WHILE לפני לא המשך לפעול כל עוד זה נכון
    REPEAT אחרי כן עצור כאשר זה הופך לנכון

    שגיאות נפוצות

    • כותבים UNTIL Number < 100 כשמתכוונים ל-UNTIL Number >= 100.
    • משתמשים ב-REPEAT כשהגוף חייב להיות יכול לרצוע אפס פעמים.
    • שוכחים שה-UNTIL מסיים את המבנה — אין ENDREPEAT.
  • 8 Arrays · ⁨מערכים⁩
    8.1

    מערכים חד-ממדיים

    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.
    עברית

    מערכת מחזיקה ערכים רבים מאותו סוג תחת שם אחד. כל ערך יושב ב- מדד.

    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]
    
    • הגבולות נכתבים lower:upper, ו-שניהם כלולים — [1:3] יש שלושה מרכיבים.
    • מערכים של Cambridge מתחילים ב-1, לא ב-0.
    • לכל מרכיב יש אותו סוג, המוגדר על ידי OF.

    מילוי וקריאה באמצעות לולאת FOR

    מערך ולולאת ספירה משלימים זה את זה: הסופר הוא האינדקס.

    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
    

    מציאת הערך הגדול ביותר

    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
    

    התחל Largest מהמרכיב הראשון, לעולם לא מ-0—עם נתונים שליליים, 0 ינצח וה התשובה תהיה שגויה.

    שגיאות נפוצות

    • קריאת Scores[6] ממעerged הוכרז כ[1:5]. זה מחוץ לגבולות.
    • כתיבת Scores(3). מערכים משתמשים בסוגריים מרובעים; סוגריים עגולים קוראים לפונקציה.
    • הכרזה של [1:5] ולאחר מכן לולאה 0 TO 4.
    8.2

    מערכים דו-ממדיים

    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.
    עברית

    מערך בעל ממד 2 הוא טבלה: שורות ועמודות, שני אינדקסים.

    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]
    

    האינדקסהראשון הוא השורה, והשני הוא העמודה.

    לולאות פנימיות מסיירות בטבלה

    לולאה אחת לממד. הלולאה הפנימה גומרת שורה שלמה לפני שהחוץ ממשיכה.

    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
    

    ⚠️ הלולאה הפנימה NEXT סוגרת את הסופרהפנימי. NEXT Row לפני NEXT Col חוצה את הלולאות ומפיק תוצאה אנונית — סימן מזהה זאת במיידי.

    שגיאות נפוצות

    • החלפת שורה ועמודה. Grid[2,3] היא שורה 2, עמודה 3.
    • Grid[Row][Col]. Cambridge כותבת זוג אחד של סוגריים עם פסיק בתוך.
    • חריגה מעבר ל NEXT, כפי שצוין לעיל.
  • 9 Procedures and functions · ⁨פרוצדורות ופונקציות⁩
    9.1

    הליך וקריאה

    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.
    עברית

    הליך הוא בלוק ממונה של פקודות. כתיבתם פעם אחת וקריאתם מ- במספר מקומות יש פירוק — הרעיון שהכל של נייר 2 נשען עליו.

    PROCEDURE <identifier>
        <statements>
    ENDPROCEDURE
    

    הליך נקרא לפי שמו, באמצעות המילה CALL:

    PROCEDURE DefaultLine
        OUTPUT "------------------------------"
    ENDPROCEDURE
    
    CALL DefaultLine
    OUTPUT "Report"
    CALL DefaultLine
    
    • ההגדרה אינה עושה דבר בעצמה. רק CALL מפעיל אותה.
    • הבקרה חוזרת לקו שמ לאחר ה CALL כאשר ההליך מסתיים.
    • ההליך אינו מחזיר ערך. אם תרצה ערך כזה, עליך להשתמש בפונקציה.

    שגיאות נפוצות

    • כתיבת DefaultLine בשורה עצמאית כדי להפעיל אותו. 0478 דורש CALL.
    • השמטת ה ENDPROCEDURE.
    • ציפייה לכך שהמשתנים של ההליך יקיימו מחוץ לו.
    9.2

    פונקציה והחזרת ערך

    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.
    עברית

    פונקציה היא הליך שמחזיר ערך. היא נכתבת שם שבו הערך נדרש — בתוך ה OUTPUT, מימין לאסIGNEDMENT, בתוך תנאי.

    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> בראש הפונקציה אומר מה סוג הערך הנחזר. זה לא אופציונלי.
    • ה RETURN מגמר את הפונקציה מיד — אין ביצועים לאחריו.
    • פונקציה מעולם אינה נקראת עם ה CALL; היא משמשת שם שבה ערכה מתאים.

    פונקציה בתוך תנאי

    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
    

    שגיאות נפוצות

    • ה CALL SumSquare(10, 20) — שמ丢弃 (מ拋弃) את התשובה.
    • פונקציה ללא ה RETURN בחלק ממסלוליה. כל מסלול יציאה חייב להחזיר ערך.
    • השארת RETURNS INTEGER מחוץ לכותרת.
    9.3

    פרמטרים

    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.
    עברית

    פרמטר הוא ערך המועבר לפרוצדורה או לפונקציה בעת הקריאה שלה. לכל אחד ישנו סוג משלו.

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

    מספר פרמטרים מופרדים בפסיקים, והסדר בהקראה חייב להתאים לסדר בהגדרה:

    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)
    

    ⚠️ ב-0478 כל פרמטר מועבר בערך: הפרוצדורה עובדת על העתק, ולכן שינוי בו בתוך הפונקציה אינו משנה דבר מחוץ לה. כדי לקבל ערך בחזרה, השתמש ב-FUNCTION ו- RETURN אותו. (A-Level 9618 מוסיף BYREF לתכונה אחרת — נושא 11.)

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

    שגיאות נפוצות

    • PROCEDURE Line(Size) ללא סוג.
    • קריאה עם הארגומנטים בסדר שגוי — Greet(2, "Mei") זהו שגיאת סוג.
    • ציפיה שהפרוצדורה תשנה את המשתנה שעברת לה. ב-0478 היא לא יכולה לעשות זאת.
  • 10 File handling · ⁨ניהול קבצים⁩
    10.1

    כתיבה לקובץ

    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.
    עברית

    קוב שומר נתונים לאחר סיום התוכנית. ב-0478 ישנם ארבע פקודות, וכל אחת מהן מונה את הקוב שעליו היא פועלת.

    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"
    

    שלושה מצבי קוב:

    מצב מה שהוא עושה
    WRITE יוצר קוב חדש — כל הנתון שקיים בו נאבד
    APPEND מוסיף בסוף מה שקיים
    READ קורא מתחילת הקוב
    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"
    
    • כל WRITEFILE כותב שורה אחת.
    • CLOSEFILE אינו אופציונלי. קובץ פתוח שאינו סגור עלול לאבד את הנתונים שעדיין מחכים להיכתב, וזהו תשובה סטנדרטית לבחינות. נכתבים, והדבר מהווה תשובה שגורה בבחנים.

    שגיאות נפוצות

    • FOR WRITE כאשר התכוונת ל-FOR APPEND, אשר מוחק את התוכן הקודם בשקט.
    • שכחת CLOSEFILE.
    • פתיחת קובץ שכבר פתוח.
    10.2

    קריאת קובץ עד לסוף

    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.
    עברית

    READFILE לוקח שורה אחת למשתנה. המשתנה חייב להיות מוגדר מראש.

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

    בדרך כלל אין ידע בכמות השורות, ולכן קוראים עד לסוף ה קובץ** — הדבר שמסמך הפונקציית 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") הוא TRUE ברגע שכל שורה נקראה, ולכן WHILE NOT EOF(...) משמעותו בעוד שיש עוד מה לקרוא.

    ספירה בזמן הקריאה

    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
    

    שגיאות נפוצות

    • לולאת WHILE EOF(...) — זו שהתאפשרת רק כאשר אין דבר שנותר. רוצים לולאת NOT.
    • קריאה מעבר לסוף בגלל העדר מגן EOF בלולאה.
    • פתיחת FOR READ קובץ שלא נכתב לעולם.
  • 11 A-Level 9618: what changes · ⁨A-Level 9618: מה השתנה⁩
    11.1

    Layout and operators

    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.
    עברית

    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:

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

    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:

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

    ⚠️ 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.
    11.2

    Parameters: BYVAL and 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.
    עברית

    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:

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

    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:

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

    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.
    11.3

    Records and other types

    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.
    עברית

    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):

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

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

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

    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.

Log in or create account · ⁨היכנס או צור חשבון⁩

IGCSE, A-Level & AP