Skip to content · ⁨الانتقال إلى المحتوى⁩

Algorithm Design and Problem-solving · ⁨تصميم الخوارزميات وحل المشكلات⁩

A-Level Computer Science · ⁨A-Level علوم الحاسوب⁩ · Topic 9 · ⁨الموضوع 9⁩

Video lesson for this topic · ⁨درس فيديو لهذا الموضوع⁩ Open the video page · ⁨افتح صفحة الفيديو⁩
14:52

التفكير الحسابي

هناك مهمة: بناء نظام لتشغيل مخزون متجر كامل — كل منتج، كل بيع، كل توصيل، كل تقرير. باعتبارها مشكلة واحدة ضخمة، فهي كبيرة جدًا لـ…

English narration · English + 中文 subtitles burned in · ⁨سرد باللغة الإنجليزية · ترجمة مدمجة بالإنجليزية + الصينية⁩

9.1

Computational thinking · ⁨التفكير الحسابي⁩

Syllabus · ⁨المنهج⁩
English
Candidates should be able to: Notes and guidance
Show an understanding of abstraction Need for and benefits of using abstraction Describe the purpose of abstraction Produce an abstract model of a system by only including essential details
Describe and use decomposition Break down problems into sub-problems leading to the concept of a program module (procedure / function)
العربية
يجب أن يكون المرشحون قادرين على: ملاحظات وإرشادات
إظهار فهم التجريد الحاجة وفوائد استخدام التجريد وصف الغرض من التجريد إنتاج نموذج مجرد لنظام عن طريق تضمين التفاصيل الأساسية فقط
وصف واستخدام التفكيك تقسيم المشكلات إلى مشكلات فرعية مما يؤدي إلى مفهوم وحدة البرنامج (إجراء / دالة)

Source: Cambridge International syllabus · ⁨المصدر: منهج كامبريدج الدولي⁩

English

Computational thinking 计算思维 is the set of mental tools for analysing a problem and designing a solution a computer can run. Two key ones are abstraction and decomposition.

Abstraction

Abstraction 抽象 means keeping the essential features of a problem and ignoring the irrelevant detail, giving a simpler model.

Examples:

  • a train-network map keeps the stations and lines but drops the geography.
  • a class in object-oriented programming keeps only the attributes and methods the system needs.
  • a function hides a piece of work behind a name.

A full model of any real problem would be too big to reason about, so abstraction is essential.

The examiner asks for the purpose of abstraction and for its benefits. Purpose: to produce a simpler model of a problem that contains only the details needed to solve it. Benefits: the problem is easier to understand and to program; the program is smaller and faster to write and test; the same model can be reused for similar problems. When you are asked to produce an abstract model of a system, list only the data and actions the task needs. For a school timetable that means the classes, rooms, teachers and periods; it does not mean the colour of the rooms or the age of the teachers.

Decomposition

Decomposition 分解 means breaking a large problem into smaller sub-problems, each easier to solve and tackled one at a time.

  1. find the main parts of the task.
  2. break each into smaller sub-tasks.
  3. continue until each is small enough to design directly.
  4. solve the small tasks and combine them.

For stock control: "manage stock" → "record sales", "record deliveries", "produce reports" → ("record sales") "look up product", "decrease stock count", "save the transaction". Decomposition makes big problems manageable, lets a team divide the work, and gives modular code — each module becomes a procedure 过程 or function.

"Explain why decomposition is used" is a three-mark question with a fixed shape. Give three separate benefits: each sub-problem 子问题 is small enough to design, code and test on its own; different programmers can work on different modules 模块 at the same time; a module that already exists (or a library routine) can be reused, and a fault is easier to find because it lies inside one module. A structure chart (topic 12) is the diagram of a decomposition: the program at the top, its modules beneath, and the data passed between them.

العربية

التفكير الحسابي هو مجموعة الأدوات الذهنية لـ تحليل مشكلة وتصميم حل يمكن للحاسوب تنفيذه. أهم أداة هما التجريد والتفكيك.

جزء من لغز مفقود
يكسر التفكير الحسابي مشكلة كبيرة إلى أجزاء أصغر أسهل — مثل حل لغز مفقود

التجريد

التجريد يعني الحفاظ على الخصائص الأساسية للمشكلة وتجاهل التفاصيل غير ذات الصلة، مما يعطي نموذجاً أبسط.

أمثلة:

  • خريطة شبكة قطارات تحافظ على المحطات والخطوط لكن تتجاهل الجغرافيا.
  • فئة في البرمجة كائنية تحافظ فقط على الصفات والأساليب التي يحتاجها النظام.
  • دالة تخفي جزءاً من العمل خلف اسم.

النموذج الكامل لأي مشكلة حقيقية سيكون كبيراً جداً للتفكير فيه، لذا فإن التجريد ضروري.

يطلب الممتحن الغرض من التجريد وفوائده. الغرض: إنتاج نموذج أبسط لمشكلة يحتوي فقط على التفاصيل اللازمة لحلها. الفوائد: تصبح المشكلة أسهل في الفهم والبرمجة؛ يكون البرنامج أصغر وأسرع في الكتابة والاختبار؛ يمكن إعادة استخدام نفس النموذج لمشاكل مماثلة. عندما يُطلب منك إنشاء نموذج مجرد لنظام، اذكر فقط البيانات والإجراءات التي تحتاجها المهمة. بالنسبة لجدول مدرسي يعني ذلك الصفوف والقاعات والمعلمين والفترات؛ لا يشمل لون القاعات أو عمر المعلمين.

التجريد يحول جغرافية حقيقية فوضوية (مسار متعرج مع مباني مبعثرة) إلى خريطة مترو نظيفة — دوائر محطات متساوية المسافة على خط مستقيم، مع الاحتفاظ بالمحطات والخطوط وتجاهل الجغرافيا
يحافظ التجريد على الأساسيات (المحطات والخطوط) ويتخلى عن التفاصيل غير ذات الصلة (الجغرافيا)

التفكيك

التفكيك يعني تقسيم مشكلة كبيرة إلى مشاكل فرعية أصغر، كل منها أسهل في الحل ومعالجة واحدة تلو الأخرى.

  1. تحديد الأجزاء الرئيسية للمهمة.
  2. تقسيم كل جزء إلى مهام فرعية أصغر.
  3. الاستمرار حتى يصبح كل جزء صغيرًا بما يكفي للتصميم مباشرة.
  4. حل المهام الصغيرة ودمجها معًا.

للتحكم في المخزون: "إدارة المخزون" → "تسجيل المبيعات"، "تسجيل الواردات"، "إعداد التقارير" → ("تسجيل المبيعات") "البحث عن المنتج"، "تقليل عدد المخزون"، "حفظ المعاملة". يجعل التفكيك المشاكل الكبيرة قابلة للإدارة، ويسمح للفريق بتقسيم العمل، ويوفر كودًا معياريًا — حيث يصبح كل وحدة إجراءً أو دالة.

"اشرح سبب استخدام التفكيك" سؤال بثلاث درجات له شكل ثابت. قدم ثلاث فوائد منفصلة: كل مشكلة فرعية صغيرة بما يكفي للتصميم والكتابة والاختبار بشكل مستقل؛ يمكن لمبرمجين مختلفين العمل على وحدات مختلفة في نفس الوقت؛ يمكن إعادة استخدام وحدة موجودة مسبقًا (أو إجراء مكتبة)، وسهولة العثور على الخطأ لأنه يقع داخل وحدة واحدة. مخطط البنية (الموضوع 12) هو رسم توضيحي للتفكيك: البرنامج في الأعلى، ووحداته تحته، والبيانات المنتقلة بينهما.

شجرة رأسها "إدارة المخزون" تتفرع إلى الوحدات "تسجيل المبيعات"، "تسليم البضائع" و"إعداد التقارير"، وتتفرع "تسجيل المبيعات" إلى المهام الفرعية "البحث عن المنتج"، "تقليل عدد المخزون" و"حفظ المعاملة"
تحليل برنامج إلى وحدات وفرعيات
Explore · ⁨استكشف⁩

Solving a problem the computational way · ⁨حل مشكلة بالطريقة الحسابية⁩

Step through the four cornerstones in the order you'd use them — break the problem down, spot what repeats, strip it to essentials, then write the steps. · ⁨مرر بالركائز الأربع بالترتيب الذي ستستخدمه — حلل المشكلة، حدد ما يتكرر، استبعد التفاصيل غير الضرورية، ثم اكتب الخطوات.⁩

Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
computational thinking/ˌkɒmpjuːˈteɪʃənl ˈθɪŋkɪŋ/ التفكير الحسابي
abstraction/əbˈstrækʃn/ التجريد
decomposition/ˌdiːkɒmpəˈzɪʃn/ تحلل
sub-problem/sʌb ˈprɒbləm/ مشكلة فرعية
procedure/prəˈsiːdʒə/ إجراء
modules/ˈmɒdjuːlz/ الوحدات
algorithm/ˈælɡərɪθəm/ خوارزمية
sequence/ˈsiːkwəns/ تسلسل
unambiguous/ʌnæmˈbɪɡjuːəs/ غير غامض
deterministic/dɪˌtɜːmɪˈnɪstɪk/ حتمي
9.2

Algorithms · ⁨الخوارزميات⁩

Syllabus · ⁨المنهج⁩
English
Candidates should be able to: Notes and guidance
Show understanding that an algorithm is a solution to a problem expressed as a sequence of defined steps
Use suitable identifier names for the representation of data used by a problem and represent these using an identifier table
Write pseudocode that contains input, process and output
Write pseudocode using the three basic constructs of sequence, selection and iteration (repetition)
Document a simple algorithm using a structured English description, a flowchart or pseudocode
Write pseudocode from: • a structured English description • a flowchart
Draw a flowchart from: • a structured English description • pseudocode
Describe and use the process of stepwise refinement to express an algorithm to a level of detail from which the task may be programmed
Use logic statements to define parts of an algorithm solution
العربية
يجب أن يكون المرشحون قادرين على: ملاحظات وإرشادات
إظهار فهم أن الخوارزمية هي حل لمشكلة مُعبّر عنها كمتسلسلة من الخطوات المحددة
وصف واستخدام أسماء معرفات مناسبة لتمثيل البيانات المستخدمة في مشكلة ما وتمثيلها باستخدام جدول المعرفات
كتابة رمز زائف يحتوي على إدخال ومعالجة وإخراج
اكتب كوداً زائفاً باستخدام البناءات الأساسية الثلاث: التسلسل، الاختيار، والتكرار (الإعادة)
توثيق خوارزمية بسيطة باستخدام وصف بالإنجليزية المنظمة، مخطط انسيابي أو رمز زائف
كتابة رمز زائف من: • وصف بالإنجليزية المنظمة • مخطط انسيابي
رسم مخطط انسيابي من: • وصف بالإنجليزية المنظمة • رمز زائف
صف واستخدم عملية التحسين التدريجي للتعبير عن خوارزمية على مستوى من التفاصيل يسمح ببرمجة المهمة
استخدم عبارات منطقية لتحديد أجزاء حل الخوارزمية

Source: Cambridge International syllabus · ⁨المصدر: منهج كامبريدج الدولي⁩

English
Bubble sort, pass by pass

An algorithm 算法 is a solution expressed as a sequence of defined steps. Each step is unambiguous 无歧义 (one meaning), deterministic 确定性 (same input → same output), finite (the steps end), and effective (each can be done). An algorithm says what to do, independent of the programming language used to implement it.

العربية
فرز الفقاعات، مررة بمررة

الخوارزمية هي حل مُعبَّر عنه كتسلسل من الخطوات المحددة. كل خطوة غير غامضة (بمعنى واحد)، حتمية (نفس المدخلات → نفس المخرجات)، منتهية (تنتهي الخطوات)، وفعالة (يمكن تنفيذ كل منها). تخبر الخوارزمية بماذا تفعل، بغض النظر عن لغة البرمجة المستخدمة لتنفيذها.

Explore · ⁨استكشف⁩

Selection: follow the IF / ELSE branches · ⁨الاختيار: اتبع فروع IF / ELSE⁩

Drag the score and watch which branch runs. Selection tests each condition in turn and takes the FIRST one that is true — that is how IF … ELSE IF … ELSE works. · ⁨اسحب الدرجة ولاحظ أي فرع يتم تنفيذه. يختبر الاختيار كل شرط على حدة ويأخذ الأول الذي يكون صحيحاً — هكذا تعمل بنية IF … ELSE IF … ELSE.⁩

Watch lesson · ⁨شاهد الدرس⁩
9.2

Identifier table · ⁨جدول المعرفات⁩

English

When you start an algorithm, list every piece of data in an identifier table 标识符表 — its identifier 标识符 (the variable 变量 name), data type 数据类型, and description. The exam's table has exactly these three columns:

Identifier Data type Description
Category STRING the product category
SaleDate DATE when the item was sold
ItemCost REAL cost of the item
InStock BOOLEAN TRUE if in stock
Sales ARRAY[1:30] OF REAL the last 30 daily sales totals

Use descriptive names (ItemCost, not x): an identifier starts with a letter, contains no spaces, and is written the same way every time it appears. Common types are INTEGER, REAL, STRING, CHAR, BOOLEAN, DATE, plus arrays. The table forces you to name every piece of data before writing code, and a "complete the identifier table" question gives one mark for each correct data type or description, so write the type exactly as the pseudocode guide does.

العربية

عند بدء خوارزمية، ادرج كل قطعة بيانات في جدول معرفات — معرفها (اسم المتغير)، نوع البيانات، والوصف. يحتوي جدول الامتحان على هذه الأعمدة الثلاثة بالضبط:

المعرف نوع البيانات الوصف
Category STRING فئة المنتج
SaleDate DATE وقت بيع العنصر
ItemCost REAL تكلفة العنصر
InStock BOOLEAN TRUE إذا كان في المخزون
Sales ARRAY[1:30] OF REAL إجمالي مبيعات اليوم الأخيرة الـ 30

استخدم أسماء وصفية (ItemCost، وليس x): يبدأ المعرف بحرف، ولا يحتوي على مسافات، ويُكتب بنفس الطريقة في كل مرة يظهر فيها. الأنواع الشائعة هي INTEGER، REAL، STRING، CHAR، BOOLEAN، DATE، بالإضافة إلى المصفوفات. يفرض الجدول عليك تسمية كل قطعة بيانات قبل كتابة الكود، ويعطي سؤال "أكمل جدول المعرفات" درجة واحدة لكل نوع بيانات أو وصف صحيح، لذا اكتب النوع تمامًا كما يفعل دليل الكود الوهمي.

جدول معرفات يسرد كل متغير مع اسمه ونوع البيانات ووصفه، على سبيل المثال ItemCost كنوع REAL لتكلفة العنصر *يحدد جدول المعرفات أسماء جميع قطع البيانات قبل كتابة الكود

Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
identifier table/aɪˈdentɪfaɪə ˈteɪbl/ جدول المعرفات
identifier/aɪˈdentɪfaɪə/ معرف
Boolean/ˈbuːlɪən/ Boolean
9.2

Pseudocode — the three basic constructs · ⁨الكود الوهمي — البناءان الأساسيان⁩

English

Pseudocode 伪代码 is a structured, language-neutral way to describe algorithms.

1. Sequence

Steps run one after another (sequence 顺序):

2. Selection

A choice of which steps run, based on a condition (selection 选择):

For more options, use CASE OF ... ENDCASE.

3. Iteration

Repeating a block (iteration 迭代, a loop 循环):

A WHILE loop tests the condition before each pass (may run zero times); a REPEAT...UNTIL loop tests after each pass (always runs at least once).

Choosing the loop is itself a mark: FOR when you know how many times (a count-controlled loop 计数循环); WHILE when the loop might not run at all (a pre-condition loop 前测循环); REPEAT ... UNTIL when it must run at least once, as in validating an input (a post-condition loop 后测循环). A "describe the iteration construct" answer names the construct, says where the condition is tested, and gives the consequence (zero times or at least once).

Common operations

  • assignment 赋值: x ← 5 (an arrow; = is for comparison).
  • input/output: INPUT variable, OUTPUT expression.
  • comparisons =, <>, <, >, <=, >=; logic AND, OR, NOT.
  • arithmetic + - * /, plus DIV (integer division) and MOD (remainder).
  • strings: LENGTH, LEFT, RIGHT, MID, and & for concatenation 拼接 (joining).

The pseudocode the exam expects

Every pseudocode answer is marked against Cambridge's published pseudocode guide. Write these forms exactly:

Construct Pseudocode
Variable DECLARE Total : INTEGER
Array DECLARE Marks : ARRAY[1:30] OF REAL
Constant CONSTANT MaxTries = 3
Assignment Total ← Total + Value
Input / output INPUT Name
OUTPUT "Hello ", Name
Selection CASE OF Choice
1 : OUTPUT "Add"
OTHERWISE OUTPUT "Error"
ENDCASE
FOR loop FOR i ← 1 TO 10 STEP 2 ... NEXT i
WHILE loop WHILE Total < 100 DO ... ENDWHILE
REPEAT loop REPEAT ... UNTIL Mark >= 0
Integer arithmetic 17 DIV 5 = 3
17 MOD 5 = 2
Strings LENGTH(S), LEFT(S, 3), RIGHT(S, 2)
MID(S, 2, 4), UCASE(S), LCASE(S)
Conversions INT(3.7) = 3, NUM_TO_STR(12)
STR_TO_NUM("4.5"), ASC('A') = 65, CHR(66) = 'B'
Random RAND(100)
INT(RAND(100)) + 1

RAND(100) gives a real number from 0 up to (but not including) 100. INT(RAND(100)) + 1 gives an integer from 1 to 100.

Two habits earn marks on every question: declare every variable you use, with the type from your identifier table, and initialise 初始化 every counter 计数器 and total (Count ← 0, Total ← 0) before the loop that changes it.

Input → Process → Output

Every program follows this shape:

Listing the inputs and outputs first makes the algorithm cleaner.

Worked example. Write pseudocode that inputs 100 integers and outputs how many of them, and the total of those, that lie between 10 and 20 inclusive.

Identifier table: Count : INTEGER (loop counter), Value : INTEGER (the integer just input), InRange : INTEGER (how many were in range), Total : INTEGER (their sum).

If the question then asks you to "identify two constructs and state how each is used", answer in the same shape: iteration, the FOR loop, repeats the input 100 times; selection, the IF statement, adds a value only when it is in range.

Worked example. A program picks a secret integer from 1 to 100. The user guesses until they are right; after each wrong guess the program says "Too low" or "Too high", and at the end it outputs how many guesses were made.

Identifier table: Secret : INTEGER (the number to guess), Guess : INTEGER (the user's input), Tries : INTEGER (how many guesses so far).

A REPEAT ... UNTIL loop is the right choice because the user must guess at least once. The marks are for: the random number in the right range, a loop that ends on a correct guess, the counter that starts at zero and increases inside the loop, the two messages under the right conditions, and the final output.

Worked example. Output two different random integers, each between $-10$ and $10$ inclusive.

There are 21 possible values, so INT(RAND(21)) gives 0 to 20 and subtracting 10 shifts it to the range $-10$ to $10$. The second number must be generated again until it differs from the first:

العربية

الكود الوهمي هو طريقة منظمة محايدة للغة لوصف الخوارزميات.

البناءان الأساسيان كمخططات تدفق مصغرة: التسلسل ينفذ الخطوة A ثم B ثم C؛ الاختيار يختبر شرطًا وينفذ X أو Y؛ التكرار يعيد تنفيذ جسم الحلقة طالما يستمر شرط، مع العودة للدائرة
البناءات الأساسية لأي خوارزمية: التسلسل، الاختيار، والتكرار

1. التسلسل

تنفذ الخطوات واحدًا تلو الآخر (تسلسل):

INPUT Name
INPUT Age
OUTPUT "Hello", Name

2. الاختيار

اختيار أي خطوات تنفذ بناءً على شرط (اختيار):

IF Age >= 18 THEN
    OUTPUT "Adult"
ELSE
    OUTPUT "Minor"
ENDIF

لمزيد من الخيارات، استخدم CASE OF ... ENDCASE.

3. التكرار

إعادة تنفيذ كتلة (تكرار، حلقة):

FOR i ← 1 TO 10
    OUTPUT i
NEXT i

حلقة WHILE تختبر الشرط قبل كل مررة (قد لا تعمل صفر مرة)؛ حلقة REPEAT...UNTIL تختبر بعد كل مررة (تعمل دائمًا مرة واحدة على الأقل).

WHILE Total < 100 DO
    INPUT Value
    Total ← Total + Value
ENDWHILE

REPEAT
    INPUT Mark
UNTIL Mark >= 0 AND Mark <= 100
مخططان تدفق بجانب بعضهما البعض. WHILE يختبر الشرط أولاً، لذا قد لا يعمل الجسم أبدًا: يوجد المعين فوق الجسم وغ出去的 الفرع 'لا' يخرج من الحلقة. REPEAT UNTIL يعمل الجسم أولاً ويختبر بعده، لذا يعمل الجسم دائمًا مرة واحدة على الأقل: يوجد الجسم فوق المعين وعود الفرع 'لا' إليه
حلقة WHILE تختبر قبل عمل الجسم؛ حلقة REPEAT ... UNTIL تختبر بعده، لذا يعمل جسمها دائمًا مرة واحدة على الأقل

اختيار الحلقة يعتبر درجة بحد ذاته: FOR عندما تعرف عدد المرات (حلقة تحكم بالعد)؛ WHILE عندما قد لا تعمل الحلقة على الإطلاق (حلقة شرط مسبق)؛ REPEAT ... UNTIL عندما يجب أن تعمل مرة واحدة على الأقل، كما في التحقق من صحة الإدخال (حلقة شرط لاحق). إجابة "وصف بناء التكرار" تذكر اسم البناء، وتقول أين يتم اختبار الشرط، وتعطي النتيجة (صفر مرة أو مرة واحدة على الأقل).

عمليات شائعة

  • التعيين: x ← 5 (سهم؛ = للمقارنة).
  • الإدخال/الإخراج: INPUT variable، OUTPUT expression.
  • المقارنات =، <>، <، >، <=، >=؛ المنطق AND، OR، NOT.
  • العمليات الحسابية + - * /، والجمع DIV (القسمة الصحيحة) والقسمة MOD (باقي القسمة).
  • النصوص: LENGTH، LEFT، RIGHT، MID، و& لـ الدمج (الربط).

خوارزمية الوصف الكاذب المتوقعة في الامتحان

يتم تصحيح كل إجابة بخوارزمية وصف كاذب وفقاً لدليل كامبريدج المنشور. اكتب هذه الأشكال تماماً:

البناء خوارزمية الوصف الكاذب
متغير DECLARE Total : INTEGER
مصفوفة DECLARE Marks : ARRAY[1:30] OF REAL
ثابت CONSTANT MaxTries = 3
إسناد Total ← Total + Value
إدخال / إخراج INPUT Name
OUTPUT "Hello ", Name
الاختيار CASE OF Choice
1 : OUTPUT "Add"
OTHERWISE OUTPUT "Error"
ENDCASE
حلقة FOR FOR i ← 1 TO 10 STEP 2 ... NEXT i
حلقة WHILE WHILE Total < 100 DO ... ENDWHILE
حلقة REPEAT REPEAT ... UNTIL Mark >= 0
الحساب الصحيح 17 DIV 5 = 3
17 MOD 5 = 2
السلاسل النصية LENGTH(S)، LEFT(S, 3)، RIGHT(S, 2)
MID(S, 2, 4)، UCASE(S)، LCASE(S)
التحويلات INT(3.7) = 3، NUM_TO_STR(12)
STR_TO_NUM("4.5")، ASC('A') = 65، CHR(66) = 'B'
عشوائي RAND(100)
INT(RAND(100)) + 1

RAND(100) يعطي عدداً حقيقياً من 0 إلى (ولكن لا يشمل) 100. INT(RAND(100)) + 1 يعطي عدداً صحيحاً من 1 إلى 100.

سلوكان يكسبان درجات في كل سؤال: أعّرّف كل متغير تستخدمه، مع النوع من جدول المعرفات الخاص بك، وابدأ كل عداد ومجموع (Count ← 0، Total ← 0) قبل الحلقة التي تغيره.

الإدخال → المعالجة → الإخراج

كل برنامج يتبع هذا الشكل:

INPUT Length
INPUT Width
Area ← Length * Width
OUTPUT "Area = ", Area

تعداد المدخلات والمخرجات أولاً يجعل الخوارزمية أنظف.

مثال محلل. اكتب خوارزمية وصف كاذب تستقبل 100 عدد صحيح وتخرج كم عدداً منها، ومجموع تلك، تقع بين 10 و20 شاملاً الحدين.

جدول المعرفات: Count : INTEGER (عداد الحلقة)، Value : INTEGER (العدد الصحيح الذي تم إدخاله للتو)، InRange : INTEGER (كم عدداً كان في النطاق)، Total : INTEGER (مجموعهم).

DECLARE Count, Value, InRange, Total : INTEGER
InRange ← 0
Total ← 0
FOR Count ← 1 TO 100
    INPUT Value
    IF Value >= 10 AND Value <= 20 THEN
        InRange ← InRange + 1
        Total ← Total + Value
    ENDIF
NEXT Count
OUTPUT InRange, Total

إذا طلب منك السؤال بعد ذلك "تحديد بناءين وإخبار كيف يُستخدم كل منهما"، فأجب بنفس الشكل: التكرار، حلقة FOR، تكرر الإدخال 100 مرة؛ الاختيار، جملة IF، تضيف قيمة فقط عندما تكون في النطاق.

مثال محلل. برنامج يختار عدداً صحيحاً سراً من 1 إلى 100. يخمن المستخدم حتى يصيب؛ بعد كل تخمين خاطئ يقول البرنامج "منخفض جداً" أو "مرتفع جداً"، وفي النهاية يخرج كم عدد التخمينات التي تمت.

جدول المعرفات: Secret : INTEGER (الرقم المراد تخمينه)، Guess : INTEGER (إدخال المستخدم)، Tries : INTEGER (كم عدد التخمينات حتى الآن).

DECLARE Secret, Guess, Tries : INTEGER
Secret ← INT(RAND(100)) + 1
Tries ← 0
REPEAT
    INPUT Guess
    Tries ← Tries + 1
    IF Guess < Secret THEN
        OUTPUT "Too low"
    ELSE
        IF Guess > Secret THEN
            OUTPUT "Too high"
        ENDIF
    ENDIF
UNTIL Guess = Secret
OUTPUT "You took ", Tries, " guesses"

حلقة REPEAT ... UNTIL هي الخيار الصحيح لأن المستخدم يجب أن يخمن مرة واحدة على الأقل. الدرجات تكون لـ: الرقم العشوائي في النطاق الصحيح، وحلقة تنتهي بتخمين صحيح، والعداد الذي يبدأ من الصيادة ويتزايد داخل الحلقة، والرسالتين تحت الشروط الصحيحة، والإخراج النهائي.

مخطط انسيابي للعبة التخمين: ابدأ، ثم اضبط السر على عدد صحيح عشوائي من 1 إلى 100 والمحاولات على 0، ثم أدخل تخميناً، أضف واحداً للمحاولات، تحقق مما إذا كان التخمين يساوي السر (نعم يؤدي إلى إخراج المحاولات والتوقف)، وإلا تحقق مما إذا كان التخمين أصغر (نعم يُخرج 'منخفض جداً'، لا يُخرج 'مرتفع جداً')، وكلا الإخراجين يعيدان حلقة إلى الإدخال
نفس لعبة التخمين كمخطط تدفق: معينا القرار هما جملتا IF، وسهم العودة هو حلقة REPEAT ... UNTIL

مثال محلل. أخرج عددين صحيحين عشوائيين مختلفين، كل منهما بين $-10$ و$10$ شاملاً الحدين.

هناك 21 قيمة محتملة، لذا INT(RAND(21)) يعطي 0 إلى 20 والطرح 10 يحركه إلى نطاق $-10$ إلى $10$. يجب توليد الرقم الثاني مجدداً حتى يختلف عن الأول:

DECLARE First, Second : INTEGER
First ← INT(RAND(21)) - 10
REPEAT
    Second ← INT(RAND(21)) - 10
UNTIL Second <> First
OUTPUT First, Second
كل برنامج يتبع شكل الإدخال، ثم المعالجة، ثم الإخراج، موضح بمثال المساحة: إدخال الطول والعرض، معالجة بالضرب، إخراج المساحة
كل برنامج يتبع شكل الإدخال، المعالجة، الإخراج
Explore · ⁨استكشف⁩

IF … ELSE selection · ⁨IF … ELSE للاختيار⁩

Change the value and watch which branch runs — how a program makes a decision. · ⁨غيّر القيمة وراقب أي فرع يتم تشغيله — كيف يتخذ البرنامج قرارًا.⁩

Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
variable/ˈveərɪəbl/ حدث
data type/ˈdeɪtə taɪp/ نوع البيانات
pseudocode/ˈsuːdəʊkəʊd/ الكود الوهمي
flowchart/ˈfləʊtʃɑːt/ مخطط انسيابي
selection/sɪˈlekʃn/ اختيار
iteration/ˌɪtəˈreɪʃn/ التكرار
loop/luːp/ متغير
count-controlled loop/kaʊnt kənˈtrəʊld luːp/ حلقة محكومة بعدد العدادات
pre-condition loop/priː kənˈdɪʃn luːp/ حلقة الشرط المسبق
post-condition loop/pəʊst kənˈdɪʃn luːp/ حلقة الشرط اللاحق
assignment/əˈsaɪnmənt/ التعيين (assignment)
concatenation/kənˌkætəˈneɪʃn/ الدمج (concatenation)
initialise/ɪˈnɪʃəlaɪz/ تهيئة
counter/ˈkaʊntə/ مضاد
structured English/ˈstrʌktʃəd ˈɪŋɡlɪʃ/ اللغة الإنجليزية المنسقة
stepwise refinement/ˈstepwaɪz rɪˈfaɪnmənt/ التدريج المتدرج
logic statement/ˈlɒdʒɪk ˈsteɪtmənt/ عبارة منطقية
precedence/ˈpresɪdəns/ أولوية
De Morgan's law/də ˈmɔːɡənz lɔː/ قانون دي مورغان
9.2

Three notations · ⁨ثلاث رموز⁩

English

The same algorithm can be written three ways.

  • structured English 结构化英语 — natural language with indentation and fixed keywords; good for a high-level description.
  • flowchart 流程图 — a diagram with standard shapes:
Shape Meaning
Rounded rectangle Start / Stop
Parallelogram Input / Output
Rectangle Process
Diamond Decision
Arrow Flow of control
  • pseudocode — the keyword notation above; closest to code.

You should be able to convert between any pair: each IF is a decision diamond, each loop is a back-arrow, and a sequence is stacked rectangles.

IF ... THEN ... ELSE ... ENDIF

العربية

يمكن كتابة نفس الخوارزمية بثلاث طرق.

  • إنجليزية منظمة — لغة طبيعية مع مسافات بادئة وكلمات مفتاحية ثابتة؛ جيدة لوصف عالي المستوى.
  • مخطط تدفق — رسم بياني بأشكال قياسية:
الشكل المعنى
مستطيل دائر الحواف بداية / توقف
متوازي الأضلاع إدخال / إخراج
مستطيل معالجة
معين قرار
سهم تدفق التحكم
  • خوارزمية وصف كاذب — رمز الكلمات المفتاحية أعلاه؛ الأقرب للكود.

يجب أن تكون قادراً على التحويل بين أي زوج: كل IF هو معين قرار، وكل حلقة هي سهم رجوع، والتسلسل هو مستطيلات متراكبة.

IF ... THEN ... ELSE ... ENDIF

مخطط تدفق لتوسط الأرقام: بدالات بداية وتوقف دائرة الحواف، ومتوازيات الأضلاع للإدخال/الإخراج، ومستطيلات المعالجة، ومعين قرار "count < n؟" فرع نعم يعيد القراءة القيمة التالية
مخطط تدفق لتوسط قائمة من الأرقام، باستخدام الأشكال القياسية
9.2

Stepwise refinement · ⁨التحسين التدريجي⁩

English

Stepwise refinement 逐步求精 starts with a high-level outline and expands each step until it is small enough to code. For an average of $n$ numbers:

Level 1:

Level 2:

Each refinement keeps the previous structure and adds detail.

A six-mark "apply stepwise refinement" question gives you a high-level outline and wants each step expanded into the concrete statements a programmer could code. Keep the steps in the same order, name the data each step reads or produces, and stop when every line is a single input, assignment, output, loop or condition. For example, "validate the password" becomes: input the password; check its length is at least 8; check it contains at least one digit; output "accepted" if both checks pass, otherwise output "rejected".

العربية

التدريج الدقيق يبدأ بمخطط عالي المستوى و يوسّع كل خطوة حتى تصبح صغيرة بما يكفي للبرمجة. لمتوسط $n$ من الأرقام:

المستوى 1:

Read in the numbers
Compute the average
Output the average

المستوى 2:

INPUT n
total ← 0
FOR i ← 1 TO n
    INPUT value
    total ← total + value
NEXT i
average ← total / n
OUTPUT average

يحافظ كل تحسين على البنية السابقة ويضيف التفاصيل.

سؤال "تطبيق التحسين التدريجي" بست درجات يمنحك مخططاً عالياً المستوى ويريد تفسير كل خطوة إلى العبارات الملمسة التي يمكن للمبرمج برمجتها. حافظ على الخطوات بنفس الترتيب، وسمّ البيانات التي تقرأها أو تنتجها كل خطوة، وتوقف عندما تكون كل سطر إدخالاً واحداً، أو إسناداً، أو إخراجاً، أو حلقة، أو شرطاً. على سبيل المثال، "تحقق من كلمة المرور" يصبح: أدخل كلمة المرور؛ تحقق من طولها أنه 8 على الأقل؛ تحقق من أنها تحتوي على رقم واحد على الأقل؛ أخرج "مقبولة" إذا نجحت كلا التحققين، وإلا أخرج "مرفوضة".

التحسين التدريجي: outline مستوى 1 (اقرأ الأرقام، احسب المتوسط، أخرج المتوسط) يتم توسيعه إلى pseudocode تفصيلي مستوى 2 مع حلقة الإدخال والقسمة
التحسين التدريجي: قم بتوسيع كل خطوة على مستوى عالٍ إلى كود زائف تفصيلي
Explore · ⁨استكشف⁩

Stepwise refinement: outline to code · ⁨التحسين التدريجي: من المخطط إلى الكود⁩

Step down the levels. You start with the whole task in one line and keep expanding each step into smaller ones — until every step is simple enough to code directly. · ⁨انزل عبر المستويات. تبدأ بالمهمة الكاملة في سطر واحد وتستمر في توسيع كل خطوة إلى أجزاء أصغر — حتى تصبح كل خطوة بسيطة بما يكفي للكتابة البرمجية مباشرة.⁩

9.2

Logic statements · ⁨العبارات المنطقية⁩

English

A logic statement 逻辑语句 is a Boolean 布尔 condition that controls branching, built from comparisons (x > 10), connectives (AND, OR, NOT) and brackets. Use it as the condition of IF, WHILE or REPEAT...UNTIL:

Precedence 优先级 (highest to lowest): NOT, then AND, then OR. Use brackets when unsure. Common mistakes:

  • a = 1 OR 2 is wrong — write a = 1 OR a = 2.
  • NOT a > 5 means NOT (a > 5), i.e. a <= 5.
  • NOT (A AND B) is the same as (NOT A) OR (NOT B) (De Morgan's law 德摩根定律) — handy for simplifying conditions.

Turning a sentence into a logic statement is a skill the papers test directly. "A ticket is free for anyone under 5 or over 65" becomes Age < 5 OR Age > 65. "A mark is valid if it is a whole number from 0 to 100" becomes Mark >= 0 AND Mark <= 100. "The loop stops when the file is finished or ten records have been read" becomes UNTIL EOF(File) OR Count = 10. Write each comparison in full: Age > 65 and Age < 5, never Age > 65 OR < 5.

Worked example. Write an identifier table and pseudocode to read 10 numbers and output the largest. The identifier table names each variable with its data type and purpose: Count : INTEGER (loop counter), Num : REAL (the number just read), Max : REAL (largest so far).

The design decision carrying the marks is initialising Max: it must start lower than any possible input - or, safer still, be set to the first number read. Initialise it to 0 and the algorithm wrongly returns 0 for a list of negative numbers, a bug your trace only exposes if the test data include a negative.

العربية

عبارة منطقية هي شرط بولياني يتحكم في التفرع، مُبنى من مقارنات (x > 10)، وروابط (AND، OR، NOT) وأقواس. استخدمه كشرط لـ IF، WHILE أو REPEAT...UNTIL:

WHILE attempts < 3 AND NOT loggedIn DO
    INPUT password
    IF password = correctPassword THEN
        loggedIn ← TRUE
    ELSE
        attempts ← attempts + 1
    ENDIF
ENDWHILE

الأولوية (من الأعلى إلى الأدنى): NOT، ثم AND، ثم OR. استخدم الأقواس عند الشك. أخطاء شائعة:

  • a = 1 OR 2 خطأ — اكتب a = 1 OR a = 2.
  • NOT a > 5 يعني NOT (a > 5)، أي a <= 5.
  • NOT (A AND B) هو نفسه (NOT A) OR (NOT B) (قانون دي مورغان) — مفيد لتبسيط الشروط.

تحويل الجملة إلى بيان منطقي هو مهارة يختبرها الامتحان مباشرة. "التذكرة مجانية لأي شخص أقل من 5 أو أكبر من 65" تصبح Age < 5 OR Age > 65. "الدرجة صالحة إذا كانت عدداً صحيحاً من 0 إلى 100" تصبح Mark >= 0 AND Mark <= 100. "تتوقف الحلقة عندما ينتهي الملف أو تم قراءة عشر سجلات" تصبح UNTIL EOF(File) OR Count = 10. اكتب كل مقارنة بالكامل: Age > 65 و Age < 5، لا تستعمل أبداً Age > 65 OR < 5.

شجرة تحليل لعبارة "attempts < 3 AND NOT loggedIn": NOT تطبق على loggedIn أولاً، ثم AND يجمع ذلك مع attempts < 3
الأولوية: NOT ترتبط بـ loggedIn أولاً، ثم AND يدمج الجانبين

مثال محلول. اكتب جدول تعريفات وكود زائف لقراءة 10 أرقام وإخراج الأكبر. جدول التعريفات يسمي كل متغير بنوع البيانات والغرض منه: Count : INTEGER (عداد الحلقة)، Num : REAL (الرقم المقروء حديثاً)، Max : REAL (الأكبر حتى الآن).

Max ← -999999
FOR Count ← 1 TO 10
    INPUT Num
    IF Num > Max THEN
        Max ← Num
    ENDIF
NEXT Count
OUTPUT Max

قرار التصميم الذي يحمل الدرجات هو تهيئة Max: يجب أن يبدأ أقل من أي إدخال ممكن - أو، الأمان أكثر، يتم تعيينه على الرقم الأول المقروء. قم بتهيئته على 0 وعادة ما يعطي الخوارزمية الخطأ 0 لمجموعة من الأرقام السالبة، وهو عيب يكشف عنه التتبع فقط إذا شملت بيانات الاختبار رقماً سالباً.

9.2

Definitions the examiner accepts · ⁨التعريفات التي يقبلها المصحح⁩

English

A definition question is marked against fixed wording. Learn these exactly, and give one answer only.

Term Definition
abstraction keeping the essential details of a problem and leaving out the details that are not needed
decomposition breaking a problem down into smaller sub-problems, each of which can be solved separately
algorithm a solution to a problem expressed as a sequence of defined steps
identifier table a table listing each identifier used in an algorithm with its data type and a description of its purpose
pseudocode a structured, language-independent way of writing the steps of an algorithm
flowchart a diagram that shows the steps and decisions of an algorithm using standard symbols joined by arrows
sequence statements executed one after another in the order written
selection choosing which statements to execute according to a condition
iteration repeating a group of statements while, or until, a condition holds
stepwise refinement breaking each step of an outline into smaller steps, repeatedly, until each step can be coded directly
logic statement a condition built from comparisons and the operators AND, OR and NOT that evaluates to TRUE or FALSE
العربية

تُصنّف أسئلة التعريف بناءً على صياغة ثابتة. احفظ هذه التعريفات بدقة، وقدم إجابة واحدة فقط.

مصطلح تعريف
تجريد الاحتفاظ بالتفاصيل الأساسية للمشكلة واستبعاد التفاصيل غير اللازمة
التفكيك تقسيم المشكلة إلى مشاكل فرعية أصغر، يمكن حل كل منها على حدة
خوارزمية حل لمشكلة معبر عنه كمسلسل من الخطوات المحددة
جدول التعريفات جدول يسرد كل معرف مستخدم في خوارزمية مع نوع البيانات ووصف لغرضه
الكود الزائف طريقة منظمة ومستقلة عن اللغة لكتابة خطوات الخوارزمية
المخطط الانسيابي مخطط يوضح خطوات الخوارزمية وقراراتها باستخدام رموز قياسية متصلة بأسهم
التسلسل تنفيذ العبارات واحدة تلو الأخرى بالترتيب المكتوب
الاختيار اختيار العبارات التي سيتم تنفيذها وفقاً لشرط
التكرار تكرار مجموعة من العبارات طالما، أو حتى، يتحقق شرط
التحسين التدريجي تقسيم كل خطوة من الخطوات الأولية إلى خطوات أصغر، مراراً وتكراراً، حتى يمكن برمجة كل خطوة مباشرة
العبارة المنطقية شرط مُبنى من المقارنات والمشغلات AND وOR وNOT الذي يُقيّم إلى TRUE أو FALSE
9.2

Exam tips · ⁨نصائح للامتحان⁩

English
  • Define an algorithm as an unambiguous, finite, deterministic sequence of steps, independent of language.
  • Use the three constructs correctly — sequence, selection, iteration — and keep an identifier table with data types.
  • Break a problem down by decomposition and abstraction, then stepwise refinement.
  • Write pseudocode that would actually run: declare variables and follow the exam's pseudocode style.

Common mistakes

  • Using = to assign a value. Assignment is ←; = is a comparison.
  • Forgetting ENDIF, ENDWHILE, ENDCASE or NEXT. Every construct closes, and the closing word is where the mark for the construct is checked.
  • Not initialising a total or counter before the loop, so the algorithm adds to a value that never existed.
  • Using a FOR loop when the number of repetitions is unknown. Reading until a sentinel value or a correct guess needs WHILE or REPEAT ... UNTIL.
  • Writing Age > 65 OR < 5. Each side of OR and AND must be a complete comparison.
  • Answering "explain why decomposition is used" with one benefit written three ways. Three marks need three different benefits.
العربية
  • عرّف الخوارزمية على أنها سلسلة غير غامضة، منتهية، حتمية من الخطوات، مستقلة عن اللغة.
  • استخدم البناءات الثلاثة بشكل صحيح — التسلسل، الاختيار، التكرار — واحتفظ بجدول تعريفات مع أنواع البيانات.
  • قسم المشكلة بواسطة التفكيك والتجريد، ثم التحسين التدريجي.
  • اكتب كوداً زائفاً يمكن تشغيله فعلياً: Declare المتغيرات واتبع نمط الكود الزائف الخاص بالامتحان.

أخطاء شائعة

  • استخدام = لتعيين قيمة. التعيين هو ←؛ = هو مقارنة.
  • نسيان ENDIF، ENDWHILE، ENDCASE أو NEXT. كل بناء يُغلق، والكلمة الختارية هي حيث يتم التحقق من علامة البناء.
  • عدم تهيئة المجموع أو العداد قبل الحلقة، مما يجعل الخوارزمية تضيف إلى قيمة لم توجد أبداً.
  • استخدام حلقة FOR عندما يكون عدد التكرارات غير معروف. القراءة حتى قيمة.sentinel أو تخمين صحيح تحتاج WHILE أو REPEAT ... UNTIL.
  • كتابة Age > 65 OR < 5. يجب أن تكون كل جانب من OR وAND مقارنة كاملة.
  • الإجابة على "اشرح سبب استخدام التفكيك" بفائدة واحدة مكتوبة بثلاث طرق. ثلاث درجات تتطلب ثلاث فوائد مختلفة.

Interactive lessons on this topic · ⁨دروس تفاعلية حول هذا الموضوع⁩

Work through it step by step, with instant-check exercises. · ⁨ا-working عليه خطوة بخطوة، مع تمارين تحقق فوري.⁩

Past Papers · ⁨أوراق الامتحانات السابقة⁩

More topics in A-Level Computer Science · ⁨A-Level علوم الحاسوب⁩ · ⁨المزيد من المواضيع في A-Level Computer Science · ⁨A-Level علوم الحاسوب⁩⁩

Log in or create account · ⁨تسجيل الدخول أو إنشاء حساب⁩

IGCSE, A-Level & AP