If else ภาษา Rust
บทความนี้จะพาผู้อ่านไปทำความรู้จักกับไวยากรณ์การใช้งานคำสั่งเงื่อนไข If else ภาษา Rust กันครับ
มีหลักไวยากรณ์ ดังนี้ครับ
If
ถ้า x เท่ากับ 5 ให้แสดงข้อความ "x is five!"
If else หากต้องการใช้ If else
นอกจากนี้เรายังสามารถทำแแบบนี้ได้ด้วยครับ
ขอบคุณครับ
คำสั่งเงื่อนไข If else ในภาษา Rust
คำสั่งเงื่อนไข คือ คำสั่งที่มีการกำหนดคำสั่งให้ทำตามหากตรงกับเงื่อนไขที่กำหนด โดยจะมีค่าจริงและเท็จ ในการเปรียบเทียบเงื่อนไขมีหลักไวยากรณ์ ดังนี้ครับ
if คำสั่งตรวจสอบเงื่อนไข {ตัวอย่างการใช้งาน if else
ถ้าตรงตามเงื่อนไขให้ทำงานตามคำสั่งนี้
} else {
ถ้าไม่ตรงกับเงื่อนไขใด ให้ทำตามคำสั่งนี้
};
If
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 5; | |
if x == 5 { | |
println!("x is five!"); | |
} |
ถ้า x เท่ากับ 5 ให้แสดงข้อความ "x is five!"
If else หากต้องการใช้ If else
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 5; | |
if x == 5 { | |
println!("x is five!"); | |
} else { | |
println!("x is not five :("); | |
} |
นอกจากนี้เรายังสามารถทำแแบบนี้ได้ด้วยครับ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let x = 5; | |
let y = if x == 5 { | |
10 | |
} else { | |
15 | |
}; // y: i32 |
ความคิดเห็น
แสดงความคิดเห็น