Decision-making statement

จตุรพร วงศ์ศรีจันทร์ 650710532

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

คำสั่งเงื่อนไขในภาษา C#

IF Statement

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

Syntax

if(condition)
     {  
         //code to be executed  
     }  

ตัวอย่างโค้ด

using System;
namespace Demo {
  public class Example {
     public static void Main(string[] args) {
       int a=5;
       if(a==5)
Console.WriteLine("Value of a is 5");
     }
  }
}

จากโค้ดเป็นการตรวจสอบ a เท่ากับ 5 หรือเปล่า โดยใช้คำสั่ง if ถ้าเงื่อนไขเป็นจริง จะพิมพ์ข้อความ "Value of a is 5" ออกมาทางหน้าจอ

IF – else Statement

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

Syntax

 if(condition)
    {  
      // code if condition is true  
    }
    else
    {  
      // code if condition is false  
    }  

ตัวอย่างโค้ด

using System;
namespace Demo {
public class Example {
 public static void Main(string[] args) {
  int a = 10;
  if (a == 5)
   Console.WriteLine("Value of a is 5");
  else
   Console.WriteLine("Value of a is not 5");
 }
}
}

จากโค้ด กำหนดค่าตัวแปร a เท่ากับ 10 เมื่อใช้คำสั่ง if เพื่อตรวจสอบว่า a เท่ากับ 5 หรือไม่ ถ้า a เท่ากับ 5 จะพิมพ์ข้อความ "Value of a is 5" ถ้าไม่ให้ พิมพ์ ข้อความ "Value of a is not 5" ซึ่งตัวแปร a ในที่นี้เท่ากับ 10 จึงพิมพ์ "Value of a is not 5" ออกมา

If – else – if ladder Statement

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

Syntax

if(condition1)
        {  
            // code to be executed if condition1 is true  
        }
        else if(condition2)
        {  
            // code to be executed if condition2 is true  
        }  
        else if(condition3)
        {  
            // code to be executed if condition3 is true  
        }  
        ... 
        else
        {
            // code to be executed if all the conditions are false  
        }  

ตัวอย่างโค้ด

using System;
namespace Constants {
public class Example {
 public static void Main(string[] args) {
  int a = 50;
  if (a == 5)
   Console.WriteLine("Value of a is 5");
  else if (a == 10)
   Console.WriteLine("Value of a is 10");
  else
   Console.WriteLine("Value of a is not 5 or 10");
 }
}
}

จากโค้ด กำหนดค่าตัวแปร a เท่ากับ 50 เมื่อใช้คำสั่ง if เพื่อตรวจสอบว่า a เท่ากับ 5 หรือไม่ ถ้า a เท่ากับ 5 จะพิมพ์ข้อความ "Value of a is 5" ถ้าไม่ถ้า a เท่ากับ 10 ให้ พิมพ์ ข้อความ "Value of a is 10" ถ้าไม่ ให้พิมพ์ข้อความ "Value of a is not 5 or 10" ซึ่งตัวแปร a ในที่นี้เท่ากับ 50 จึงพิมพ์ข้อความ "Value of a is not 5 or 10" ออกมา

Nested – If Statement

คำสั่ง if ที่อยู่ภายในคำสั่ง if อีกทีหนึ่ง เรียกว่า nested if โดยเป้าหมายมีเงื่อนไขอยู่ภายใต้อีกเงื่อนไขหนึ่ง หรือก็คือ หนึ่งในเงื่อนไขนั้นเป็นเงื่อนไขย่อยของเงื่อนไขหลัก

Syntax

if (condition1) 
        {
             // code to be executed 
             // if condition2 is true 
             if (condition2) 
             {
                 // code to be executed 
                 // if condition2 is true 
             }
        }

ตัวอย่างโค้ด

using System;
 
class GFG {
 
    public static void Main(String[] args)
    {
        int i = 10;
 
        if (i == 10) {
 
            // Nested - if statement
            // Will only be executed if statement
            // above it is true
            if (i < 12)
                Console.WriteLine("i is smaller than 12 too");
            else
                Console.WriteLine("i is greater than 15");
        }
    }
}

จากโค้ด กำหนดค่าตัวแปร i เท่ากับ 10เมื่อใช้คำสั่ง if เพื่อตรวจสอบว่า i เท่ากับ 10 หรือไม่ ถ้าใช่ ก็จะมีคำสั่ง if ตรวจสอบว่า i น้อยกว่า 12 หรือไม่ ถ้าใช่ จะพิมพ์ข้อความ "i is smaller than 12 too" ถ้าไม่ ให้พิมพ์ข้อความ "i is greater than 15" ซึ่งตัวแปร i ในที่นี้เท่ากับ 10 จึงผ่านเงื่อนไขแรก i เท่ากับ 10 หรือไม่ และก็จะผ่านเงื่อนไข i น้อยกว่า 12 หรือไม่ ซึ่งก็คือใช่ ก็จะพิมพ์ข้อความ "i is smaller than 12 too" ออกมา

Switch Statement

คำสั่ง switch เป็นการใช้ทางเลือกแทนการใช้ if-else-if ที่ยาว โดยคำสั่งจะตรวจสอบหา case ที่ตรงกันเพื่อดำเนินการ และใช้คำสั่ง break เพื่อออกจาก switch เมื่อพบ case ที่ตรง โดยคำสั่งจะดำเนินไปเรื่อยจนกว่าจะพบ case ที่ตรงแล้วbreakหรือจุดสิ้นสุดของ switch ซึ่งถ้าไม่ตรงกับ caseไหนเลยก็จะดำเนินการกรณี default

Syntax

switch (expression)
 {
case value1: // statement sequence
             break;
case value2: // statement sequence
             break;
.
.
.
case valueN: // statement sequence
             break;
default: // default statement sequence
}

ตัวอย่างโค้ด

using System;
 
public class GFG 
{
    public static void Main(String[] args)
    {
        int number = 30;
        switch(number)
        {
        case 10: Console.WriteLine("case 10");
                 break;
        case 20: Console.WriteLine("case 20");
                 break;
        case 30: Console.WriteLine("case 30");
                 break;
        default: Console.WriteLine("None matches"); 
                 break;
        }
    }
}

จากโค้ด กำหนดค่าตัวแปร number เท่ากับ 30 เมื่อใช้คำสั่ง switch เพื่อตรวจสอบว่า number ตรงกับเคสใด กรณี number เท่ากับ 10 ก็จะพิมพ์ข้อความ "case 10" ออกมาแล้วbreak กรณี number เท่ากับ 20 ก็จะพิมพ์ข้อความ "case 20" ออกมาแล้วbreak กรณี number เท่ากับ 30 ก็จะพิมพ์ข้อความ "case 30" ออกมาแล้วbreak แต่ถ้าไม่ตรงกับกรณีไหนเลยก็จะเข้ากรณี default ซึ่งก็จะพิมพ์ข้อความ "None matches" ออกมาแล้วbreak

Nested switch

ใน C# จะอนุญาตให้ใช้คำสั่ง switch แบบซ้อนกันได้ โดยคำสั่ง switch หนึ่งจะอยู่ภายใน switch อีกคำสั่งหนึ่งซึ่ง switch ด้านในจะอยู่ในcaseของ switch หลัก

ตัวอย่างโค้ด

using System;
 
public class GFG 
{
    public static void Main(String[] args)
    {
        int j = 5;
 
        switch (j)
        {
            case 5: Console.WriteLine(5);
                    switch (j - 1)
                   {
                    case 4: Console.WriteLine(4);
                            switch (j - 2)
                          {
                            case 3: Console.WriteLine(3);
                                    break;
                         }
                    break;
                }
                break;
            case 10: Console.WriteLine(10);
                     break;
            case 15: Console.WriteLine(15);
                     break;
            default: Console.WriteLine(100);
                     break;
        }
 
    }
}

จากโค้ด กำหนดค่าตัวแปร j เท่ากับ 5 เมื่อใช้คำสั่ง switch เพื่อตรวจสอบว่า j ตรงกับเคสใด กรณี j เท่ากับ 5 ก็จะพิมพ์ข้อความ "5" ออกมา ในswitchซ้อนswitchก็จะตรวจสอบว่า j-1 ตรงกับเคสใด กรณี j-1 เท่ากับ 4 ก็จะพิมพ์ข้อความ "4" ออกมา ในswitchซ้อนswitchอีกทีก็จะตรวจสอบว่า j-2 ตรงกับเคสใด กรณี j-2 เท่ากับ 3 ก็จะพิมพ์ข้อความ "3" ออกมา แล้วจึงbreak ตามเคส กรณี j เท่ากับ 10 ก็จะพิมพ์ข้อความ "10" ออกมาแล้วbreak กรณี j เท่ากับ 15 ก็จะพิมพ์ข้อความ "15" ออกมาแล้วbreak แต่ถ้าไม่ตรงกับกรณีไหนเลยก็จะเข้ากรณี default ซึ่งก็จะพิมพ์ข้อความ "100" ออกมาแล้วbreak

การเปรียบเทียบกับภาษาอื่น

การใช้ if, if-else, if-else-if ladder, nested if, switch, nested switch ในหลายภาษาจะมีลักษณะการทำงานคล้ายกัน ต่างกันที่ syntax โดยรวมการใช้คำสั่งมีความคล้ายคลึงกัน

IF Statement

ตัวอย่างโค้ดเปรียบเทียบจากตัวอย่างในC#

#include <stdio.h>

int main() {
    int a = 5;
    if (a == 5)
        printf("Value of a is 5\n");
}

IF – else Statement

ตัวอย่างโค้ดเปรียบเทียบจากตัวอย่างในC#

#include <stdio.h>

int main() {
    int a = 10;
    if (a == 5){
        printf("Value of a is 5\n");
    }
    else{
        printf("Value of a is not 5\n");
    }
}

If – else – if ladder Statement

ตัวอย่างโค้ดเปรียบเทียบจากตัวอย่างในC#

#include <stdio.h>

int main() {
    int a = 50;
    if (a == 5){
        printf("Value of a is 5\n");
    }
    else if (a == 10){
        printf("Value of a is 10\n");
    }
    else{
        printf("Value of a is not 5 or 10\n");
    }
}

Nested – If Statement

ตัวอย่างโค้ดเปรียบเทียบจากตัวอย่างในC#

#include <stdio.h>
int main() {
    int i = 10;

    if (i == 10) {
        if (i < 12){
            printf("i is smaller than 12 too\n");
        }
        else{
            printf("i is greater than 15\n");
        }
    }
   
}

Switch Statement

ใน Java และ C มีโครงสร้างการใช้ที่คล้ายคลึงกัน แต่ Python ไม่มีคำสั่ง switch จึงใช้ if-elif-else แทน ซึ่งทำให้โค้ดมีความเรียบง่ายและกระชับมากขึ้น

ตัวอย่างโค้ดเปรียบเทียบจากตัวอย่างในC#

#include <stdio.h>

int main() {
    int number = 30;
    switch(number) {
        case 10:
            printf("case 10\n");
            break;
        case 20:
            printf("case 20\n");
            break;
        case 30:
            printf("case 30\n");
            break;
        default:
            printf("None matches\n");
            break;
    }
}

Nested switch

ใน Java และ C มีโครงสร้างการใช้ที่คล้ายคลึงกัน แต่ Python ไม่มีคำสั่ง switch จึงใช้ if-elif-else แทน จึงนำมาให้ดูความแตกต่างจากตัวอย่าง

ตัวอย่างโค้ดเปรียบเทียบจากตัวอย่างในC#

#include <stdio.h>

int main() {
    int j = 5;

    switch (j) {
        case 5:
            printf("%d\n", 5);
            switch (j - 1) {
                case 4:
                    printf("%d\n", 4);
                    switch (j - 2) {
                        case 3:
                            printf("%d\n", 3);
                            break;
                    }
                    break;
            }
            break;
        case 10:
            printf("%d\n", 10);
            break;
        case 15:
            printf("%d\n", 15);
            break;
        default:
            printf("%d\n", 100);
            break;
    }
}

Video:

https://youtu.be/LDzirtORdS8?si=bTuamd0aEatJud72

สไลด์:

https://drive.google.com/file/d/1nz1bC3EVi4p6jTxfQiSLZNmL-WMA8lYx/view?usp=sharing

Reference :

C#

ความหมาย Decision-making statement.https://www.knowledgehut.com/tutorials/csharp/csharp-decision-making-statements

การใช้ If statemant.https://www.geeksforgeeks.org/c-sharp-decision-making-else-else-ladder-nested-switch-nested-switch/

ตัวอย่างการใช้ If statemant.https://www.knowledgehut.com/tutorials/csharp/csharp-decision-making-statements

การใช้ If-else statemant.https://www.geeksforgeeks.org/c-sharp-decision-making-else-else-ladder-nested-switch-nested-switch/

ตัวอย่างการใช้ If -else statemant.https://www.knowledgehut.com/tutorials/csharp/csharp-decision-making-statements

การใช้ If – else – if ladder Statement.https://www.geeksforgeeks.org/c-sharp-decision-making-else-else-ladder-nested-switch-nested-switch/

ตัวอย่างการใช้ If – else – if ladder Statement.https://www.knowledgehut.com/tutorials/csharp/csharp-decision-making-statements

การใช้และตัวอย่าง Nested – If Statement.https://www.geeksforgeeks.org/c-sharp-decision-making-else-else-ladder-nested-switch-nested-switch/

การใช้ Switch Statement.https://www.geeksforgeeks.org/c-sharp-decision-making-else-else-ladder-nested-switch-nested-switch/

ตัวอย่างการใช้ Switch Statement.https://www.knowledgehut.com/tutorials/csharp/csharp-decision-making-statements

การใช้และตัวอย่าง Nested switch.https://www.geeksforgeeks.org/c-sharp-decision-making-else-else-ladder-nested-switch-nested-switch/

C

Syntax.https://www.w3schools.com/c/c_conditions.php

Java

Syntax.https://www.w3schools.com/java/java_conditions.asp

Python

Syntax.https://www.w3schools.com/python/python_conditions.asp

ความแตกต่างการใช้โดยไม่มี switch.https://www.freecodecamp.org/news/python-switch-statement-switch-case-example/

Last updated