Go Lang

การเขียนโปรแกรมภาษา Go กับเรื่องของ Decision Making

จากบทเรียนก่อนหน้านี้ของการเขียนภาษา Go หรือ Go Lang ภาษาใหม่ของ Google เราจะมาศึกษาต่อเนื่องเกี่ยวกับ Decistion Making หรือ if then else

บทเรียนก่อนหน้านี้เป็นการติดตั้งภาษา Go และขั้นตอนการ Compile ไปจนถึงการเขียนโปรแกรมเบื้องต้น สามารถอ่านได้ที่: การเขียนโปรแกรมภาษา Go

รอบนี้จะเป็นการทำงานกับ Decision Making เราจะทดสอบทีละส่วนแล้วกันนะครับ

If Then Else

package main

import "fmt"

func main() {
    if 7%2 == 0 {
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    }

    if 8%4 == 0 {
        fmt.Println("8 is divisible by 4")
    }
}

เป็นการตรวจสอบว่า 7 หาร 2 ได้ซึ่งได้เศษ 0 เช็คว่าถ้าผลลัพธ์เป็น 0 ก็เป็นเลขคี่ หรือ 8 หาร 4 ได้เศษเป็น 0 นั่นเป็นแปลว่า 8 หารด้วย 4 ลงตัวเป็นต้น

หรือการตรวจสอบเงื่อนไข เช่น

package main

import "fmt"

func main() {
   var a int = 10
   if( a < 20 ) {
       fmt.Printf("a is less than 20\n" )
   }
   fmt.Printf("value of a is : %d\n", a)
}

เป็นการตั้งตัวแปรว่า a เป็น 10  มี Data Type เป็น int ถ้า a น้อยกว่า 20 ก็ให้เข้าเงื่อนไขเป็นต้น การแสดงผลของ Printf ใกล้เคียงการใช้งาน C++ ยังไงไม่รู้

ถ้าเพิ่ม else หรือการเช็คเพิ่มเติมว่าเข้าเงื่อนไขไหมก็ตามนี้ครับ:

package main

import "fmt"

func main() {
   var a int = 100;
   if( a < 20 ) {
       fmt.Printf("a is less than 20\n" );
   } else {
       fmt.Printf("a is not less than 20\n" );
   }
   fmt.Printf("value of a is : %d\n", a);

}

ส่วนรูปแบบของ Nested If ก็สามารถเรียกใช้ได้คือ:

package main

import "fmt"

func main() {
   var a int = 100
   var b int = 200
   if( a == 100 ) {
       if( b == 200 )  {
          fmt.Printf("Value of a is 100 and b is 200\n" );
       }
   }
   fmt.Printf("Exact value of a is : %d\n", a );
   fmt.Printf("Exact value of b is : %d\n", b );
}

ผลลัพธ์:

การใช้งาน Switch Case ก็จะมีรูปแบบดังนี้:

package main

import "fmt"

func main() {
   var grade string = "B"
   var marks int = 90

   switch marks {
      case 90: grade = "A"
      case 80: grade = "B"
      case 50,60,70 : grade = "C"
      default: grade = "D"
   }

   switch {
      case grade == "A" :
         fmt.Printf("Excellent!\n" )
      case grade == "B", grade == "C" :
         fmt.Printf("Well done\n" )
      case grade == "D" :
         fmt.Printf("You passed\n" )
      case grade == "F":
         fmt.Printf("Better try again\n" )
      default:
         fmt.Printf("Invalid grade\n" );
   }
   fmt.Printf("Your grade is  %s\n", grade );
}

ก็คือโปรแกรมเกรดนั่นแหละ (ข้อสอบยอดฮิต) ส่วนรูปแบบสุดท้ายที่คล้าย Switch Case คือ Select Statement ครับใช้งานดังนี้:

package main

import "fmt"

func main() {
   var c1, c2, c3 chan int
   var i1, i2 int
   select {
      case i1 = <-c1:
         fmt.Printf("received ", i1, " from c1\n")
      case c2 <- i2:
         fmt.Printf("sent ", i2, " to c2\n")
      case i3, ok := (<-c3):  // same as: i3, ok := <-c3
         if ok {
            fmt.Printf("received ", i3, " from c3\n")
         } else {
            fmt.Printf("c3 is closed\n")
         }
      default:
         fmt.Printf("no communication\n")
   }    
}

เป็นอันจบการใช้ Decision Making ครับ

Asst. Prof. Banyapon Poolsawas

อาจารย์ประจำสาขาวิชาการออกแบบเชิงโต้ตอบ และการพัฒนาเกม วิทยาลัยครีเอทีฟดีไซน์ & เอ็นเตอร์เทนเมนต์เทคโนโลยี มหาวิทยาลัยธุรกิจบัณฑิตย์ ผู้ก่อตั้ง บริษัท Daydev Co., Ltd, (เดย์เดฟ จำกัด)

Related Articles

Back to top button
Game & Mobile Development AR VR XR
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Adblock Detected

เราตรวจพบว่าคุณใช้ Adblock บนบราวเซอร์ของคุณ,กรุณาปิดระบบ Adblock ก่อนเข้าอ่าน Content ของเรานะครับ, ถือว่าช่วยเหลือกัน