if / elif / else: choose a path based on a condition If / Elif / Else: เลือกทางตามเงื่อนไข

Use if elif else to control which block runs — note the indentation after : ใช้ if elif else ควบคุมว่าบล็อกไหนจะทำงาน — ย้ำ indentation หลัง :

A conditional structure uses a boolean value (True / False) from the Booleans lesson to choose the program's path. After the : you must indent the code in that block.

โครงสร้างเงื่อนไขใช้ค่าบูลีน (True / False) จากบท Booleans เพื่อเลือกทางเดินของโปรแกรม — หลังเครื่องหมาย : ต้องย่อหน้า (indent) โค้ดในบล็อกนั้น

1 if only 1 if อย่างเดียว

if condition: runs the indented block when the condition is true; if it is false, the block is skipped.

if เงื่อนไข: ถ้าเงื่อนไขเป็นจริง จะรันบล็อกที่ย่อหน้าไว้ ถ้าเงื่อนไขเป็นเท็จ จะข้ามบล็อกนั้นไป

[1]
[output]
pass

2 if / else — two paths 2 if / else สองทาง

else: is the alternative when the if condition is false — exactly one of the two blocks runs.

else: คือทางเลือกเมื่อเงื่อนไขของ if เป็นเท็จ — จะรันอย่างใดอย่างหนึ่งเท่านั้น

[2]
[output]
cool

3 if / elif / else — several ranges 3 if / elif / else หลายช่วง

elif (else if) checks the next condition when the earlier ones were false. Python checks from top to bottom and stops at the first true condition.

elif (else if) ใช้เช็คเงื่อนไขถัดไปเมื่อเงื่อนไขก่อนหน้าเป็นเท็จ ตรวจจากบนลงล่าง — เจอเงื่อนไขจริงอันแรกแล้วจะไม่เช็คต่อ

[3]
[output]
B

4 Combined conditions with and / or 4 เงื่อนไขรวม and / or

Combine conditions with and (both must be true) or or (at least one is true), as learned in the Booleans lesson.

รวมเงื่อนไขด้วย and (ต้องจริงทั้งคู่) หรือ or (จริงอย่างน้อยหนึ่ง) ตามที่เรียนในบท Booleans

[4]
[output]
allowed

5 Nested if 5 if ซ้อน (nested)

You can put an if inside another if block — indent clearly so it is obvious which block is under which condition.

วาง if ภายในบล็อก if อื่นได้ — ย่อหน้าให้ชัดว่าบล็อกไหนอยู่ภายใต้เงื่อนไขใด

[5]
[output]
high

6 Shipping fee 6 ค่าจัดส่ง

Online shops often set a shipping fee by order value and parcel weight. The first matching branch wins: free shipping above 1000 baht, otherwise the fee depends on the weight. Only one branch runs.

ร้านค้าออนไลน์มักกำหนดค่าจัดส่งจากยอดสั่งซื้อและน้ำหนักพัสดุ เงื่อนไขแรกที่เป็นจริงจะถูกเลือก: ส่งฟรีเมื่อยอดเกิน 1000 บาท ถ้าไม่ถึงค่าจัดส่งขึ้นกับน้ำหนัก และจะรันเพียงหนึ่งบล็อก

[6]
[output]
Shipping fee = 80 baht

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

  1. = instead of ==. = assigns a value; == compares. if score = 50: is a SyntaxError.
  2. ใช้ = แทน == = คือการกำหนดค่า ส่วน == คือการเปรียบเทียบ if score = 50: จะเกิด SyntaxError
  3. Wrong elif order. Conditions are tested top to bottom and the first true one wins. With score = 95 the wide test >= 50 comes first, so the narrower >= 80 is never reached. Put the narrowest condition first.
  4. เรียง elif ผิดลำดับ เงื่อนไขถูกตรวจจากบนลงล่างและข้อแรกที่เป็นจริงจะชนะ เมื่อ score = 95 เงื่อนไขกว้าง >= 50 มาก่อน จึงไม่มีทางถึง >= 80 ต้องวางเงื่อนไขที่แคบที่สุดไว้ก่อน
  5. Missing or uneven indentation. The block after : must be indented, and every line of one block must use the same indentation, otherwise you get an IndentationError.
  6. ลืมหรือย่อหน้าไม่เท่ากัน บล็อกหลัง : ต้องย่อหน้า และทุกบรรทัดในบล็อกเดียวกันต้องย่อหน้าเท่ากัน ไม่เช่นนั้นเกิด IndentationError
[7]
[output]
pass

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

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

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

  1. A year is a leap year if it is divisible by 4 and not by 100, or if it is divisible by 400. Test 2024, 1900 and 2000. Hint: % gives the remainder.
  2. ปีที่หารด้วย 4 ลงตัวแต่หารด้วย 100 ไม่ลงตัว หรือหารด้วย 400 ลงตัว เป็นปีอธิกสุรทิน ทดสอบ 2024, 1900 และ 2000 Hint: % ให้เศษจากการหาร
  3. Convert a score to a grade A, B, C or F with an if / elif / else chain. Which order of conditions is safe?
  4. แปลงคะแนนเป็นเกรด A, B, C หรือ F ด้วย if / elif / else ลำดับเงื่อนไขแบบไหนปลอดภัย
  5. Check a username: it must not be empty and must be at most 12 characters. Hint: len() and and.
  6. ตรวจ username: ต้องไม่ว่างและยาวไม่เกิน 12 ตัวอักษร Hint: len() และ and

Key takeaways สรุปสำคัญ

Remember these points when you write your own programs:

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

  1. Conditions are checked from top to bottom and only the first true branch runs, so order them from the most specific to the most general.
  2. เงื่อนไขถูกตรวจจากบนลงล่างและรันเฉพาะบล็อกแรกที่เป็นจริง จึงควรเรียงจากเฉพาะเจาะจงที่สุดไปกว้างที่สุด
  3. Use == to compare and = to assign; combine tests with and, or and not.
  4. ใช้ == เพื่อเปรียบเทียบ และ = เพื่อกำหนดค่า และรวมเงื่อนไขด้วย and, or, not
  5. Indentation is part of the syntax: it shows which lines belong to which branch.
  6. การย่อหน้าเป็นส่วนหนึ่งของไวยากรณ์ บอกว่าบรรทัดไหนอยู่ในบล็อกไหน
  7. Always test the boundary values (for example a score of exactly 50 or 80), because that is where > and >= differ.
  8. ทดสอบค่าที่ขอบเสมอ (เช่น คะแนน 50 หรือ 80 พอดี) เพราะเป็นจุดที่ > กับ >= ให้ผลต่างกัน

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. More Control Flow Tools — if Statements — Python Tutorial — if statements (if / elif / else)

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

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

What must you do with the code block after the : of an if?หลังเครื่องหมาย : ของ if ต้องทำอย่างไรกับบล็อกโค้ด?

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

เฉลย / Answer: (A) Indent the code in the blockย่อหน้า (indent) โค้ดในบล็อก

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

  • (A) Indent the code in the blockย่อหน้า (indent) โค้ดในบล็อก
    Correct! Python uses indentation to mark the block after a :.ถูกต้อง! Python ใช้ indentation กำหนดขอบเขตบล็อกหลัง :
  • (B) Wrap every line in braces {}ใส่วงเล็บปีกกา {} ครอบทุกบรรทัด
    Wrong: Python does not use {} to mark an if block.ผิด: Python ไม่ใช้ {} เป็นหลักในการกำหนดบล็อก if
  • (C) End every line with a ;ปิดท้ายทุกบรรทัดด้วย ; เท่านั้น
    Wrong: a ; is not required and does not replace indentation.ผิด: ไม่จำเป็นต้องใส่ ; และไม่ได้แทน indentation
  • (D) Only write code on the same line as ifเขียนโค้ดในบรรทัดเดียวกับ if เท่านั้น
    Wrong: a multi-line block is fine if indented correctly.ผิด: บล็อกหลายบรรทัดได้ ถ้าย่อหน้าถูกต้อง

ข้อ 2

2 / 10

With n = 75 and if n >= 80: print("A") / elif n >= 70: print("B") / else: print("C"), what is printed?จาก n = 75 แล้ว if n >= 80: print("A") / elif n >= 70: print("B") / else: print("C") ผลลัพธ์คือข้อใด?

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

เฉลย / Answer: (B) BB

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

  • (A) AA
    Wrong: 75 is not >= 80, so the first if does not run.ผิด: 75 ไม่ถึง 80 จึงไม่เข้า if แรก
  • (B) BB
    Correct! elif n >= 70 is true, so it prints B.ถูกต้อง! elif n >= 70 เป็นจริง จึงพิมพ์ B
  • (C) CC
    Wrong: else runs only when all earlier conditions are false.ผิด: else ทำงานเมื่อเงื่อนไขก่อนหน้าเป็นเท็จทั้งหมด
  • (D) Nothing is printedไม่พิมพ์อะไร
    Wrong: a matching branch exists, so something is printed.ผิด: มีสาขาที่ตรงเงื่อนไขจึงมีการพิมพ์

ข้อ 3

3 / 10

What does elif mean?elif มีความหมายใกล้เคียงกับข้อใด?

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

เฉลย / Answer: (A) else if — check the next condition when the earlier ones were falseelse if — เช็คเงื่อนไขถัดไปเมื่อเงื่อนไขก่อนหน้าเป็นเท็จ

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

  • (A) else if — check the next condition when the earlier ones were falseelse if — เช็คเงื่อนไขถัดไปเมื่อเงื่อนไขก่อนหน้าเป็นเท็จ
    Correct! elif is 'else if' in Python.ถูกต้อง! elif คือ else if ใน Python
  • (B) End the program immediatelyจบโปรแกรมทันที
    Wrong: elif does not end the program.ผิด: elif ไม่ได้จบโปรแกรม
  • (C) Loop the same condition foreverวนซ้ำเงื่อนไขเดิมไม่รู้จบ
    Wrong: looping uses for/while, not elif.ผิด: การวนซ้ำใช้ for/while ไม่ใช่ elif
  • (D) Catch an errorจับข้อผิดพลาด
    Wrong: catching errors uses try/except.ผิด: การจับข้อผิดพลาดใช้ try/except

ข้อ 4

4 / 10

With if False: print("a") then else: print("b"), what is printed?จาก if False: print("a") แล้ว else: print("b") จะพิมพ์อะไร?

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

เฉลย / Answer: (C) bb

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

  • (A) aa
    Wrong: the condition is False, so the if block does not run.ผิด: เงื่อนไขเป็น False จึงไม่รันบล็อก if
  • (B) a and ba และ b
    Wrong: if/else runs only one branch.ผิด: if/else รันเพียงสาขาเดียว
  • (C) bb
    Correct! The condition is false, so else runs and prints b.ถูกต้อง! เงื่อนไขเท็จจึงเข้า else แล้วพิมพ์ b
  • (D) An errorเกิด Error
    Wrong: this is valid if/else syntax.ผิด: โค้ดนี้ถูกต้องตามไวยากรณ์ if/else

ข้อ 5

5 / 10

When is age >= 18 and has_id true?เงื่อนไข age >= 18 and has_id เป็นจริงเมื่อใด?

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

เฉลย / Answer: (B) When both sides are trueเมื่อทั้งสองฝั่งเป็นจริง

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

  • (A) When just one of the conditions is trueเมื่อเงื่อนไขใดเงื่อนไขหนึ่งเป็นจริงก็พอ
    Wrong: that is how or behaves, not and.ผิด: นั่นคือพฤติกรรมของ or ไม่ใช่ and
  • (B) When both sides are trueเมื่อทั้งสองฝั่งเป็นจริง
    Correct! and requires both conditions to be true.ถูกต้อง! and ต้องการให้ทั้งสองเงื่อนไขเป็นจริง
  • (C) When both sides are falseเมื่อทั้งสองฝั่งเป็นเท็จ
    Wrong: if both are false, and is false.ผิด: ถ้าทั้งคู่เท็จ ผลของ and เป็นเท็จ
  • (D) When there is a variable named andเมื่อมีตัวแปรชื่อ and ในโปรแกรม
    Wrong: and is a logical operator, not a variable.ผิด: and เป็นตัวดำเนินการทางตรรกะ ไม่ใช่ตัวแปร

ข้อ 6

6 / 10

With score = 85 and if score >= 50: print("pass"), what is printed?จาก score = 85 และ if score >= 50: print("pass") จะพิมพ์อะไร?

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

เฉลย / Answer: (C) passpass

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

  • (A) Nothingไม่พิมพ์อะไร
    Wrong: 85 >= 50 is true, so it prints.ผิด: 85 >= 50 เป็นจริง จึงพิมพ์
  • (B) 8585
    Wrong: the program prints the word pass, not the score value.ผิด: โปรแกรมพิมพ์คำว่า pass ไม่ใช่ค่า score
  • (C) passpass
    Correct! The condition is true, so the if block runs.ถูกต้อง! เงื่อนไขเป็นจริง จึงรันบล็อก if
  • (D) ErrorError
    Wrong: the code is valid.ผิด: โค้ดถูกต้อง

ข้อ 7

7 / 10

In a multi-branch if/elif, once the first true condition is found, what happens to the remaining elif branches?ใน if/elif หลายสาขา ถ้าเจอเงื่อนไขจริงอันแรกแล้ว จะเกิดอะไรกับ elif ที่เหลือ?

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

เฉลย / Answer: (D) They are all skippedถูกข้ามไปทั้งหมด

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

  • (A) They are still checked and every true one runsยังตรวจต่อและรันทุกอันที่จริง
    Wrong: if/elif runs only one branch.ผิด: if/elif รันเพียงสาขาเดียว
  • (B) They are re-checked bottom to topตรวจย้อนกลับจากล่างขึ้นบน
    Wrong: checking goes top to bottom, once.ผิด: ตรวจจากบนลงล่างครั้งเดียว
  • (C) They become else automaticallyกลายเป็น else อัตโนมัติ
    Wrong: elif and else are different keywords.ผิด: elif กับ else เป็นคนละคำสั่ง
  • (D) They are all skippedถูกข้ามไปทั้งหมด
    Correct! At the first true branch it stops and checks no further.ถูกต้อง! เจอสาขาจริงอันแรกแล้วหยุด ไม่ตรวจต่อ

ข้อ 8

8 / 10

Is an else always required in an if structure?else ในโครงสร้าง if จำเป็นต้องมีเสมอหรือไม่?

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

เฉลย / Answer: (A) Not required — add it when you want an alternative for the false caseไม่จำเป็น — ใส่เมื่อต้องการทางเลือกเมื่อเงื่อนไขเป็นเท็จ

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

  • (A) Not required — add it when you want an alternative for the false caseไม่จำเป็น — ใส่เมื่อต้องการทางเลือกเมื่อเงื่อนไขเป็นเท็จ
    Correct! else is optional.ถูกต้อง! else เป็นทางเลือก
  • (B) Always required; without it there is an errorจำเป็นเสมอ ถ้าไม่มีจะ Error
    Wrong: an if alone with no else is fine.ผิด: if เดี่ยวๆ ไม่มี else ก็ได้
  • (C) Required only when there is an elifจำเป็นเมื่อมี elif เท่านั้น
    Wrong: an elif does not force an else.ผิด: elif ไม่บังคับให้มี else
  • (D) Required only in Python 3.10+จำเป็นเฉพาะใน Python 3.10+
    Wrong: this rule does not depend on the version.ผิด: กฎนี้ไม่ขึ้นกับเวอร์ชัน

ข้อ 9

9 / 10

With x = 10 and nested if: if x > 5: containing if x > 8: print("high") else: print("mid"), what is printed?จาก x = 10 โค้ด nested if: if x > 5: ข้างในมี if x > 8: print("high") else: print("mid") จะพิมพ์อะไร?

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

เฉลย / Answer: (D) highhigh

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

  • (A) midmid
    Wrong: 10 > 8 is true, so it goes to high.ผิด: 10 > 8 เป็นจริง จึงเข้า high
  • (B) lowlow
    Wrong: 10 > 5 is true, so the outer else is not taken.ผิด: 10 > 5 เป็นจริง จึงไม่เข้า else นอก
  • (C) Prints both high and midพิมพ์ทั้ง high และ mid
    Wrong: the inner if/else runs only one branch.ผิด: if/else ด้านในรันเพียงสาขาเดียว
  • (D) highhigh
    Correct! x > 5 is true and x > 8 is true, so it prints high.ถูกต้อง! x > 5 จริง และ x > 8 จริง จึงพิมพ์ high

ข้อ 10

10 / 10

To check that score is in the range 50 to 79 inclusive and print "C", which condition is correct?ต้องการตรวจว่า score อยู่ในช่วง 50 ถึง 79 (รวมปลาย) แล้วพิมพ์ "C" เขียนเงื่อนไขใดถูกต้อง?

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

เฉลย / Answer: (B) if 50 <= score and score <= 79:if 50 <= score and score <= 79:

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

  • (A) if score >= 50 or score <= 79:if score >= 50 or score <= 79:
    Wrong: using or lets almost any value pass; use and.ผิด: ใช้ or ทำให้เกือบทุกค่าผ่าน ต้องใช้ and
  • (B) if 50 <= score and score <= 79:if 50 <= score and score <= 79:
    Correct! Both conditions must hold.ถูกต้อง! ต้องเป็นจริงทั้งสองเงื่อนไข
  • (C) if score = 50 to 79:if score = 50 to 79:
    Wrong: that is not Python syntax.ผิด: ไม่ใช่ไวยากรณ์ Python
  • (D) if score > 50 elif score < 79:if score > 50 elif score < 79:
    Wrong: elif goes on its own line, and this is invalid syntax.ผิด: elif ต้องอยู่คนละบรรทัด และเขียนแบบนี้ผิดไวยากรณ์