Iteration · التكرار
Repeat with iteration
- Iteration means doing the same steps again and again.
- Loops save you from writing the same line many times.
- AP CSP cares about loops because they let a little code do a lot of work.
التكرار باستخدام التكرارية
- التكرارية تعني تنفيذ نفس الخطوات مرارًا وتكرارًا.
- الحلقات توفر عليك كتابة نفس السطر مرات عديدة.
- تهتم AP CSP بالحلول لأنها تسمح للكود الصغير بإجراء الكثير من العمل.
Count with for and range
for i in range(n):repeats the bodyntimes.range(5)gives0, 1, 2, 3, 4(it stops before 5).- The loop body is indented, like an
if.
العد باستخدام for و range
for i in range(n):يكرر الجسمnمرة.range(5)يعطي0, 1, 2, 3, 4(يتوقف قبل 5).- جسم الحلقة مُزاح مسافة بادئة، مثل
if.
for i in range(5):
print(i)
Build a total (accumulation)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it each pass to build a running total.
بناء المجموع (التراكم)
- الراكم هو متغير ينمو داخل الحلقة.
- ابدأه عند
0قبل الحلقة. - أضف إليه في كل دورة لبناء مجموع تراكمي.
total = 0
for n in range(1, 6):
total = total + n
print(total)
Loop until a condition with while
- A
whileloop repeats as long as its condition isTrue. - Something inside must move toward making it
False. - Use it when you do not know the count in advance.
حلقة حتى شرط باستخدام while
- حلقة
whileتتكرر طالما كان شرطهاTrue. - يجب أن شيء ما داخلها يتحرك نحو جعلها
False. - استخدمها عندما لا تعرف العدد مسبقًا.
count = 3
while count > 0:
print(count)
count = count - 1
Loop over a list (for-each)
for item in mylist:visits each item in turn.- The loop variable holds one item per pass.
- This is the clean way to process every value.
تكرار القائمة (لكل عنصر)
for item in mylist:يزور كل عنصر على التوالي.- يحمل متغير الحلقة عنصرًا واحدًا في كل دورة.
- هذه هي الطريقة النظيفة لمعالجة كل قيمة.
scores = [10, 20, 30]
for s in scores:
print(s)
In AP CSP pseudocode
- CB has three loop shapes that match Python loops.
REPEAT n TIMES= a fixed count;REPEAT UNTIL(cond)= loop until true.FOR EACH x IN list= a for-each loop.DISPLAY= print,←==,MOD=%.
في الكود الوهمي لـ AP CSP
- تمتلك CB ثلاث أشكال للحلقات تطابق حلقات بايثون.
REPEAT n TIMES= عدد ثابت؛REPEAT UNTIL(cond)= حلقة حتى تصبح صحيحة.FOR EACH x IN list= حلقة لكل عنصر.DISPLAY= طباعة،←==،MOD=%.
REPEAT 5 TIMES
{
DISPLAY "hi"
}
count ← 3
REPEAT UNTIL (count = 0)
{
DISPLAY count
count ← count - 1
}
FOR EACH s IN scores
{
DISPLAY s
}
Common mistakes
range(n)runs0ton - 1.- Change the condition inside a
while, or it never stops.
أخطاء شائعة
range(n)تشغل من0إلىn - 1.- غيّر الشرط داخل
while، وإلا فلن تتوقف أبدًا.
Now you try
- Use a loop in each task.
- Press Check answer to test your code.
الآن جرب بنفسك
- استخدم حلقة في كل مهمة.
- اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
Iteration repeats the body · التكرار يكرر الجسم
A loop repeats; a counter and a total change each pass. · الحلقة تتكرر؛ عداد و مجموع يتغيران في كل دورة.
Use a for loop with range(...) to add the whole numbers 1 through 10. Store the result in total. (It should be 55.) · استخدم حلقة for مع range(...) لجمع الأعداد الصحيحة من 1 حتى 10. احفظ النتيجة في total. (يجب أن تكون 55.)
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Use a while loop to print the numbers 5, 4, 3, 2, 1, each on its own line, then print Go. Start from n = 5. · استخدم حلقة while لطباعة الأعداد 5, 4, 3, 2, 1، كل رقم في سطر منفصل، ثم اطبع Go. ابدأ من n = 5.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
nums is [3, 6, 7, 8, 10, 11]. Loop over it with a for-each loop and count how many values are even (use % 2 == 0). Store the count in evens. (It should be 3.) · nums هي [3, 6, 7, 8, 10, 11]. مرر عليها بحلقة for-eck وحسب كم عدد القيم الزوجية (استخدم % 2 == 0). احفظ العداد في evens. (يجب أن يكون 3.)
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.