Devdit
 

Go การใช้ if, if - else และ if - else if

1.5K

สอนการเขียนโปรแกรมแบบมีเงื่อนไขด้วยภาษา Go (Golang) การใช้ if, if - else และ if - else if โดยมีรายละเอียดดังนี้

 

ตัวอย่างที่ 1 Go (Golang) การใช้ if คือ การเขียนโปรแกรมแบบมีเงื่อนไขเดียว

package main
import (
    "fmt"
)

func main() {
	a := 10
	if( a == 10 ) {
		fmt.Print("a = 10")
	}	
}

ผลลัพธ์

a = 10

 

ตัวอย่างที่ 2 Go (Golang) การใช้ if - else คือ การเขียนโปรแกรมแบบมี 2 เงื่อนไข

package main
import (
    "fmt"
)

func main() {
	b := 10
	if( b == 20 ) {
		fmt.Print("b = 10")
	} else {
		fmt.Print("b != 10")
	}	
}

ผลลัพธ์

b != 10

ตัวอย่างที่ 3 Go (Golang) การใช้ if - else if คือ การเขียนโปรแกรมแบบมีเงื่อนไข อย่างน้อย 3 เงื่อนไข

package main
import (
    "fmt"
)

func main() {
	c := 3
	if( c == 5 ) {
		fmt.Print("c = 5")
	} else if( c == 4 ) {
		fmt.Print("c = 4")
	} else if( c == 3 ) {
		fmt.Print("c = 3")
	} else if( c == 2 ) {
		fmt.Print("c = 2")
	} else if( c == 1 ) {
		fmt.Print("c = 1")				
	} else {
		fmt.Print("C != 1 - 5")
	}	
}

ผลลัพธ์

c = 3
เขียน 2 ปีที่แล้ว
ชอบ
ลิ้งก์
แชร์
Devdit มีช่อง YouTube แล้ว
เราสร้างวิดีโอเกี่ยวกับเทคโนโลยี ทำตามง่ายๆ