Java - Decision Making

คำสั่งตัดสินใจ เป็นประโยคคำสั่งที่ใช้ควบคุมให้โปรแกรมเลือกดำเนินไปในเส้นทางใดเส้นทางหนึ่ง โดยขึ้นอยู่กับผลการตรวจสอบเงื่อนไข

คำสั่งตัดสินใจในภาษา Java ได้แก่

„- คำสั่ง if

„- คำสั่ง switch

คำสั่ง if..

Syntax

if(Boolean_expression)
{
    //Statements will execute if the Boolean expression is true
}

การทำงาน : เริ่มต้นจะทำการตรวจสอบ condition ในวงเล็บ ผลการตรวจสอบจะได้ true หรือ false ถ้าเป็น true จะทำ statement แต่ถ้าเป็น false จะไปทำคำสั่งอื่นๆถัดไป

ตัวอย่าง

public class Test {
   public static void main(String args[]){
      int x = 10
      if( x < 20)
      {
          System.out.print("This is if statement");
      }
   }
}

ผลการรัน

This is if statement

คำสั่ง if..else

Syntax

if(Boolean_expression){
    //Executes when the Boolean expression is true
}else{
    //Executes when the Boolean expression is false
}

ตัวอย่าง

public class Test {
   public static void main(String args[]){
      int x = 30;
      if( x < 20)
      {
          System.out.print("This is if statement");
      }else{
          System.out.print("This is else statement");
      }
  }
}

ผลการรัน

This is else statement

คำสั่ง if.. else if.. else

if..else if..else เป็นคำสั่งในภาษา Java โดย if...else if...else ใช้สร้างเงื่อนไข โดยสามารถ else if กำหนดและสร้างทางเลือกได้หลายทาง และหลาย block โดยในแต่ล่ะ block สามารถสร้างเงื่อนไขของตัวเองได้

Syntax

if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
   //Executes when the Boolean expression 3 is true
}else {
   //Executes when the none of the above condition is true.
}

ตัวอย่าง

public class Test {
   public static void main(String args[]){
      int x = 30;
      if( x == 10)
      {
          System.out.print("Value of X is 10");
      }else if( x == 20){
          System.out.print("Value of X is 20");
      }else if( x == 30){
          System.out.print("Value of X is 30");
      }else{
          System.out.print("This is else statement");
      }
  }
}

ผลการรัน

Value of X is 30

คำสั่ง Nested if...else

Nested if...else เป็นคำสั่งในภาษา Java โดย Nested if...else ใช้สร้างเงื่อนไขซ้อนเงื่อนไข โดยเงื่อนไขทั้งสอง จะต้องเป็นจริง ถึงจะทำงานใน Statement ที่ต้องการ

Syntax

if(Boolean_expression 1){
   //Executes when the Boolean expression 1 is true
if(Boolean_expression 2){
   //Executes when the Boolean expression 2 is true
   }
}

ตัวอย่าง

public class Test {
    public static void main(String args[]){
      int x = 30;
      int y = 10;
      if( x == 30){
         if( y == 10){
            System.out.print("X = 30 and Y = 10");
         }
      }
    }
}

ผลการรัน

X = 30 and Y = 10

คำสั่ง switch

Switch Case คือ คำสั่งสำหรับการสร้างเงื่อนไขแบบทำหลายทิศทาง ซึ่งจะไม่เหมือนกับ If - Then - Else Statement เพราะ Switch Statement นั้นจะสามารถทำได้หลายเงื่อนไขที่เป็นจริง ซึ่งสามารถทำงานกับชนิดข้อมูลต่าง ๆ เช่น byte, short, char, และ int primitive data types

ภาพรวมของ Switch Statement

1. ประกอบไปด้วย 3 คำสั่งหลัก ๆ คือ switch, case และ break

2. switch ใช้สำหรับระบุค่าที่ต้องการสร้างเงื่อนไข

3. case ใช้สำหรับระบุขั้นตอนการดำเนินงานในแต่ละทิศทาง

4. break ใช้สำหรับจบขั้นตอนการดำเนินงานในเหตุการณ์นั้น ๆ

5. ใช้คำสั่ง default เพื่อระบุถึงขั้นตอนการดำเนินงานที่ไม่พบทิศทางใด ๆ

ตัวอย่าง

public class Test {
    public static void main(String args[]){
      //char grade = args[0].charAt(0);
       char grade = 'C';
       switch(grade)
       {
         case 'A' :
            System.out.println("Excellent!");
            break;
         case 'B' :
         case 'C' :
            System.out.println("Well done");
            break;
         case 'D' :
            System.out.println("You passed");
         case 'F' :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
         }
         System.out.println("Your grade is " + grade);
     }
}

ผลการรัน

Well done
Your grade is a C

จะเห็นว่าเงือนไขในการตัดสินใจไม่ยากเลยใช่ไหมครับ ศึกษาให้แม่นเพราะ เราจะอยู่กับมันไปตลอดในการเขียนโปรแกรมกันเลย
กระทู้ล่าสุดจากเว็บบอร์ด
หัวข้อกระทู้
ตอบ
เปิดดู
ล่าสุด
SQL JOIN: การรวมข้อมูลจากหลายตารางในฐานข้อมูล
โดย witsarutt000 พฤ 14 มี.ค. 2024 4:07 pm บอร์ด SQL Knowledge
1
166
พฤ 14 มี.ค. 2024 5:44 pm โดย Sirayu View Topic SQL JOIN: การรวมข้อมูลจากหลายตารางในฐานข้อมูล
PHP การเปลี่ยนแปลงที่สร้างปรากฏการณ์ในโลกของเว็บ
โดย witsarutt000 พฤ 14 มี.ค. 2024 11:17 am บอร์ด PHP Knowledge
0
125
พฤ 14 มี.ค. 2024 11:17 am โดย witsarutt000 View Topic PHP การเปลี่ยนแปลงที่สร้างปรากฏการณ์ในโลกของเว็บ
ปัญหา Harddisk ขึ้น 100% เวลาเซฟไฟล์ หรือภาพ จะค้่างที่หน้าแท๊บ Expolorer
โดย Thanavat_n พ 13 มี.ค. 2024 11:02 am บอร์ด ถาม - ตอบ คอมพิวเตอร์
5
270
พ 13 มี.ค. 2024 1:34 pm โดย Thanavat_n View Topic ปัญหา Harddisk ขึ้น 100% เวลาเซฟไฟล์ หรือภาพ จะค้่างที่หน้าแท๊บ Expolorer
ตู้รองเท้า ไอเท็มวิเศษช่วยจัดระเบียบคอลเลกชันรองเท้าคู่โปรด
โดย @Foretoday อ 12 มี.ค. 2024 1:46 pm บอร์ด พูดคุยเรื่องทั่วไป จับฉ่าย
0
184
อ 12 มี.ค. 2024 1:46 pm โดย @Foretoday View Topic ตู้รองเท้า ไอเท็มวิเศษช่วยจัดระเบียบคอลเลกชันรองเท้าคู่โปรด
แนะนำสถานที่น่าเที่ยวในจังหวัดชุมพรพร้อมวิธีการเดินทาง
โดย witsarutt000 จ 11 มี.ค. 2024 6:14 pm บอร์ด พูดคุยเรื่องทั่วไป จับฉ่าย
0
142
จ 11 มี.ค. 2024 6:14 pm โดย witsarutt000 View Topic แนะนำสถานที่น่าเที่ยวในจังหวัดชุมพรพร้อมวิธีการเดินทาง
ย้าย VM ข้าม Host ด้วย scp กรณีศึกษา Vmware ESXI
โดย mindphp อ 10 มี.ค. 2024 4:36 am บอร์ด Linux - Web Server
0
239
อ 10 มี.ค. 2024 4:36 am โดย mindphp View Topic ย้าย VM ข้าม Host ด้วย scp กรณีศึกษา Vmware ESXI
IP และ vpn (VMware)
โดย ballmykids อ 10 มี.ค. 2024 2:35 am บอร์ด ถาม - ตอบ คอมพิวเตอร์
2
203
จ 11 มี.ค. 2024 3:19 pm โดย ballmykids View Topic IP และ vpn (VMware)
แบบนี้ต้องทำยังไง ในกรณีที่ Server เดิมเราได้ทำการ Raid 1 กับ HDD 2 ลูกแรกแล้ว
โดย Anonymous ศ 08 มี.ค. 2024 7:02 am บอร์ด ถาม - ตอบ คอมพิวเตอร์
1
166
ศ 08 มี.ค. 2024 8:12 pm โดย mindphp View Topic แบบนี้ต้องทำยังไง ในกรณีที่ Server เดิมเราได้ทำการ Raid 1 กับ HDD 2 ลูกแรกแล้ว