Iteration — loops · การทำซ้ำ — ลูป
Count-controlled loops
- Iteration means repeating steps.
- A count-controlled loop repeats a fixed number of times.
- In Python that is
for i in range(n):.
Count-controlled loops
- Iteration means repeating steps.
- ลูปแบบ ควบคุมด้วยนับจำนวน จะทำซ้ำจำนวนครั้งที่กำหนดไว้ล่วงหน้า
- In Python that is
for i in range(n):.
for i in range(5):
print("Hello")
Choosing the range
range(5)gives0, 1, 2, 3, 4.range(1, 6)gives1, 2, 3, 4, 5— it stops before 6.range(2, 11, 2)counts in steps of 2.
Choosing the range
range(5)ให้0, 1, 2, 3, 4- คำสั่ง
range(1, 6)จะให้ผลลัพธ์1, 2, 3, 4, 5— โดยจะหยุด ก่อน ถึง 6 range(2, 11, 2)counts in steps of 2.
for n in range(1, 6):
print(n)
Pre-condition loops: while
- A pre-condition loop tests before each pass.
- It may run zero times if the test is false at the start.
Pre-condition loops: while
- ลูปแบบ เงื่อนไขก่อน (pre-condition) จะทดสอบเงื่อนไข ก่อน การวนลูปแต่ละรอบ
- ลูปนี้อาจ被执行 ศูนย์ ครั้งหากเงื่อนไขเป็นเท็จตั้งแต่เริ่มต้น
count = 0
while count < 5:
print(count)
count = count + 1
Post-condition loops: REPEAT UNTIL
- A post-condition loop tests after each pass, so it always runs at least once.
- Python has no
repeat, so we usewhile Truewithbreak.
ลูปแบบเงื่อนไขหลัง: REPEAT UNTIL
- ลูปแบบ เงื่อนไขหลัง จะทดสอบเงื่อนไข หลังจาก การวนลูปแต่ละรอบ ดังนั้นจึง被执行 อย่างน้อยหนึ่ง ครั้งเสมอ
- Python ไม่มีคำสั่ง
repeatเราจึงใช้while Trueร่วมกับbreakแทน
total = 0
while True:
total = total + 1
if total >= 3:
break
print(total)
Each pass changes count · แต่ละรอบเปลี่ยน count
A loop repeats its body; count must change so the test finally fails. · ลูปทำซ้ำ body ของมัน; count ต้องเปลี่ยนเพื่อให้การทดสอบล้มเหลวในที่สุด
In the exam: FOR, WHILE, REPEAT
- Pseudocode has all three loop types.
ในข้อสอบ: FOR, WHILE, REPEAT
- ปсевโคดมีลูปทั้งสามประเภทนี้
FOR I ← 1 TO 5
OUTPUT I
NEXT I
Common mistakes
- A
forloop repeats a known number of times; awhilerepeats until something changes. range(n)runs0ton - 1.- Change the condition inside a
while, or it never ends.
ข้อผิดพลาดที่พบบ่อย
- ลูป
forจะทำซ้ำจำนวนครั้งKnown; ส่วนลูปwhileจะทำซ้ำจนกว่าจะมีอะไรเปลี่ยนแปลง range(n)runs0ถึงn - 1- เปลี่ยนเงื่อนไขภายในลูป
whileมิฉะนั้นลูปจะไม่มีวันสิ้นสุด
Now you try
- Pick the loop that fits each task.
- Press Check answer to test your code.
ลองดูเลย
- เลือกลูปที่ตรงกับแต่ละงาน
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Use a for loop to print the numbers 1 to 5, one on each line. · ใช้ลูป for เพื่อพิมพ์เลข 1 ถึง 5, เลขละหนึ่งบรรทัด
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Use a loop to add up the numbers 1 to 10, then print the total (it is 55). · ใช้ลูปเพื่อบวกตัวเลขตั้งแต่ 1 ถึง 10 แล้วพิมพ์ผลรวม (คำตอบคือ 55)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Read a whole number n, then count down from n to 1, one per line. For input 3, print 3, 2, 1. · อ่านจำนวนเต็ม n จากนั้นนับถอยหลังจาก n ไปยัง 1 บรรทัดละหนึ่งจำนวน สำหรับอินพุต 3 ให้พิมพ์ 3, 2, 1
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่