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.
ทำซ้ำด้วยการวนซ้ำ
- Iteration หมายถึงการทำขั้นตอนเดิมซ้ำแล้วซ้ำอีก
- Loom ช่วยให้คุณไม่ต้องเขียนบรรทัดเดิมหลายครั้ง
- AP CSP ให้ความสำคัญกับ loops เพราะทำให้โค้ดน้อยทำงานได้มาก
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):重复 bodynครั้งrange(5)ให้0, 1, 2, 3, 4(หยุด ก่อน 5)- Loop body 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.
สร้างผลรวม (accumulation)
- Accumulator คือตัวแปรที่เพิ่มขึ้นภายใน loop
- เริ่มที่
0ก่อน loop - เพิ่มเข้าไปทุกครั้งที่วนเพื่อสร้างผลรวมสะสม
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.
Loop จนกว่าจะมีเงื่อนไขด้วย while
- A
whileloop repeats as long as condition isTrue - สิ่งบางอย่างภายในต้องเคลื่อนที่เข้าใกล้การทำให้เป็น
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.
Loop over a list (for-each)
for item in mylist:เข้าถึงแต่ละรายการตามลำดับ- Loop variable เก็บค่าหนึ่งต่อรอบ
- นี่คือวิธีที่สะอาดที่สุดในการประมวลผลทุกค่า
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=%.
In AP CSP pseudocode
- CB มีสามรูปแบบ loop ที่ตรงกับ Python loops
REPEAT n TIMES= จำนวนคงที่;REPEAT UNTIL(cond)= loop จนกว่าจะเป็นจริงFOR EACH x IN list= for-each loop.DISPLAY= print,←==,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)runs0ถึงn - 1- เปลี่ยนเงื่อนไขภายใน
whileมิฉะนั้นจะไม่หยุด
Now you try
- Use a loop in each task.
- Press Check answer to test your code.
ลองดูเลย
- ใช้ loop ในแต่ละโจทย์
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Iteration repeats the body · Iteration ทำซ้ำส่วนประกอบ
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
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] Loop ด้วย for-each และนับว่ามีค่า คู่ กี่ตัว (ใช้ % 2 == 0) เก็บจำนวนนับใน evens (ควรได้ 3)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่