while loop: repeat while a condition is true While Loop: วนตราบที่เงื่อนไขจริง
Use while when you do not know the number of rounds in advance — update a variable in the loop so it does not run forever
ใช้ while เมื่อยังไม่รู้จำนวนรอบล่วงหน้า — ต้องอัปเดตตัวแปรในลูปเพื่อไม่ให้วนไม่รู้จบ
while condition: runs the block again and again as long as the condition is still True.
Unlike for, which is usually used when the sequence or number of rounds is known, with
while you must make sure the condition eventually becomes false (for example, by increasing a
counter).
while เงื่อนไข: จะรันบล็อกซ้ำๆ ตราบที่เงื่อนไขยังเป็น True
ต่างจาก for ที่มักใช้เมื่อรู้ลำดับ/จำนวนรอบชัดเจน —
กับ while ต้องดูแลเองว่าเมื่อไหร่เงื่อนไขจะกลายเป็นเท็จ (เช่น เพิ่มตัวนับ)
1 while counting up to N 1 while นับขึ้นจนถึง N
Start a counter and increase it in the loop until the condition is false — then the loop stops.
เริ่มตัวนับแล้วเพิ่มค่าในลูปจนเงื่อนไขเป็นเท็จ — ลูปจึงหยุด
0 1 2
2 while counting down / multiple conditions 2 while ลดค่า / เงื่อนไขหลายตัว
The condition is checked every round before entering the block — you can count down, or combine
conditions with and.
เงื่อนไขจะเช็คทุกรอบก่อนเข้าบล็อก — ลดค่าหรือรวมเงื่อนไขด้วย and ได้
3 2 1
3 break in a while 3 break ใน while
break leaves the while at once, even if the condition is still true.
break ออกจาก while ทันที แม้เงื่อนไขยังเป็นจริงอยู่
0 1 2
4 continue in a while 4 continue ใน while
continue skips the rest of the round and goes back to check the while condition again. Warning:
update the counter before continue, or the loop may never end.
continue ข้ามส่วนที่เหลือของรอบแล้วกลับไปเช็คเงื่อนไข while ใหม่
ระวัง: ต้องอัปเดตตัวนับก่อน continue มิฉะนั้นอาจวนไม่รู้จบ
1 3 4
5 A quick comparison with for 5 เทียบสั้นๆ กับ for
When the number of rounds is clearly known (e.g. 0–2), for + range reads more
easily. This gives a similar result to the count-up while, but written with for.
เมื่อรู้จำนวนรอบชัดเจน (เช่น 0–2) ใช้ for + range อ่านง่าย
ตัวอย่างนี้ให้ผลคล้าย while นับขึ้น แต่เขียนด้วย for
0 1 2
6 Saving until a goal 6 ออมเงินจนถึงเป้าหมาย
When you do not know in advance how many rounds are needed, use while. Here 1000 baht grows by 5% each year and the loop repeats while the money is below the 1500 goal, counting the years.
เมื่อไม่รู้ล่วงหน้าว่าต้องวนกี่รอบ ให้ใช้ while ตัวอย่างนี้เงิน 1000 บาทเพิ่มขึ้น 5% ทุกปี และลูปจะวนตราบใดที่เงินยังต่ำกว่าเป้าหมาย 1500 พร้อมนับจำนวนปี
9 years 1551.33
7 Common mistakes 7 ข้อผิดพลาดที่พบบ่อย
-
An infinite loop. If nothing inside the loop changes the condition, it never ends. Add a safety counter, as in the demo where
nis never updated butroundsstops the loop. -
ลูปไม่รู้จบ ถ้าไม่มีอะไรในลูปเปลี่ยนเงื่อนไข ลูปจะไม่จบ ให้เพิ่มตัวนับกันไว้ เหมือนตัวอย่างที่
nไม่เคยถูกอัปเดต แต่roundsหยุดลูปได้ -
< instead of <=.
while i <= 3prints 1, 2, 3 butwhile i < 3stops after 2. Decide whether the last value should be included. -
ใช้ < แทน <=
while i <= 3พิมพ์ 1, 2, 3 แต่while i < 3หยุดหลัง 2 ต้องตัดสินใจว่าค่าสุดท้ายรวมด้วยหรือไม่ -
while Truewithoutbreak. A loop that is meant to stop from the inside needs abreakon some path, or it runs forever. -
while Trueที่ไม่มีbreakลูปที่ตั้งใจให้หยุดจากข้างใน ต้องมีbreakอย่างน้อยหนึ่งเส้นทาง ไม่เช่นนั้นจะวนตลอดไป
5 1 2 3
8 Think further 8 แบบฝึกคิดต่อ
Try these yourself in the Run boxes above (or in Google Colab):
ลองทำเองในกล่อง Run ด้านบน (หรือใน Google Colab):
-
Give the user at most 3 attempts to guess a password. Stop early when the guess is right. Hint: a counter and
break. -
ให้ผู้ใช้เดารหัสผ่านได้มากสุด 3 ครั้ง และหยุดทันทีเมื่อเดาถูก Hint: ตัวนับและ
break -
Rewrite
for i in range(1, 6): print(i)as awhileloop. Which version is easier to get wrong? -
เขียน
for i in range(1, 6): print(i)ใหม่เป็นลูปwhileแบบไหนพลาดง่ายกว่า - Halve the number 100 again and again until it is smaller than 1, and count the steps.
- หาร 100 ครึ่งหนึ่งซ้ำ ๆ จนน้อยกว่า 1 แล้วนับจำนวนครั้ง
Key takeaways สรุปสำคัญ
Remember these points when you write your own programs:
จำประเด็นเหล่านี้ไว้เมื่อเขียนโปรแกรมของคุณเอง:
-
A
whileloop repeats while its condition is true, so something inside the loop must eventually make the condition false. -
ลูป
whileวนซ้ำตราบใดที่เงื่อนไขเป็นจริง จึงต้องมีบางอย่างในลูปที่ทำให้เงื่อนไขเป็นเท็จในที่สุด -
Use
forwhen you know the number of rounds or the items to visit, andwhilewhen the number of rounds depends on the data. -
ใช้
forเมื่อรู้จำนวนรอบหรือรายการที่ต้องวน และใช้whileเมื่อจำนวนรอบขึ้นกับข้อมูล -
A counter or an update line (
i += 1) is the usual way to move the loop toward its end — check it whenever a loop never stops. -
ตัวนับหรือบรรทัดอัปเดต (
i += 1) คือวิธีปกติที่พาลูปไปสู่จุดจบ ตรวจสิ่งนี้เมื่อลูปไม่ยอมหยุด -
breakleaves the loop at once;continueskips to the next round. Both keep loops readable when used sparingly. -
breakออกจากลูปทันที ส่วนcontinueข้ามไปรอบถัดไป ทั้งคู่ช่วยให้ลูปอ่านง่ายเมื่อใช้แต่พอดี
Note: Run on this page is a JavaScript simulation, not real Python. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง หากต้องการทดลอง Python จริง แนะนำ Google Colab
References References / แหล่งอ้างอิง
Sources used for this lesson:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
-
The while statement —
Python Language Reference — while
(
while/break/continue)