While loops · ลูป while
Loop while a condition is true
- A
whileloop repeats as long as its condition is true. - It checks the condition before each pass.
- Use it when you do not know the number of repeats in advance.
ทำซ้ำwhileเงื่อนไขเป็นจริง
- ลูป
whileจะทำซ้ำ ตราบใดที่ เงื่อนไขของมันยังเป็นจริง - มันจะตรวจสอบเงื่อนไข ก่อน แต่ละรอบ
- ใช้เมื่อไม่ทราบจำนวนรอบการทำซ้ำล่วงหน้า
Always change the condition
- Something inside the loop must move toward making the condition false.
- If it never becomes false, the loop runs forever (an infinite loop).
- Press Stop if a loop ever gets stuck.
ต้องเปลี่ยนเงื่อนไขเสมอ
- สิ่งบางอย่างภายในลูปต้องเคลื่อนที่ไปสู่การทำให้เงื่อนไขเป็นfalse
- หากมันไม่เป็นfalseตลอดไป ลูปจะรันไปเรื่อยๆ (เป็น infinite loop)
- กด Stop ถ้าลูปติดขัด
count = 3
while count > 0:
print(count)
count = count - 1
break and continue
breakstops the loop right away.continueskips the rest of this pass and goes to the next one.while True:loops forever until abreakends it.
break และ continue
breakจะหยุดลูปทันทีcontinueจะข้ามส่วนที่เหลือของรอบนี้และไปยังรอบถัดไปwhile True:จะวนลูปไปเรื่อยๆ จนกว่าจะเจอbreakที่หยุดมัน
n = 0
while True:
n = n + 1
if n == 4:
break
print(n)
Checking before or after
- A
whileloop checks first, so it may run zero times. - A "repeat until" loop checks after, so it always runs at least once.
- Python has no
repeat; usewhile True:with abreakat the end.
ตรวจสอบก่อนหรือหลัง
- ลูป
whileจะตรวจสอบ ก่อน ดังนั้นอาจรัน ศูนย์ ครั้ง - ลูป "repeat until" จะตรวจสอบ หลัง ดังนั้นจะรัน อย่างน้อย หนึ่งครั้ง
- Python ไม่มี
repeat; ใช้while True:พร้อมbreakที่ท้าย
In Cambridge pseudocode
whileis the pre-condition loop;REPEAT … UNTILis the post-condition loop.
ใน伪代码 Cambridge
whileคือ pre-condition loop;REPEAT … UNTILคือ post-condition loop
Count ← 3
WHILE Count > 0
OUTPUT Count
Count ← Count - 1
ENDWHILE
REPEAT
OUTPUT Count
Count ← Count + 1
UNTIL Count = 3
Common mistakes
- Change something inside the loop so the condition can become false, or it runs forever.
- A
whilechecks its condition first, so it may run zero times.
ข้อผิดพลาดที่พบบ่อย
- เปลี่ยนสิ่งบางอย่างภายในลูปเพื่อให้เงื่อนไขกลายเป็นfalseได้ มิฉะนั้นจะรันไปเรื่อยๆ
- A
whileตรวจสอบเงื่อนไขก่อน ดังนั้นอาจรันศูนย์ครั้ง
Now you try
- Make sure your condition will eventually become false.
- Stuck on the loop? Do the short drill Finish the countdown first. It is one missing line.
- Press Check answer to test your code.
ลองดูเลย
- ตรวจสอบให้แน่ใจว่าเงื่อนไขของคุณจะกลายเป็นfalseในที่สุด
- ติดกับลูป? ทำ drill สั้น Finish the countdown ก่อน มันคือบรรทัดที่ขาดหายไปหนึ่งบรรทัด
- กด Check answer เพื่อทดสอบโค้ดของคุณ
A while loop builds up a value · ลูป while สร้างค่าขึ้นทีละน้อย
A while loop repeats until its condition is false. · ลูป while จะทำซ้ำ จนกว่าเงื่อนไขจะเป็นเท็จ
Use a while loop to count down. Each turn, print the value of count, then make count one smaller. Stop when it reaches 0, so the output is 5, 4, 3, 2, 1 — one per line. · ใช้ลูป while นับถอยหลัง ในแต่ละรอบ ให้พิมพ์ ค่าของ count แล้วทำให้ count น้อยลง 1 หน่วย หยุดเมื่อถึง 0 เพื่อให้ผลลัพธ์คือ 5, 4, 3, 2, 1 — Each on a new line.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Use a while loop to add up the numbers 1 to 10. Store the total in total (the answer is 55). · ใช้ลูป while เพื่อรวมจำนวน 1 ถึง 10. เก็บผลรวมลงใน total (คำตอบคือ 55).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Start n at 1 and add 1 each pass. The loop stops too soon: it ends while n is still 1. Change the condition so it keeps going while n * n is at most 50. When it stops, n is the first whole number whose square is over 50. · เริ่ม n ที่ 1 และเพิ่ม 1 ทุกครั้ง ลูปหยุดเร็วเกินไป:它จะจบwhile n ยังเป็น 1 เปลี่ยนเงื่อนไขให้มันไปต่อ while n * n มีค่าน้อยที่สุดหรือเท่ากับ 50 เมื่อหยุด n คือจำนวนเต็มตัวแรกwhose square is over 50
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Same answer, with break. n starts at 1 inside while True:. The if is already inside the loop and n grows after it. Change the condition so you break when n * n is greater than 50. Leave the answer in n (it is 8). Do not add 1 before the test — that makes the answer 9. · คำตอบเดียวกัน ใช้ break. n เริ่มที่ 1 ภายใน while True:. The if อยู่ภายในลูปอยู่แล้วและ n เติบโต after it. เปลี่ยนเงื่อนไขเพื่อให้คุณ break เมื่อ n * n มากกว่า 50. ทิ้งคำตอบไว้ใน n (คือ 8). อย่าเพิ่ม 1 ก่อนการทดสอบ—makes the answer 9.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Start with n = 1 and steps = 0. While n is smaller than 1000, double n and add 1 to steps. When the loop ends, n is 1024. How many doublings did it take? Leave the answer in steps. · เริ่มด้วย n = 1 และ steps = 0. While n น้อยกว่า 1000, double n และเพิ่ม 1 เข้ากับ steps. When the loop ends, n is 1024. It took how many doublings? Leave the answer in steps.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่