Devfavor

Back

Control Structures & Boolean Logic — กลไกที่ทำให้โปรแกรมคิดเองได้Blur image

ถ้าโปรแกรมทำได้แค่รันจากบรรทัดแรกถึงบรรทัดสุดท้าย — ไม่ว่า user จะกรอกอะไรมา ผลลัพธ์ก็เหมือนกันทุกครั้ง — โปรแกรมก็ไม่ต่างอะไรจากสูตรในกระดาษ

สิ่งที่ทำให้ซอฟต์แวร์ “คิดได้” คือ control structures — กลไกที่กำหนดว่าโปรแกรมจะทำอะไร เมื่อไหร่ และกี่ครั้ง และสิ่งที่ขับเคลื่อน control structures ทั้งหมดคือ boolean logic — ระบบที่ตอบทุกคำถามด้วยค่าสองค่า: จริง หรือ เท็จ


🧩 Control Structures มีแค่ 3 แบบ#

control structures มีแค่ 3 ประเภท ไม่ว่าโปรแกรมจะซับซ้อนแค่ไหน

1. Sequential — ทำตามลำดับ#

รูปแบบพื้นฐานที่สุด คือรันคำสั่งทีละบรรทัดจากบนลงล่าง ไม่มีการแตกกิ่ง ไม่มีการย้อนกลับ เหมาะสำหรับงานที่ทำตามขั้นตอนตายตัว เช่น คำนวณราคา ประกอบข้อความ หรือเรียกใช้ฟังก์ชันตามลำดับ

2. Selection — ตัดสินใจ#

นี่คือจุดที่โปรแกรมเริ่ม “คิดได้” จริง ๆ โปรแกรมจะเช็กเงื่อนไข (boolean expression) แล้วเลือกว่าจะรันโค้ดส่วนไหนต่อ

# ระบบ authentication อย่างง่าย
if username == stored_username and password == stored_password:
    grant_access()
else:
    show_error("ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง")
python

if/elif/else เหมาะกับเงื่อนไขที่มี logic ซับซ้อน ส่วน switch/match เหมาะกับการเทียบค่าตายตัวหลายกรณี เช่น HTTP status code

switch (statusCode) {
  case 200: showSuccess(); break;
  case 404: showNotFound(); break;
  case 500: showServerError(); break;
}
javascript

3. Iteration — วนซ้ำ#

ใช้เมื่อต้องทำงานซ้ำกับชุดข้อมูล หรือรอจนกว่าเงื่อนไขจะเป็น true

# คำนวณราคาสินค้าในตะกร้า
total = 0
for item in cart:
    total += item.price

# รอจนกว่าจะหาเจอ
found = False
while not found:
    found = search_next()
python

for เหมาะเมื่อรู้จำนวนรอบล่วงหน้า while เหมาะเมื่อหยุดตามเงื่อนไข boolean


⚙️ Boolean Operators ทำงานอย่างไร#

Operators หลักมีสามตัว และลำดับความสำคัญ (precedence) คือ NOT ก่อน → AND → OR

Operatorความหมายตัวอย่าง
AND (&&)จริงทั้งคู่ถึงจะจริงhas_ticket AND is_adult
OR (||)จริงอย่างน้อยหนึ่งข้างก็พอis_member OR has_coupon
NOT (!)กลับค่าNOT is_banned

Short-Circuit Evaluation#

ภาษาอย่าง Python, JavaScript, Java ใช้ short-circuit evaluation — ถ้าใช้ AND แล้วตัวแรกเป็น false ก็ข้ามตัวที่เหลือทันทีโดยไม่เช็กต่อ ถ้าใช้ OR แล้วตัวแรกเป็น true ก็หยุดได้เลย

// ปลอดภัย — ถ้า obj เป็น null จะหยุดและไม่เรียก getValue()
if (obj != null && obj.getValue() > 0) {
    process(obj);
}
java

🌍 ตัวอย่างในชีวิตจริง#

Weather app — ตัดสินใจแนะนำเสื้อผ้าตามอุณหภูมิ

if temp < 15:
    suggest("เสื้อกันหนาว")
elif temp > 30:
    suggest("เสื้อยืด")
else:
    suggest("เสื้อธรรมดาก็ได้")
python

Game loop — ควบคุม state ของเกม

// boolean flags ควบคุมสถานะ
let isRunning = true;
let isPaused = false;

while (isRunning) {
    if (!isPaused) {
        updateGameState();
        renderFrame();
    }
    handleInput();
}
javascript

Data validation — ตรวจสอบ input ก่อนบันทึก

is_valid = len(username) >= 3 and "@" in email and len(password) >= 8

if is_valid:
    save_user()
else:
    show_validation_errors()
python

⚠️ ข้อผิดพลาดที่เจอบ่อย#

1. ใช้ = แทน ==#

# ผิด — นี่คือ assignment ไม่ใช่ comparison
if score = 100:
    ...

# ถูก
if score == 100:
    ...
python

Bug ชนิดนี้พบได้บ่อย — Python จะแจ้ง SyntaxError ทันที ส่วน Java จะ compile error ถ้าตัวแปรเป็น int (ไม่ใช่ boolean) แต่ถ้าตัวแปรเป็น boolean เช่น if (isReady = true) Java จะ compile ผ่านและกลายเป็น logic error เงียบ ๆ เหมือน C/C++ — โปรแกรมรันผ่านโดยไม่มี error ใด ๆ แต่ผลลัพธ์ผิดพลาดและจับยากกว่ามาก

2. ลืม Operator Precedence#

# คิดว่าเป็น (a OR b) AND c
# แต่จริง ๆ ประมวลผลเป็น a OR (b AND c)
if a or b and c:
    ...

# ใส่วงเล็บให้ชัดเจน
if (a or b) and c:
    ...
python

3. Range Comparison ใน C/Java/JavaScript#

// ผิด — ใน JS ไม่ได้ผลที่ต้องการ
if (10 < age < 20) { ... }

// ถูก
if (age > 10 && age < 20) { ... }
javascript

Python เป็นข้อยกเว้น — รองรับ chained comparison แบบ 10 < age < 20 ได้โดยตรง

4. Infinite Loop#

เกิดเมื่อ exit condition ของ while ไม่มีวันเป็น false

# อันตราย — ถ้า is_running ไม่ถูกเปลี่ยนค่าในลูป
while is_running:
    do_something()
    # ลืมใส่ is_running = False หรือ break
python

ถ้ารู้จำนวนรอบล่วงหน้า ให้เลือกใช้ for แทน while จะปลอดภัยกว่า


🪜 เมื่อ Nested If-Else ลึกเกินไป#

ปัญหาที่พบบ่อยคือ “rightward drift” — โค้ดที่เยื้องลึกหลายชั้นจน logic ตามยาก

# แบบที่อ่านยาก
def process_order(order):
    if order is not None:
        if order.is_paid:
            if order.has_stock:
                if order.address is not None:
                    ship(order)
python

วิธีแก้ที่ดีที่สุดคือ Guard Clauses (Early Return) — จัดการ edge cases ก่อน แล้วให้ main logic อยู่ระดับเดียว

# แบบที่อ่านง่ายกว่า
def process_order(order):
    if order is None:
        return
    if not order.is_paid:
        return
    if not order.has_stock:
        return
    if order.address is None:
        return

    ship(order)  # main logic อยู่ระดับเดียว ไม่ต้อง nest
python

📌 สรุป#

Control structures ทั้งสามประเภท (sequential, selection, iteration) กับ boolean logic สามตัว (AND, OR, NOT) คือทุกสิ่งที่จำเป็นสำหรับโปรแกรมทุกประเภท ตั้งแต่ script เล็ก ๆ ไปจนถึงระบบขนาดใหญ่

สองสิ่งนี้ดูเรียบง่าย แต่คือรากที่ logic ทุกชิ้นในซอฟต์แวร์ยืนอยู่ — เข้าใจตรงนี้ให้แน่น แล้วโค้ดที่ซับซ้อนขึ้นจะอ่านออกเองทีละชั้น

ถ้าอยากต่อยอด ลองศึกษา recursion — control structure อีกรูปแบบที่ทรงพลังสำหรับปัญหาบางประเภท หรือ state machines วิธีจัดการ control flow ที่ซับซ้อนให้เป็นระบบกว่า if-else ซ้อนกันหลายชั้น

😎 ☕

Code, coffee, and calm — the holy trinity of a good day. ☕

Control Structures & Boolean Logic — กลไกที่ทำให้โปรแกรมคิดเองได้
Author Coffee Stack
Published at 3 มิถุนายน 2026