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

Selection and Iteration · ⁨الاختيار والتكرار⁩

AP Computer Science A · ⁨AP علوم الحاسوب A⁩ · Topic 2 · ⁨الموضوع 2⁩

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

الاختيار والتكرار

إليك ثلاث حلقات تكرار. تختلف عن بعضها بحرف واحد — علامة أصغر بدلاً من أصغر أو يساوي، علامة أكبر بدلاً من أصغر. الأولى تعمل…

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

2.1

Selection and Repetition in Algorithms · ⁨الاختيار والتكرار في الخوارزميات⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.1.A: Represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams.

  • 2.1.A.1 The building blocks of algorithms include sequencing, selection, and repetition.
  • 2.1.A.2 Algorithms can contain selection, through decision making, and repetition, via looping.
  • 2.1.A.3 Selection occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision.
  • 2.1.A.4 Repetition is when a process repeats itself until a desired outcome is reached.
  • 2.1.A.5 The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.
العربية

هدف التعلم 2.1.A: تمثيل الأنماط والخوارزميات التي تتضمن الاختيار والتكرار الموجودة في الحياة اليومية باستخدام اللغة المكتوبة أو المخططات.

  • 2.1.A.1 تشمل اللبنات الأساسية للخوارزميات التتابع والاختيار والتكرار.
  • 2.1.A.2 يمكن أن تحتوي الخوارزميات على اختيار، من خلال اتخاذ القرار، وتكرار، عبر الحلقات.
  • 2.1.A.3 الاختيار يحدث عندما يعتمد اختيار كيفية سير تنفيذ الخوارزمية على قرار صحيح أو خاطئ.
  • 2.1.A.4 التكرار هو عندما يتكرر عملية ما حتى الوصول إلى نتيجة مرغوبة.
  • 2.1.A.5 الترتيب الذي يتم فيه استخدام التتابع والاختيار والتكرار يساهم في نتيجة الخوارزمية.

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

Algorithms are built from three control structures 控制结构: sequence (steps in order), selection 选择 (choosing a path), and iteration 迭代 (repeating steps). This topic covers selection and iteration – the tools that let a program make decisions and loop.

العربية
مخطط انسيابي مع معين قرار: الاختيار يحدد المسار الذي يتخذه الخوارزمية
مخطط انسيابي مع معين قرار: الاختيار يحدد المسار الذي يتخذه الخوارزمية

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

الهياكل التحكمية الثلاثة: التسلسل، الاختيار، والتكرار
الهياكل التحكمية الثلاثة: التسلسل، الاختيار، والتكرار
Vocabulary · ⁨مفردات⁩ Train · ⁨تدريب⁩
English العربية
control structures/kənˈtrəʊl ˈstrʌktʃəz/ هياكل التحكم
selection/sɪˈlekʃn/ اختيار
iteration/ˌɪtəˈreɪʃn/ التكرار
boolean expression/ˈbuːlɪən ekˈspreʃn/ عبارة منطقية
relational operators/rɪˈleɪʃənl ˈɒpəreɪtəz/ المقارنات العلاقية
if statement/ɪf ˈsteɪtmənt/ عبارة if
Logical operators/ˈlɒdʒɪkl ˈɒpəreɪtəz/ المؤثرات المنطقية
short-circuit evaluation/ʃɔːt ˈsɜːkɪt ɪˌvæljuːˈeɪʃn/ التقييم القصير الدائرة
De Morgan's laws/də ˈmɔːɡənz lɔːz/ قوانين دي مورغان
while loop/waɪl luːp/ حلقة while
infinite loop/ˈɪnfɪnət luːp/ حلقة لا نهائية
flag/flæɡ/ علامة
nested loop/ˈnestɪd luːp/ حلقة متداخلة
Run-time analysis/rʌn taɪm əˈnæləsɪs/ تحليل وقت التشغيل
2.2

Boolean Expressions · ⁨العبارات المنطقية⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.2.A: Develop code to create Boolean expressions with relational operators and determine the result of these expressions.

  • 2.2.A.1 Values can be compared using the relational operators == and != to determine whether the values are the same. With primitive types, this compares the actual primitive values. With reference types, this compares the object references.
  • 2.2.A.2 Numeric values can be compared using the relational operators <, >, <=, and >= to determine the relationship between the values.
  • 2.2.A.3 An expression involving relational operators evaluates to a Boolean value.
العربية

هدف التعلم 2.2.A: تطوير كود لإنشاء عبارات منطقية باستخدام المعاملات العلائقية وتحديد نتيجة هذه العبارات.

  • 2.2.A.1 يمكن مقارنة القيم باستخدام المعاملات العلائقية == و!= لتحديد ما إذا كانت القيم متساوية. مع الأنواع الأولية، يقارن هذا القيم الأولية الفعلية. مع أنواع المرجع، يقارن هذا مرجعات الكائن.
  • 2.2.A.2 يمكن مقارنة القيم العددية باستخدام المعاملات العلائقية <، >، <=، و>= لتحديد العلاقة بين القيم.
  • 2.2.A.3 تعطي العبارة التي تتضمن معاملات علائقية قيمة منطقية (Boolean value).

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English
Logic gates & the half-adder

A boolean expression 布尔表达式 evaluates to true or false, using relational operators 关系运算符: == (equal), != (not equal), <, >, <=, >=. Note == compares primitive values but object references for objects, so use .equals for Strings.

العربية
البوابات المنطقية والمجمّع النصف

تُقيّم العلاقة المنطقية إلى true أو false، باستخدام المعاملات العلائقية: == (متساوي)، != (لا يساوي)، <، >، <=، >=. لاحظ أن == يقارن القيم الأولية ولكن مرجعات الكائنات للكائنات، لذا استخدم .equals لـ Strings.

عائلات المعاملات الثلاث: الحسابية، العلائقية، والمنطقية
عائلات المعاملات الثلاث: الحسابية، العلائقية، والمنطقية
Explore · ⁨استكشف⁩

Explore the AND truth table · ⁨استكشف جدول الحقيقة لـ AND⁩

A Boolean expression evaluates to true or false. AND is true only when both operands are true; toggle the inputs to see all four cases. · ⁨التعبير المنطقي يُقيّم إلى true أو false. يكون AND صحيحاً فقط عندما يكون كلا المعاملين صحيحين؛ غيّر المدخلات لرؤية الحالات الأربع.⁩

2.3

The if Statement · ⁨جملة if⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.3.A: Develop code to represent branching logical processes by using selection statements and determine the result of these processes.

  • 2.3.A.1 Selection statements change the sequential execution of statements.
  • 2.3.A.2 An if statement is a type of selection statement that affects the flow of control by executing different segments of code based on the value of a Boolean expression.
  • 2.3.A.3 A one-way selection (if statement) is used when there is a segment of code to execute under a certain condition. In this case, the body is executed only when the Boolean expression is true.
  • 2.3.A.4 A two-way selection (if-else statement) is used when there are two segments of code—one to be executed when the Boolean expression is true and another segment for when the Boolean expression is false. In this case, the body of the if is executed when the Boolean expression is true, and the body of the else is executed when the Boolean expression is false.
العربية

هدف التعلم 2.3.A: تطوير كود لتمثيل العمليات المنطقية المتفرعة باستخدام جمل الاختيار وتحديد نتيجة هذه العمليات.

  • 2.3.A.1 جمل الاختيار تغير التنفيذ التسلسلي للجمل.
  • 2.3.A.2 جملة if هي نوع من جمل الاختيار تؤثر على تدفق التحكم عن طريق تنفيذ أجزاء مختلفة من الكود بناءً على قيمة عبارة منطقية.
  • 2.3.A.3 الاختيار أحادي المسار (جملة if) يُستخدم عندما يكون هناك جزء من الكود لتنفيذه تحت شرط معين. في هذه الحالة، يتم تنفيذ الجسم فقط عندما تكون العبارة المنطقية true.
  • 2.3.A.4 الاختيار ثنائي المسار (جملة if-else) يُستخدم عندما يكون هناك جزآن من الكود - واحد لتنفيذه عندما تكون العبارة المنطقية true وجزء آخر لتنفيذه عندما تكون العبارة المنطقية false. في هذه الحالة، يتم تنفيذ جسم if عندما تكون العبارة المنطقية true، ويتم تنفيذ جسم else عندما تكون العبارة المنطقية false.

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

An if statement 条件语句 runs a block only when its condition is true; an optional else gives an alternative:

العربية

تشغل جملة if كتلة فقط عندما يكون شرطها صحيحاً؛ ومقدّم اختياري else يوفر بديلاً:

if (score >= 60) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}
إشارات المرور: الاختيار يحدد أي فرع يعمل، تماماً كما تختار جمل if مسارات الكود
إشارات المرور: الاختيار يحدد أي فرع يعمل، تماماً كما تختار جمل if مسارات الكود
Explore · ⁨استكشف⁩

See which branch an if chooses · ⁨شاهد أي فرع يختاره if⁩

An if statement runs its body only when the condition is true, otherwise it skips to else. Slide the score across the boundaries and watch the grade change. · ⁨يعمل أمر if جسمه فقط عندما يكون الشرط صحيحاً، وإلا يتخطاه إلى else. حرك الدرجة عبر الحدود وراقب تغير التقدير.⁩

2.4

Nested if Statements · ⁨جمل if المتداخلة⁩

Syllabus · ⁨المنهج⁩
Learning ObjectiveEssential Knowledge

2.4.A
Develop code to represent nested branching logical processes and determine the result of these processes.

  • 2.4.A.1 Nested if statements consist of if, if-else, or if-else-if statements within if, if-else, or if-else-if statements.
  • 2.4.A.2 The Boolean expression of the inner nested if statement is evaluated only if the Boolean expression of the outer if statement evaluates to true.
  • 2.4.A.3 A multiway selection (if-else-if) is used when there are a series of expressions with different segments of code for each condition. Multiway selection is performed such that no more than one segment of code is executed based on the first expression that evaluates to true. If no expression evaluates to true and there is a trailing else statement, then the body of the else is executed.

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

Placing an if inside another, or chaining with else if, tests several cases in order. Only the first matching branch runs:

العربية

وضع if داخل أخرى، أو تسلسلها مع else if، يختبر عدة حالات بالترتيب. يتم تنفيذ الفرع الأول المطابق فقط:

if (g >= 90) grade = 'A';
else if (g >= 80) grade = 'B';
else grade = 'C';
2.5

Compound Boolean Expressions · ⁨عبارات منطقية مركبة⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.5.A: Develop code to represent compound Boolean expressions and determine the result of these expressions.

  • 2.5.A.1 Logical operators ! (not), && (and), and || (or) are used with Boolean expressions. The expression !a evaluates to true if a is false and evaluates to false otherwise. The expression a && b evaluates to true if both a and b are true and evaluates to false otherwise. The expression a || b evaluates to true if a is true, b is true, or both, and evaluates to false otherwise. The order of precedence for evaluating logical operators is ! (not), && (and), then || (or). An expression involving logical operators evaluates to a Boolean value.
  • 2.5.A.2 Short-circuit evaluation occurs when the result of a logical operation using && or || can be determined by evaluating only the first Boolean expression. In this case, the second Boolean expression is not evaluated.
العربية

هدف التعلم 2.5.A: تطوير كود لتمثيل العبارات المنطقية المركبة وتحديد نتيجة هذه العبارات.

  • 2.5.A.1 المعاملات المنطقية ! (not)، && (and)، و|| (or) تُستخدم مع العبارات المنطقية. تعطي العبارة !a نتيجة true إذا كانت a تساوي false وتعطي نتيجة false بخلاف ذلك. تعطي العبارة a && b نتيجة true إذا كان كل من a وb يساوي true وتعطي نتيجة false بخلاف ذلك. تعطي العبارة a || b نتيجة true إذا كانت a تساوي true، أو b تساوي true، أو كلاهما، وتعطي نتيجة false بخلاف ذلك. ترتيب الأولوية لتقييم المعاملات المنطقية هو ! (not)، ثم && (and)، ثم || (or). تعطي العبارة التي تتضمن معاملات منطقية قيمة منطقية.
  • 2.5.A.2 يحدث التقييم المختصر (Short-circuit evaluation) عندما يمكن تحديد نتيجة عملية منطقية تستخدم && أو || عن طريق تقييم العبارة المنطقية الأولى فقط. في هذه الحالة، لا يتم تقييم العبارة المنطقية الثانية.

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English
Short-circuit evaluation

Logical operators 逻辑运算符 combine conditions: && (and – both true), || (or – at least one true), ! (not – reverse). Java uses short-circuit evaluation 短路求值: && stops if the left side is false, and || stops if the left side is true – useful to guard against errors, e.g. if (n != 0 && total / n > 5).

العربية
التقييم القصري

المعاملات المنطقية تجمع الشروط: && (و – كلاهما صحيح)، || (أو – واحد على الأقل صحيح)، ! (ليس – عكس). تستخدم Java التقييم القصري: && يتوقف إذا كان الجانب الأيسر خاطئاً، و|| يتوقف إذا كان الجانب الأيسر صحيحاً – مفيد للحماية من الأخطاء، مثلاً if (n != 0 && total / n > 5).

2.6

Comparing Boolean Expressions · ⁨مقارنة العبارات المنطقية⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.6.A: Compare equivalent Boolean expressions.

  • 2.6.A.1 Two Boolean expressions are equivalent if they evaluate to the same value in all cases. Truth tables can be used to prove Boolean expressions are equivalent.
  • 2.6.A.2 De Morgan's law can be applied to Boolean expressions to create equivalent Boolean expressions. Under De Morgan's law, the Boolean expression !(a && b) is equivalent to !a || !b and the Boolean expression !(a || b) is equivalent to !a && !b.

Learning Objective 2.6.B: Develop code to compare object references using Boolean expressions and determine the result of these expressions.

  • 2.6.B.1 Two different variables can hold references to the same object. Object references can be compared using == and !=.
  • 2.6.B.2 An object reference can be compared with null, using == or !=, to determine if the reference actually references an object.
  • 2.6.B.3 Classes often define their own equals method, which can be used to specify the criteria for equivalency for two objects of the class. The equivalency of two objects is most often determined using attributes from the two objects.
    • Exclusion statement: Overriding the equals method is outside the scope of the AP Computer Science A course and exam.
العربية

هدف التعلم 2.6.A: مقارنة العبارات المنطقية المكافئة.

  • 2.6.A.1 تكون العبارتان المنطقيتان مكافئتين إذا أعطتا نفس القيمة في جميع الحالات. يمكن استخدام جداول الحقيقة لإثبات أن العبارات المنطقية مكافئة.
  • 2.6.A.2 يمكن تطبيق قانون دي مورغان على العبارات المنطقية لإنشاء عبارات منطقية مكافئة. تحت قانون دي مورغان، العبارة المنطقية !(a && b) مكافئة لـ !a || !b والعبارة المنطقية !(a || b) مكافئة لـ !a && !b.

هدف التعلم 2.6.B: تطوير كود لمقارنة مرجعات الكائن باستخدام العبارات المنطقية وتحديد نتيجة هذه العبارات.

  • 2.6.B.1 يمكن لمتغيرين مختلفين أن يحتوي على مرجعين لنفس الكائن. يمكن مقارنة مرجعات الكائن باستخدام == و!=.
  • 2.6.B.2 يمكن مقارنة مرجع كائن مع null، باستخدام == أو !=، لتحديد ما إذا كان المرجع يوجه فعلياً إلى كائن.
  • 2.6.B.3 عادةً ما تُعرّف الفئات (Classes) نفسها طريقة equals، والتي يمكن استخدامها لتحديد معايير التكافؤ لكائنين من هذه الفئة. غالبًا ما يتم تحديد تكافؤ كائنين باستخدام سمات من الكائنين.
    • عبارة الاستبعاد: تجاوز equals خارج نطاق منهج AP علوم الحاسوب A والامتحان.

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

De Morgan's laws 德摩根定律 rewrite negations: !(a && b) equals !a || !b, and !(a || b) equals !a && !b. Two boolean expressions are equivalent if they give the same result for every input – a truth table proves it. Simplifying conditions this way is a common exam task.

العربية

قوانين دي مورغان تعيد كتابة النفي: !(a && b) يساوي !a || !b، و!(a || b) يساوي !a && !b. تكون عبارتان منطقيتان متكافئتين إذا أعطتا نفس النتيجة لكل مدخل – ويثبت ذلك جدول الحقيقة. يُعد تبسيط الشروط بهذه الطريقة مهمة شائعة في الامتحانات.

2.7

while Loops · ⁨حلقات while⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.7.A: Identify when an iterative process is required to achieve a desired result.

  • 2.7.A.1 Iteration is a form of repetition. Iteration statements change the flow of control by repeating a segment of code zero or more times as long as the Boolean expression controlling the loop evaluates to true.
  • 2.7.A.2 An infinite loop occurs when the Boolean expression in an iterative statement always evaluates to true.
  • 2.7.A.3 The loop body of an iterative statement will not execute if the Boolean expression initially evaluates to false.
  • 2.7.A.4 Off by one errors occur when the iteration statement loops one time too many or one time too few.

Learning Objective 2.7.B: Develop code to represent iterative processes using while loops and determine the result of these processes.

  • 2.7.B.1 A while loop is a type of iterative statement. In while loops, the Boolean expression is evaluated before each iteration of the loop body, including the first. When the expression evaluates to true, the loop body is executed. This continues until the Boolean expression evaluates to false, whereupon the iteration terminates.
العربية

هدف التعلم 2.7.A: تحديد متى يكون العملية التكرارية ضرورية لتحقيق النتيجة المطلوبة.

  • 2.7.A.1 التكرار هو شكل من أشكال التكرار. تقوم عبارات التكرار بتغيير تدفق التحكم عن طريق تكرار جزء من الكود صفرًا أو أكثر من المرات طالما أن التعبير المنطقي الذي يتحكم في الحلقة يعطي نتيجة true.
  • 2.7.A.2 تحدث الحلقة اللانهائية عندما يعطي التعبير المنطقي في عبارة التكرار دائمًا نتيجة true.
  • 2.7.A.3 لن يتم تنفيذ جسم الحلقة في عبارة التكرار إذا كانت النتيجة الأولية للتعبير المنطقي تساوي false.
  • 2.7.A.4 تحدث أخطاء الانحراف بمقدار واحد عندما تكرر عبارة التكرار مرة واحدة أكثر من اللازم أو مرة واحدة أقل من اللازم.

هدف التعلم 2.7.B: تطوير كود لتمثيل العمليات التكرارية باستخدام while وحلقات determining result of these processes.

  • 2.7.B.1 while هي نوع من عبارات التكرار. في while، يتم تقييم التعبير المنطقي قبل كل تكرار لجسم الحلقة، بما في ذلك التكرار الأول. عندما تكون النتيجة true، يتم تنفيذ جسم الحلقة. يستمر هذا حتى يصبح التعبير المنطقي false، وحينها ينتهي التكرار.

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

A while loop 循环 repeats while its condition stays true, testing before each pass. You must change something inside so the loop eventually stops, or it becomes an infinite loop 无限循环:

العربية

تكرر حلقة while طوال بقاء شرطها صحيحاً، مع اختبار قبل كل مرور. يجب تغيير شيء ما داخلها حتى تتوقف الحلقة أخيراً، وإلا تصبح حلقة لا نهائية:

أنواع الحلقات الثلاثة تختلف في مكان اختبار الشرط
أنواع الحلقات الثلاثة تختلف في مكان اختبار الشرط
int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}
Explore · ⁨استكشف⁩

Trace a while loop · ⁨تتبع حلقة while⁩

A while loop repeats as long as its condition stays true, updating its variables each pass. Step through to see the sum of squares build up. · ⁨تكرر حلقة while طالما بقي شرطها صحيحاً، محدثة متغيراتها في كل دورة. مرّ بالخطوات لرؤية تجمع مجموع المربعات.⁩

2.8

for Loops · ⁨حلقات for⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.8.A: Develop code to represent iterative processes using for loops and determine the result of these processes.

  • 2.8.A.1 A for loop is a type of iterative statement. There are three parts in a for loop header: the initialization, the Boolean expression, and the update.
  • 2.8.A.2 In a for loop, the initialization statement is only executed once before the first Boolean expression evaluation. The variable being initialized is referred to as a loop control variable. The Boolean expression is evaluated immediately after the loop control variable is initialized and then following each execution of the increment statement until it is false. In each iteration, the update is executed after the entire loop body is executed and before the Boolean expression is evaluated again.
  • 2.8.A.3 A for loop can be rewritten into an equivalent while loop (and vice versa).
العربية

هدف التعلم 2.8.A: تطوير كود لتمثيل العمليات التكرارية باستخدام for وحلقات determining result of these processes.

  • 2.8.A.1 for هي نوع من عبارات التكرار. تتكون رأس for من ثلاثة أجزاء: التهيئة، والتعبير المنطقي، والتحديث.
  • 2.8.A.2 في for، يتم تنفيذ جملة التهيئة مرة واحدة فقط قبل التقييم الأولي للتعبير المنطقي. يُشار إلى المتغير الذي يتم تهيئته باسم متحكم الحلقة. يتم تقييم التعبير المنطقي فورًا بعد تهيئة متحكم الحلقة ثم بعد كل تنفيذ لجملة الزيادة حتى تصبح false. في كل تكرار، يتم تنفيذ التحديث بعد تنفيذ جسم الحلقة بالكامل وقبل إعادة تقييم التعبير المنطقي.
  • 2.8.A.3 يمكن إعادة كتابة for إلى while مكافئة ( والعكس صحيح).

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

A for loop packs initialization, condition, and update into one line – best when you know the count:

A for and an equivalent while do the same work; be able to convert between them.

العربية

تجمع حلقة for التهيئة والشرط والتحديث في سطر واحد – الأفضل عندما تعرف العدد:

for (int i = 0; i < n; i++) {
    // runs n times, i = 0..n-1
}

يفعل for وwhile المكافئ نفس العمل؛ يجب أن تكون قادراً على التحويل بينهما.

خط إنتاج: تكررت الحلقات عملية لكل عنصر، مثل for وwhile
خط إنتاج: تكررت الحلقات عملية لكل عنصر، مثل for وwhile
Explore · ⁨استكشف⁩

Trace a for loop · ⁨تتبع حلقة for⁩

A for loop runs a fixed number of times, its counter stepping through a range. Watch the counter and running total advance one pass at a time. · ⁨تعمل حلقة for عدداً ثابتاً من المرات، حيث يمر عدادها عبر نطاق محدد. راقب العداد والمجموع الجاري يتقدمون دورة بدورة.⁩

2.9

Building Complete Selection and Iteration Algorithms · ⁨بناء خوارزميات اختيار وتكرار كاملة⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.9.A: Develop code for standard and original algorithms (without data structures) and determine the result of these algorithms.

  • 2.9.A.1 There are standard algorithms to:
    • identify if an integer is or is not evenly divisible by another integer
    • identify the individual digits in an integer
    • determine the frequency with which a specific criterion is met
    • determine a minimum or maximum value
    • compute a sum or average
العربية

هدف التعلم 2.9.A: تطوير كود للخوارزميات القياسية والأصلية (بدون هياكل بيانات) وتحديد نتائج هذه الخوارزميات.

  • 2.9.A.1 توجد خوارزميات قياسية لـ:
    • تحديد ما إذا كان عدد صحيح يقبل القسمة على عدد صحيح آخر بدون باقٍ
    • تحديد الأرقام الفردية في عدد صحيح
    • تحديد تكرار تحقيق معيار محدد
    • تحديد قيمة حد أدنى أو حد أعلى
    • حساب مجموع أو متوسط

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

Combine loops and conditions to solve real problems – count, sum, find a maximum, or test a property:

Two integer patterns the exam tests directly use % and /. To read the digits of an integer one at a time, repeatedly take n % 10 (the last digit) and then n = n / 10 (drop it). To test divisibility, n % d == 0 means n is evenly divisible by d. Combine them with a counter to find the frequency with which some criterion is met.

Standard patterns like a running total, a counter, or a flag 标志 (a boolean that records whether something happened) recur throughout the course.

العربية

اجمع الحلقات والشروط لحل مشاكل حقيقية – عدّ، اجمع، أوجد الحد الأقصى، أو اختص خاصية:

int max = arr[0];
for (int k = 1; k < arr.length; k++) {
    if (arr[k] > max) max = arr[k];
}

يستخدم نمطان عدديان يختبرهما الامتحان مباشرة % و/. لقراءة أرقام عدد صحيح واحدة تلو الأخرى، خذ n % 10 (آخر رقم) مراراً، ثم n = n / 10 (تجاهله). لاختبار القابلية للقسمة، يعني n % d == 0 أن n يقبل القسمةENCY على d. ادمجها مع عداد لإيجاد التكرار الذي يتحقق فيه معيار ما.

تتكرر أنماط قياسية مثل المجموع الجاري، العداد، أو علم الإشارة (منطقي يسجل حدوث شيء ما) عبر البرنامج بأكمله.

2.10

String Algorithms · ⁨خوارزميات السلاسل النصية⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.10.A: Develop code for standard and original algorithms that involve strings and determine the result of these algorithms.

  • 2.10.A.1 There are standard string algorithms to:
    • find if one or more substrings have a particular property
    • determine the number of substrings that meet specific criteria
    • create a new string with the characters reversed
العربية

هدف التعلم 2.10.A: تطوير كود لخوارزميات قياسية وأصلية تتضمن نصوصًا وتحديد نتيجة هذه الخوارزميات.

  • 2.10.A.1 هناك خوارزميات نصية قياسية لـ:
    • البحث عما إذا كانت واحدة أو أكثر من النصوص الجزئية تمتلك خاصية معينة
    • تحديد عدد النصوص الجزئية التي تلبي معايير محددة
    • إنشاء نص جديد مع عكس الأحرف

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

Loop through a string by index to process each character:

Typical tasks: count occurrences, build a reversed or filtered copy, or test whether one string contains another.

العربية

تكرّر عبر سلسلة نصية بالمؤشر لمعالجة كل حرف:

for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    // count vowels, reverse, check for a substring, ...
}

المهام النموذجية: عدّ التكرارات، بناء نسخة مقلوبة أو مصفاة، أو اختبار هل تحتوي سلسلة نصية على أخرى.

2.11

Nested Iteration · ⁨التكرار المتداخل⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.11.A: Develop code to represent nested iterative processes and determine the result of these processes.

  • 2.11.A.1 Nested iteration statements are iteration statements that appear in the body of another iteration statement. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue to its next iteration.
العربية

هدف التعلم 2.11.A: تطوير كود لتمثيل العمليات التكرارية المتداخلة وتحديد نتيجة هذه العمليات.

  • 2.11.A.1 الجمل التكرارية المتداخلة هي جمل تكرار تظهر في جسم جملة تكرار أخرى. عندما يكون حلقة مفرغة متداخلة داخل حلقة أخرى، يجب أن تكمل الحلقة الداخلية جميع تكراراتها قبل أن تتمكن الحلقة الخارجية من الاستمرار إلى تكرارها التالي.

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English

A nested loop 嵌套循环 puts one loop inside another; the inner loop completes fully for each pass of the outer. If the outer runs $n$ times and the inner $m$ times, the body runs $n\times m$ times – the basis for processing grids and comparing all pairs.

العربية

تضع الحلقة المتداخلة حلقة داخل أخرى؛ تكتمل الحلقة الداخلية بالكامل لكل مرور من الحلقة الخارجية. إذا مرت الحلقة الخارجية $n$ مرات والحلقة الداخلية $m$ مرة، فإن الجسم يعمل $n\times m$ مرة – وهو أساس معالجة الشبكات ومقارنة جميع الأزواج.

2.12

Informal Run-Time Analysis · ⁨التحليل غير الرسمي لوقت التشغيل⁩

Syllabus · ⁨المنهج⁩
English

Learning Objective 2.12.A: Calculate statement execution counts and informal run-time comparison of iterative statements.

  • 2.12.A.1 A statement execution count indicates the number of times a statement is executed by the program. Statement execution counts are often calculated informally through tracing and analysis of the iterative statements.
العربية

هدف التعلم 2.12.A: حساب عدد تنفيذ الجمل والمقارنة غير الرسمية لوقت تشغيل الجمل التكرارية.

  • 2.12.A.1 عدد تنفيذ الجملة يشير إلى عدد المرات التي يتم فيها تنفيذ جملة بواسطة البرنامج. غالبًا ما يتم حساب عدد تنفيذ الجمل بشكل غير رسمي من خلال تتبع وتحليل الجمل التكرارية.

Source: College Board AP Course and Exam Description · ⁨المصدر: وصف دورة وامتحان College Board AP⁩

English
Big-O growth rates

Run-time analysis 运行时间分析 counts how many basic steps an algorithm takes as the input size $n$ grows. Count the executions of the innermost statement: a single loop over $n$ items is linear ($n$ steps); two nested loops over $n$ are quadratic ($n^2$). This informal counting lets you compare two algorithms' efficiency.

Exam skill: for a nested loop, be able to state how many times the inner statement runs in terms of the loop bounds – a frequent multiple-choice question.

Worked example. How many stars does this print?

The inner loop runs i times for each outer i: 0 + 1 + 2 + 3 = 6 stars. When the inner bound is the outer variable, the total is the triangular sum $0+1+\dots+(n-1)=\dfrac{n(n-1)}{2}$ – here $\dfrac{4\times3}{2}=6$ – not the full $n^2=16$ of a rectangular nested loop.

العربية
معدلات نمو Big-O

التحليل الزمني يحسب عدد الخطوات الأساسية التي يأخذها الخوارزمية كلما كبر حجم الإدخال $n$. احسب تنفيذ العبارة الداخلية: حلقة واحدة على $n$ عنصر هي خطية ($n$ خطوة)؛ حلقتان متداخلتان على $n$ هما مربعتان ($n^2$). يسمح هذا الحساب غير الرسمي بمقارنة كفاءة خوارزميتين.

كيف ينمو وقت التنفيذ مع عدد العناصر n
كيف ينمو وقت التشغيل مع عدد العناصر n

مهارة الامتحان: بالنسبة للحلقة المتداخلة، كن قادراً على stating كم مرة تعمل العبارة الداخلية بدلالة حدود الحلقات – سؤال خيارات متعددة شائع.

مثال محلول. كم نجمة يطبع هذا؟

for (int i = 0; i < 4; i++)
    for (int j = 0; j < i; j++)
        System.out.print("*");

يُنفَّذ الحلقة الداخلية i مرة لكل حلقة خارجية i: 0 + 1 + 2 + 3 = 6 نجوم. عندما يكون الحد الداخلي هو المتغير الخارجي، يكون المجموع مجموع المثلثات $0+1+\dots+(n-1)=\dfrac{n(n-1)}{2}$ – وهنا $\dfrac{4\times3}{2}=6$ – وليس المجموع الكامل $n^2=16$ للحلقات المتداخلة المستطيلة.

Explore · ⁨استكشف⁩

Compare how algorithms scale · ⁨قارن كيفية توسع الخوارزميات⁩

Run-time describes how the number of steps grows with the input size $n$. Increase $n$ and watch a linear $O(n)$ pull far ahead of a quadratic $O(n^2)$. · ⁨وقت التشغيل يصف كيف ينمو عدد الخطوات مع حجم الإدخال $n$. زِد $n$ وراقب كيف يسبق خطي $O(n)$ تربيعي $O(n^2)$ مسافة كبيرة.⁩

2.12

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

English
  • Get boundary conditions right: use < vs <= deliberately, and watch the first and last iteration of every loop (off-by-one is the classic bug).
  • Build compound conditions with &&, ||, ! and remember short-circuit evaluation (put the null check first).
  • Trace nested loops by counting how many times the inner body runs in total.
  • Choose the right structure — if/else if for ranges, a loop for repetition — and avoid an infinite loop by updating the loop variable.
  • Apply De Morgan's laws when you simplify or negate a boolean condition.
العربية
  • تأكد من شروط الحدود بشكل صحيح: استخدم < مقابل <= بوعي، وراقب التكرار الأول والأخير لكل حلقة (الخطأ الشائع هو off-by-one).
  • قم ببناء الشروط المركبة باستخدام &&، ||، و! وتذكر تقييم التقاطع القصير (ضع التحقق من القيم الفارغة أولاً).
  • تتبع الحلقات المتداخلة عن طريق حساب عدد مرات تنفيذ الجسم الداخلي إجماليًا.
  • اختر الهيكل المناسب – if/else if للمدارات، والحلقة للتكرار – وتجنب الحلقة اللانهائية عن طريق تحديث متغير الحلقة.
  • طبق قوانين دي مورغان عند تبسيط أو نفي شرط منطقي.

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

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

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

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

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

IGCSE, A-Level & AP