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

Quiz: บทที่ 4 - Control Flow

คำถามที่ 1

if expression ใน Rust ต้องมี condition เป็น type อะไร?

A. i32
B. bool
C. อะไรก็ได้
D. 0 หรือ 1

ดูเฉลย

B. bool

if true { }  // ✅
if 1 { }     // ❌ Error
```text

</details>

---

# คำถามที่ 2

loop ใดรันไม่รู้จบ?

A. `for`  
B. `while`  
C. `loop`  
D. ทุกแบบ

<details>
<summary>ดูเฉลย</summary>

**C. `loop`**

`loop` รันตลอดจนกว่าจะ `break`

```rust,ignore
loop {
    break; // ออกจาก loop
}

คำถามที่ 3

for i in 0..5 วนกี่รอบ?

A. 4 รอบ
B. 5 รอบ
C. 6 รอบ
D. Error

ดูเฉลย

B. 5 รอบ

0..5 = 0, 1, 2, 3, 4 (exclusive 5) 0..=5 = 0, 1, 2, 3, 4, 5 (inclusive)


คำถามที่ 4

break สามารถ return ค่าได้ไหม?

A. ได้ ใน loop เท่านั้น
B. ได้ ทุก loop
C. ไม่ได้
D. ได้ แต่ต้องเป็น return

ดูเฉลย

A. ได้ ใน loop เท่านั้น

let result = loop {
    break 42;
};
```text

</details>

---

# คำถามที่ 5

`continue` ทำอะไร?

A. หยุด loop  
B. ข้ามไป iteration ถัดไป  
C. Return ค่า  
D. ไม่มี effect

<details>
<summary>ดูเฉลย</summary>

**B. ข้ามไป iteration ถัดไป**

```rust,ignore
for i in 0..5 {
    if i == 2 { continue; }
    println!("{}", i); // 0, 1, 3, 4
}

👉 Quiz บทที่ 5