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 เงื่อนไข: ถ้าเงื่อนไขเป็นจริง จะรันบล็อกที่ย่อหน้าไว้
ถ้าเงื่อนไขเป็นเท็จ จะข้ามบล็อกนั้นไป
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 เป็นเท็จ — จะรันอย่างใดอย่างหนึ่งเท่านั้น
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) ใช้เช็คเงื่อนไขถัดไปเมื่อเงื่อนไขก่อนหน้าเป็นเท็จ
ตรวจจากบนลงล่าง — เจอเงื่อนไขจริงอันแรกแล้วจะไม่เช็คต่อ
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
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 อื่นได้ — ย่อหน้าให้ชัดว่าบล็อกไหนอยู่ภายใต้เงื่อนไขใด
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 บาท ถ้าไม่ถึงค่าจัดส่งขึ้นกับน้ำหนัก และจะรันเพียงหนึ่งบล็อก
Shipping fee = 80 baht
7 Common mistakes 7 ข้อผิดพลาดที่พบบ่อย
-
=instead of==.=assigns a value;==compares.if score = 50:is aSyntaxError. -
ใช้
=แทน===คือการกำหนดค่า ส่วน==คือการเปรียบเทียบif score = 50:จะเกิดSyntaxError -
Wrong
eliforder. Conditions are tested top to bottom and the first true one wins. Withscore = 95the wide test>= 50comes first, so the narrower>= 80is never reached. Put the narrowest condition first. -
เรียง
elifผิดลำดับ เงื่อนไขถูกตรวจจากบนลงล่างและข้อแรกที่เป็นจริงจะชนะ เมื่อscore = 95เงื่อนไขกว้าง>= 50มาก่อน จึงไม่มีทางถึง>= 80ต้องวางเงื่อนไขที่แคบที่สุดไว้ก่อน -
Missing or uneven indentation. The block after
:must be indented, and every line of one block must use the same indentation, otherwise you get anIndentationError. -
ลืมหรือย่อหน้าไม่เท่ากัน บล็อกหลัง
:ต้องย่อหน้า และทุกบรรทัดในบล็อกเดียวกันต้องย่อหน้าเท่ากัน ไม่เช่นนั้นเกิดIndentationError
pass
8 Think further 8 แบบฝึกคิดต่อ
Try these yourself in the Run boxes above (or in Google Colab):
ลองทำเองในกล่อง Run ด้านบน (หรือใน Google Colab):
-
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. -
ปีที่หารด้วย 4 ลงตัวแต่หารด้วย 100 ไม่ลงตัว หรือหารด้วย 400 ลงตัว เป็นปีอธิกสุรทิน ทดสอบ 2024, 1900 และ 2000 Hint:
%ให้เศษจากการหาร -
Convert a score to a grade A, B, C or F with an
if / elif / elsechain. Which order of conditions is safe? -
แปลงคะแนนเป็นเกรด A, B, C หรือ F ด้วย
if / elif / elseลำดับเงื่อนไขแบบไหนปลอดภัย -
Check a username: it must not be empty and must be at most 12 characters. Hint:
len()andand. -
ตรวจ username: ต้องไม่ว่างและยาวไม่เกิน 12 ตัวอักษร Hint:
len()และand
Key takeaways สรุปสำคัญ
Remember these points when you write your own programs:
จำประเด็นเหล่านี้ไว้เมื่อเขียนโปรแกรมของคุณเอง:
- 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.
- เงื่อนไขถูกตรวจจากบนลงล่างและรันเฉพาะบล็อกแรกที่เป็นจริง จึงควรเรียงจากเฉพาะเจาะจงที่สุดไปกว้างที่สุด
-
Use
==to compare and=to assign; combine tests withand,orandnot. -
ใช้
==เพื่อเปรียบเทียบ และ=เพื่อกำหนดค่า และรวมเงื่อนไขด้วยand,or,not - Indentation is part of the syntax: it shows which lines belong to which branch.
- การย่อหน้าเป็นส่วนหนึ่งของไวยากรณ์ บอกว่าบรรทัดไหนอยู่ในบล็อกไหน
-
Always test the boundary values (for example a score of exactly 50 or 80), because that is where
>and>=differ. -
ทดสอบค่าที่ขอบเสมอ (เช่น คะแนน 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:
แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:
-
More Control Flow Tools — if Statements —
Python Tutorial — if statements
(
if/elif/else)