Go 101 คำสั่ง if , if-else , if-else-if , switchในภาษาโก

golang-ep8-if-else-switch

บทความนี้จะพูดถึงชุดคำสั่งที่ใช้ในการตรวจสอบเงื่อนไข if , if-else , if-else-if , switch เป็นส่วนที่สำคัญในการนำไปเขียนโปรแกรม ให้เข้าใจหลักไวยากรณ์ (Syntax) ในรูปแบบต่างๆ รวมถึงยกตัวอย่างและผลการรันโปรแกรม

1. คำสั่งตรวจสอบเงื่อนไข if

รูปแบบคำสั่ง

if condition {
   // if condition is true
}

ตัวอย่าง

package main

import "fmt"
  
func main() {
      
   // total point 
   var totalPoint int = 80
   
   if totalPoint >= 50 {
	  // if totalPoint >= 50 
      fmt.Println("Your Point is ", totalPoint)
   }

   if totalPoint >= 90 {
	  // if totalPoint >= 90 
      fmt.Println("Your Point is ", totalPoint)
   }

}
  • บรรทัดที่ 10 เป็นการตรวจสอบค่า totalPoint มากกว่าหรือเท่ากับ 50 หรือไม่ ซึ่งเราทราบว่า totalPoint มีค่าเท่ากับ 80 จากบรรทัดที่ 8 ทำให้เงื่อนไขเป็นจริง (true) ทำให้บรรทัดที่ 12 ทำงานด้วยการแสดงข้อความ “Your Point is 80” ดังภาพผลลัพธ์ด้านล่าง
  • บรรทัดที่ 15 เป็นการตรวจสอบค่า totalPoint มากกว่าหรือเท่ากับ 90 หรือไม่ ซึ่งเราทราบว่า totalPoint มีค่าเท่ากับ 80 จากบรรทัดที่ 8 ทำให้เงื่อนไขเป็นเท็จ (false) ทำให้บรรทัดที่ 17 ไม่ทำงาน จึงไม่มีการแสดงข้อความ “Your Point is 80” ดังภาพผลลัพธ์ด้านล่าง

ผลการรันโปรแกรม

2. คำสั่งตรวจสอบเงื่อนไข if-else

รูปแบบคำสั่ง

if condition {
	// if condition is true
} else {
	// if condition is false
}

ตัวอย่าง

package main
  
import "fmt"
  
func main() {
      
   // total point 
   var totalPoint int = 80
   
   if totalPoint >= 50 {
	  	// if totalPoint >= 50 
      	fmt.Println("Passed !")
   } else {
     	// else
		fmt.Println("Failed !")
   }

}
  • บรรทัดที่ 10 เป็นการตรวจสอบค่า totalPoint มากกว่าหรือเท่ากับ 50 หรือไม่ ซึ่งเราทราบว่า totalPoint มีค่าเท่ากับ 80 จากบรรทัดที่ 8 ทำให้เงื่อนไขเป็นจริง (true) ทำให้บรรทัดที่ 12 ทำงานด้วยการแสดงข้อความ “Passed” ดังภาพผลลัพธ์ด้านล่าง
  • บรรทัดที่ 15 อยู่ในเงื่อนไข else โปรแกรมจะไม่ทำงาน จึงไม่มีการแสดงข้อความ “Failed !” ออกมา หากอยากทดสอบให้ลองเปลี่ยนค่า totalPoint = 49 แล้วรันโปรแกรม ระบบจะแสดงข้อความ “Failed !” ออกมา

ผลการรันโปรแกรม

3. คำสั่งตรวจสอบเงื่อนไข if-else-if

รูปแบบคำสั่ง

if condition1 {
	// if condition1 is true
} else if condition2 {
	// if condition2 is true
}else {
	// if condition1 and condition2 is false
}

ตัวอย่าง

package main
  
import "fmt"
  
func main() {
      
   // total point 
   var totalPoint int = 80
   
	if totalPoint >= 50 && totalPoint < 60{
	  	// if totalPoint >= 50 && totalPoint < 60
      	fmt.Println("Your Grade is D")
	} else if totalPoint >= 60 && totalPoint < 70  {
	   // if totalPoint >= 60 && totalPoint < 70
		fmt.Println("Your Grade is C")
	} else if totalPoint >= 70 && totalPoint < 80  {
	   // if totalPoint >= 70 && totalPoint < 80
		fmt.Println("Your Grade is B")
	} else if totalPoint >= 80 {
	   // if totalPoint >= 80
	    fmt.Println("Your Grade is A")
	} else {
		// else
	    fmt.Println("Your Grade is F")
	}

}
  • บรรทัดที่ 10 เป็นการตรวจสอบค่า totalPoint มากกว่าหรือเท่ากับ 50 และ totalPoint น้อยกว่า 60 หรือไม่ ซึ่งเป็นเท็จ (false)
  • บรรทัดที่ 13 เป็นการตรวจสอบค่า totalPoint มากกว่าหรือเท่ากับ 60 และ totalPoint น้อยกว่า 70 หรือไม่ ซึ่งเป็นเท็จ (false)
  • บรรทัดที่ 16 เป็นการตรวจสอบค่า totalPoint มากกว่าหรือเท่ากับ 70 และ totalPoint น้อยกว่า 80 หรือไม่ ซึ่งเป็นเท็จ (false)
  • บรรทัดที่ 19 เป็นการตรวจสอบค่า totalPoint มากกว่าหรือเท่ากับ 80 หรือไม่ ซึ่งเป็นจริง (true) ทำให้ระบบแสดงข้อความ Your Grade is A ออกมาดังภาพด้านล่าง
  • บรรทัดที่ 24 อยู่ในเงื่อนไข else โปรแกรมจะไม่ทำงาน จึงไม่มีการแสดงข้อความ “Your Grade is F” ออกมา

ผลการรันโปรแกรม

5. คำสั่งตรวจสอบเงื่อนไข switch

รูปแบบคำสั่ง

switch expression {
    case value1:
        // expression = value1
    case value2:
        // expression = value2
    default:
        // default
    }

ตัวอย่าง 1

package main
  
import "fmt"
  
func main() {

   programming := "Golang"
      
   switch programming {
       case "Java":
    		fmt.Println("Java")
       case "JavaScript":
       		fmt.Println("JavaScript")
       case "Go", "Golang":
			fmt.Println("Go,Golang")
	   default:
			fmt.Println("default")
   }  
}
  • ตัวอย่างที่ 1 จะเป็นการนำเอาตัวแปร programming เปรียบเทียบกับ case ถ้าตรงกับเงื่อนไขเป็นจริงก็จะรันคำสั่งที่อยู่ใน case นั้น

ตัวอย่าง 2

package main
  
import "fmt"
  
func main() {

	programming := "Golang"
      
   switch {
       case programming == "Java":
    		fmt.Println("Java")
       case programming == "JavaScript":
       		fmt.Println("JavaScript")
       case programming == "Go", programming == "Golang":
			fmt.Println("Go,Golang")
	   default:
			fmt.Println("default")
   }  
}
  • ตัวอย่างที่ 2 จะต่างจากตัวอย่างที่ 1 คือ บรรทัดที่ 9 จะมีแค่คำสั่ง switch แต่เราจะต้องไปกำหนดการตรวจสอบเงื่อนไขที่ case เอาเองเป็นค่าจริงหรือเท็จ true or false

ตัวอย่าง 3

package main
  
import "fmt"
  
func main() {

   switch programming := "Golang";programming{
       case "Java":
    		fmt.Println("Java")
       case "JavaScript":
       		fmt.Println("JavaScript")
       case "Go","Golang":
			fmt.Println("Go,Golang")
	   default:
			fmt.Println("default")
   }  
}
  • ตัวอย่างที่ 3 คือจะมีการประกาศตัวแปรและกำหนด expression ด้วยตัวแปร programming โดยคั่นด้วยเครื่องหมาย Semicolon ( ; ) ส่วน case ให้กำหนดค่าข้อมูลเหมือนตัวอย่างที่ 1

ผลการรันโปรแกรม

สำหรับบทความนี้ทำให้เข้าใจการใช้ชุดคำสั่งตรวจสอบเงื่อนไขด้วย if , if-else , if-else-if , switch หวังว่าคงพอจะมีประโยชน์สำหรับคนที่กำลังศึกษาบ้างไม่มากก็น้อยนะครับ.