Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

if/else - เงื่อนไข

if ใช้ตัดสินใจว่าจะทำโค้ดส่วนไหน ตามเงื่อนไข

🔀 Control Flow Visualization

+-------------------------------------------------------------------+
|                    if/else Control Flow                           |
+-------------------------------------------------------------------+
|                                                                   |
|                      +-----------+                                |
|                      | condition |                                |
|                      +-----+-----+                                |
|                            |                                      |
|             +--------------+--------------+                       |
|             |              |              |                       |
|             v              v              v                       |
|        +--------+    +----------+    +--------+                   |
|        |  true  |    | else if  |    | false  |                   |
|        | block  |    | (option) |    | block  |                   |
|        +---+----+    +----+-----+    +---+----+                   |
|            |              |              |                        |
|            +--------------+--------------+                        |
|                           |                                       |
|                           v                                       |
|                     continue...                                   |
|                                                                   |
+-------------------------------------------------------------------+
|   Tip: In Rust, no () around condition: if x > 5 (not if (x>5))   |
+-------------------------------------------------------------------+

Syntax พื้นฐาน

if condition {
    // ทำเมื่อ condition เป็น true
}

ตัวอย่าง

fn main() {
    let number = 7;

    if number > 5 {
        println!("{} is greater than 5", number);
    }
}

if-else

fn main() {
    let number = 3;

    if number > 5 {
        println!("Greater than 5");
    } else {
        println!("Less than or equal to 5");
    }
}

else if

fn main() {
    let number = 6;

    if number % 4 == 0 {
        println!("Divisible by 4");
    } else if number % 3 == 0 {
        println!("Divisible by 3");
    } else if number % 2 == 0 {
        println!("Divisible by 2");
    } else {
        println!("Not divisible by 4, 3, or 2");
    }
}

ผลลัพธ์:

Divisible by 3

หมายเหตุ: Rust จะหยุดเมื่อเจอเงื่อนไขแรกที่ true ดังนั้น 6 หาร 2 ลงตัวด้วย แต่ไม่ถูกพิมพ์


Condition ต้องเป็น bool

ใน Rust condition ต้องเป็น bool:

fn main() {
    let number = 3;

    // ❌ Error! number ไม่ใช่ bool
    // if number {
    //     println!("number is not zero");
    // }

    // ✅ ถูกต้อง
    if number != 0 {
        println!("number is not zero");
    }
}

if ใน let (Ternary-like)

เพราะ if เป็น expression สามารถใส่ใน let ได้:

fn main() {
    let condition = true;

    let number = if condition { 5 } else { 6 };

    println!("number = {}", number); // 5
}

สำคัญ: ทุก branch ต้อง return ชนิดเดียวกัน

fn main() {
    let condition = true;

    // ❌ Error! ชนิดไม่ตรงกัน
    // let number = if condition { 5 } else { "six" };

    // ✅ ต้องเป็นชนิดเดียวกัน
    let number = if condition { 5 } else { 6 };
}

ตัวอย่างจริง: ตรวจสอบอายุ

fn main() {
    let age = 25;

    let category = if age < 13 {
        "Child"
    } else if age < 20 {
        "Teenager"
    } else if age < 60 {
        "Adult"
    } else {
        "Senior"
    };

    println!("Age {} is {}", age, category);
}

ตัวอย่างจริง: ตรวจสอบเกรด

fn main() {
    let score = 85;

    let grade = if score >= 80 {
        'A'
    } else if score >= 70 {
        'B'
    } else if score >= 60 {
        'C'
    } else if score >= 50 {
        'D'
    } else {
        'F'
    };

    println!("Score {} = Grade {}", score, grade);
}

Nested if

fn main() {
    let number = 15;

    if number > 0 {
        if number % 2 == 0 {
            println!("{} is positive and even", number);
        } else {
            println!("{} is positive and odd", number);
        }
    } else {
        println!("{} is not positive", number);
    }
}

ลองทำดู! 🎯

  1. เขียนโปรแกรมตรวจสอบว่าตัวเลขเป็นบวก ลบ หรือศูนย์
  2. เขียนฟังก์ชัน max(a, b) -> i32 ที่ return ค่าที่มากกว่า
  3. เขียนโปรแกรมตรวจสอบปีอธิกสุรทิน

สรุป

แนวคิดตัวอย่าง
ifif x > 0 { ... }
if-elseif x > 0 { ... } else { ... }
else ifelse if x < 0 { ... }
if ใน letlet y = if x > 0 { 1 } else { -1 };

👉 ต่อไป: Loops