Iteration · איטרציה ⟦number⟧
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).- גוף הלופ מוקדוד (indented), כמו חוסן
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-each)
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 יש שלוש צורות לולאה המתאימות ללולאות Python.
REPEAT n TIMES= ספירה קבועה;REPEAT UNTIL(cond)= לולאה עד לאמת.FOR EACH x IN list= לולאת for-each.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, או she never stops.
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-each ספור כמה ערכים הם זוגיים (השתמש ב-% 2 == 0). אחסן את הספירה ב-evens. (זה אמור להיות 3.)
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.