Decision making statements ต้องการให้โปรแกรมเมอร์ระบุเงื่อนไขหนึ่งหรือมากกว่า เพื่อให้โปรแกรมประเมิน ทำคำสั่งหนึ่งหรือหลายคำสั่ง หากเงื่อนไขเป็น true และอาจมีคำสั่งสำหรับกรณี false
ประเภทของ Decision Making Statements
| No. | Statement & Description |
|---|---|
| 1 | if statement - นิพจน์ boolean ตามด้วยคำสั่งอย่างน้อยหนึ่งคำสั่ง |
| 2 | if...else statement - if ตามด้วย else สำหรับกรณี false |
| 3 | nested if statements - if/else if ซ้อนใน if/else if อื่น |
| 4 | switch statement - ทดสอบตัวแปรเท่ากับรายการค่า |
| 5 | nested switch statements - switch ซ้อนใน switch อื่น |
ตัวอย่าง if-else
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int a = 50;
if (a < 10) {
Console.WriteLine("a is less than 10");
} else {
Console.WriteLine("a is not less than 10");
}
Console.WriteLine("a = {0}", a);
Console.ReadLine();
}
}
}
ผลลัพธ์
a is not less than 10 a = 50