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.

เริ่มตัวนับแล้วเพิ่มค่าในลูปจนเงื่อนไขเป็นเท็จ — ลูปจึงหยุด

[1]
[output]
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 ได้

[2]
[output]
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 ทันที แม้เงื่อนไขยังเป็นจริงอยู่

[3]
[output]
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 มิฉะนั้นอาจวนไม่รู้จบ

[4]
[output]
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

[5]
[output]
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 พร้อมนับจำนวนปี

[6]
[output]
9 years
1551.33

7 Common mistakes 7 ข้อผิดพลาดที่พบบ่อย

  1. An infinite loop. If nothing inside the loop changes the condition, it never ends. Add a safety counter, as in the demo where n is never updated but rounds stops the loop.
  2. ลูปไม่รู้จบ ถ้าไม่มีอะไรในลูปเปลี่ยนเงื่อนไข ลูปจะไม่จบ ให้เพิ่มตัวนับกันไว้ เหมือนตัวอย่างที่ n ไม่เคยถูกอัปเดต แต่ rounds หยุดลูปได้
  3. < instead of <=. while i <= 3 prints 1, 2, 3 but while i < 3 stops after 2. Decide whether the last value should be included.
  4. ใช้ < แทน <= while i <= 3 พิมพ์ 1, 2, 3 แต่ while i < 3 หยุดหลัง 2 ต้องตัดสินใจว่าค่าสุดท้ายรวมด้วยหรือไม่
  5. while True without break. A loop that is meant to stop from the inside needs a break on some path, or it runs forever.
  6. while True ที่ไม่มี break ลูปที่ตั้งใจให้หยุดจากข้างใน ต้องมี break อย่างน้อยหนึ่งเส้นทาง ไม่เช่นนั้นจะวนตลอดไป
[7]
[output]
5
1
2
3

8 Think further 8 แบบฝึกคิดต่อ

Try these yourself in the Run boxes above (or in Google Colab):

ลองทำเองในกล่อง Run ด้านบน (หรือใน Google Colab):

  1. Give the user at most 3 attempts to guess a password. Stop early when the guess is right. Hint: a counter and break.
  2. ให้ผู้ใช้เดารหัสผ่านได้มากสุด 3 ครั้ง และหยุดทันทีเมื่อเดาถูก Hint: ตัวนับและ break
  3. Rewrite for i in range(1, 6): print(i) as a while loop. Which version is easier to get wrong?
  4. เขียน for i in range(1, 6): print(i) ใหม่เป็นลูป while แบบไหนพลาดง่ายกว่า
  5. Halve the number 100 again and again until it is smaller than 1, and count the steps.
  6. หาร 100 ครึ่งหนึ่งซ้ำ ๆ จนน้อยกว่า 1 แล้วนับจำนวนครั้ง

Key takeaways สรุปสำคัญ

Remember these points when you write your own programs:

จำประเด็นเหล่านี้ไว้เมื่อเขียนโปรแกรมของคุณเอง:

  1. A while loop repeats while its condition is true, so something inside the loop must eventually make the condition false.
  2. ลูป while วนซ้ำตราบใดที่เงื่อนไขเป็นจริง จึงต้องมีบางอย่างในลูปที่ทำให้เงื่อนไขเป็นเท็จในที่สุด
  3. Use for when you know the number of rounds or the items to visit, and while when the number of rounds depends on the data.
  4. ใช้ for เมื่อรู้จำนวนรอบหรือรายการที่ต้องวน และใช้ while เมื่อจำนวนรอบขึ้นกับข้อมูล
  5. 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.
  6. ตัวนับหรือบรรทัดอัปเดต (i += 1) คือวิธีปกติที่พาลูปไปสู่จุดจบ ตรวจสิ่งนี้เมื่อลูปไม่ยอมหยุด
  7. break leaves the loop at once; continue skips to the next round. Both keep loops readable when used sparingly.
  8. 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:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. The while statement — Python Language Reference — while (while / break / continue)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

When does a while loop repeat?while วนซ้ำเมื่อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) As long as the condition is still trueตราบที่เงื่อนไขยังเป็นจริง

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) As long as the condition is still trueตราบที่เงื่อนไขยังเป็นจริง
    Correct! while checks the condition every round before entering the block.ถูกต้อง! while เช็คเงื่อนไขทุกรอบก่อนเข้าบล็อก
  • (B) Only for the number of rounds given in a rangeเฉพาะจำนวนรอบที่ระบุใน range เท่านั้น
    Wrong: that is more like for + range.ผิด: นั่นเป็นลักษณะของ for + range มากกว่า
  • (C) Always exactly onceเพียงครั้งเดียวเสมอ
    Wrong: while can run many rounds.ผิด: while วนได้หลายรอบ
  • (D) Only when there is an exceptionเมื่อมี exception เท่านั้น
    Wrong: it has nothing to do with exceptions.ผิด: ไม่เกี่ยวกับ exception

ข้อ 2

2 / 10

Why must you update a variable in a while loop (e.g. n += 1)?ทำไมต้องอัปเดตตัวแปรในลูป while (เช่น n += 1)?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) So the condition can eventually become false and stop the loopเพื่อให้เงื่อนไขมีโอกาสเป็นเท็จและหยุดลูป

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Because Python requires a += in every whileเพราะ Python บังคับให้มี += ในทุก while
    Wrong: it is not a syntax rule, but you must design it to stop.ผิด: ไม่ได้บังคับ syntax แต่ต้องออกแบบให้หยุดได้
  • (B) So the condition can eventually become false and stop the loopเพื่อให้เงื่อนไขมีโอกาสเป็นเท็จและหยุดลูป
    Correct! Without updating it, the loop may run forever (infinite loop).ถูกต้อง! ไม่อัปเดตอาจวนไม่รู้จบ (infinite loop)
  • (C) To convert while into for automaticallyเพื่อแปลง while เป็น for โดยอัตโนมัติ
    Wrong: it does not change the loop type.ผิด: ไม่ได้แปลงชนิดลูป
  • (D) To make except runเพื่อให้ except ทำงาน
    Wrong: it has nothing to do with try/except.ผิด: ไม่เกี่ยวกับ try/except

ข้อ 3

3 / 10

What does break do in a while loop?break ใน while ทำให้เกิดอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) Leaves the loop immediatelyออกจากลูปทันที

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Skips the round and keeps looping by the conditionข้ามรอบแล้ววนต่อตามเงื่อนไข
    Wrong: that is closer to continue.ผิด: นั่นใกล้เคียง continue
  • (B) Leaves the loop immediatelyออกจากลูปทันที
    Correct! break stops the while at once.ถูกต้อง! break หยุด while เลย
  • (C) Sets the condition to True foreverตั้งเงื่อนไขเป็น True ตลอดไป
    Wrong: it does not force the condition to True.ผิด: ไม่ได้บังคับเงื่อนไขเป็น True
  • (D) Deletes the variable n from memoryลบตัวแปร n ออกจากหน่วยความจำ
    Wrong: it does not delete a variable.ผิด: ไม่ได้ลบตัวแปร

ข้อ 4

4 / 10

When using continue in a while, what must you watch out for?ใน while เมื่อใช้ continue ควรระวังเรื่องใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) Update the counter before continue, or the loop may never endต้องอัปเดตตัวนับก่อน continue มิฉะนั้นอาจวนไม่รู้จบ

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Never use it with printห้ามใช้ร่วมกับ print
    Wrong: print is fine.ผิด: ใช้ print ได้ตามปกติ
  • (B) It must be on the same line as whileต้องเขียนในบรรทัดเดียวกับ while
    Wrong: continue is inside the indented block.ผิด: continue อยู่ในบล็อกที่ย่อหน้า
  • (C) Update the counter before continue, or the loop may never endต้องอัปเดตตัวนับก่อน continue มิฉะนั้นอาจวนไม่รู้จบ
    Correct! If you skip updating the counter, the condition may never change.ถูกต้อง! ถ้าข้ามการอัปเดตตัวนับ เงื่อนไขอาจไม่เปลี่ยน
  • (D) It always needs a matching exceptต้องมี except คู่เสมอ
    Wrong: no try/except is needed.ผิด: ไม่จำเป็นต้องมี try/except

ข้อ 5

5 / 10

When does for fit better than while?เมื่อใดเหมาะใช้ for มากกว่า while?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) When the round count or sequence is clearly known, e.g. range(3)เมื่อรู้จำนวนรอบหรือลำดับชัดเจน เช่น range(3)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) When looping until the user types a special command, with no known round countเมื่อต้องการวนจนกว่าผู้ใช้จะพิมพ์คำสั่งพิเศษโดยไม่รู้จำนวนรอบ
    Wrong: an unknown round count usually fits while better.ผิด: สถานการณ์ไม่รู้จำนวนรอบมักเหมาะกับ while มากกว่า
  • (B) When you only want to catch a ZeroDivisionErrorเมื่อต้องการจับ ZeroDivisionError เท่านั้น
    Wrong: that is a try/except job.ผิด: งานนั้นใช้ try/except
  • (C) When you only want to use case _เมื่อต้องการใช้ case _ เท่านั้น
    Wrong: case belongs to match, not the for/while choice.ผิด: case อยู่ใน match ไม่ใช่การเลือก for/while
  • (D) When the round count or sequence is clearly known, e.g. range(3)เมื่อรู้จำนวนรอบหรือลำดับชัดเจน เช่น range(3)
    Correct! for + range reads well when the count is known.ถูกต้อง! for + range อ่านง่ายเมื่อรู้จำนวนรอบ

ข้อ 6

6 / 10

With n = 0; while n < 3: print(n); n += 1, what is printed?จาก n = 0; while n < 3: print(n); n += 1 พิมพ์อะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 0, 1, 20, 1, 2

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 0, 1, 20, 1, 2
    Correct! It loops while n < 3, i.e. n = 0, 1, 2.ถูกต้อง! วนขณะ n < 3 คือ n = 0, 1, 2
  • (B) 0, 1, 2, 30, 1, 2, 3
    Wrong: when n is 3, the condition n < 3 is false.ผิด: เมื่อ n เป็น 3 เงื่อนไข n < 3 เป็นเท็จ
  • (C) 1, 2, 31, 2, 3
    Wrong: printing starts at n = 0.ผิด: เริ่มพิมพ์ที่ n = 0
  • (D) It loops foreverวนไม่รู้จบ
    Wrong: n += 1 eventually makes the condition false.ผิด: n += 1 ทำให้เงื่อนไขเป็นเท็จในที่สุด

ข้อ 7

7 / 10

When is a while condition checked?เงื่อนไขของ while ถูกเช็คเมื่อใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) Before entering the block, every roundก่อนเข้าบล็อกในทุกรอบ

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Only after the block finishesหลังจากรันบล็อกจบแล้วเท่านั้น
    Wrong: it is checked before entering the block each round.ผิด: เช็คก่อนเข้าบล็อกทุกรอบ
  • (B) Once at the start of the loopครั้งเดียวตอนเริ่มลูป
    Wrong: it is checked every round, not once.ผิด: เช็คทุกรอบ ไม่ใช่ครั้งเดียว
  • (C) Before entering the block, every roundก่อนเข้าบล็อกในทุกรอบ
    Correct! If it is false from the start, the block never runs.ถูกต้อง! ถ้าเป็นเท็จตั้งแต่แรก บล็อกจะไม่รันเลย
  • (D) Only when there is a breakเฉพาะเมื่อมี break
    Wrong: checking the condition does not depend on break.ผิด: การเช็คเงื่อนไขไม่ขึ้นกับ break

ข้อ 8

8 / 10

What happens with while True: and no break?while True: โดยไม่มี break จะเกิดอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) An infinite loopวนไม่รู้จบ (infinite loop)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) It runs once and stopsรันครั้งเดียวแล้วหยุด
    Wrong: the condition is always True.ผิด: เงื่อนไขเป็น True เสมอ
  • (B) It never runsไม่รันเลย
    Wrong: True is true, so the block runs.ผิด: True เป็นจริง จึงเข้าบล็อก
  • (C) A SyntaxErrorเกิด SyntaxError
    Wrong: the syntax is valid; it is a non-terminating logic.ผิด: ไวยากรณ์ถูกต้อง แต่เป็น logic ที่ไม่หยุด
  • (D) An infinite loopวนไม่รู้จบ (infinite loop)
    Correct! You need a break or a changing condition to stop it.ถูกต้อง! ต้องมี break หรือเงื่อนไขที่เปลี่ยนได้เพื่อหยุด

ข้อ 9

9 / 10

With n = 3; while n > 0: print(n); n -= 1, what is printed?จาก n = 3; while n > 0: print(n); n -= 1 พิมพ์อะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) 3, 2, 13, 2, 1

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 3, 2, 13, 2, 1
    Correct! It loops while n > 0, decreasing by 1 each round.ถูกต้อง! วนขณะ n > 0 พร้อมลดค่าทีละ 1
  • (B) 3, 2, 1, 03, 2, 1, 0
    Wrong: when n is 0, the condition n > 0 is false.ผิด: เมื่อ n เป็น 0 เงื่อนไข n > 0 เป็นเท็จ
  • (C) 1, 2, 31, 2, 3
    Wrong: this loop counts down.ผิด: ลูปนี้นับถอยหลัง
  • (D) It loops foreverวนไม่รู้จบ
    Wrong: n -= 1 eventually makes the condition false.ผิด: n -= 1 ทำให้เงื่อนไขเป็นเท็จในที่สุด

ข้อ 10

10 / 10

Which is an 'infinite loop' you should avoid?ข้อใดคือ 'infinite loop' ที่ควรหลีกเลี่ยง?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) n = 0; while n < 5: print(n) (forgot n += 1)n = 0; while n < 5: print(n) (ลืม n += 1)

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) for i in range(5): print(i)for i in range(5): print(i)
    Wrong: range(5) is finite, so it stops.ผิด: range(5) จำกัด จึงหยุดเอง
  • (B) n = 0; while n < 5: print(n); n += 1n = 0; while n < 5: print(n); n += 1
    Wrong: n += 1 eventually makes the condition false.ผิด: n += 1 ทำให้เงื่อนไขเป็นเท็จในที่สุด
  • (C) n = 0; while n < 5: print(n) (forgot n += 1)n = 0; while n < 5: print(n) (ลืม n += 1)
    Correct! n never changes, the condition stays true, and it loops forever.ถูกต้อง! n ไม่เปลี่ยน เงื่อนไขจริงตลอด วนไม่รู้จบ
  • (D) while False: print("x")while False: print("x")
    Wrong: the condition is false, so it never runs.ผิด: เงื่อนไขเป็นเท็จ จึงไม่รันเลย