在Go語言中,條件語句主要包括有if
、 switch
與 select
。
注意: Go語言中沒有三目運算符,不支持 ?:
形式的條件判斷。
if 語句
最簡單的if語句
最簡單的 if
語句的基本語法:
if 條件判斷 {
// 在當前條件判斷為true時執行
}
條件判斷如果為真(true),那么就執行大括號中的語句;如果為假(false),就不執行大括號中的語句,繼續執行if
結構后面的代碼。
值得注意的是:Go語言規定與 if
匹配的左括號 {
必須與 if和條件判斷
放在同一行。
示例
package main
import "fmt"
func main() {
var year int = 2020
if year > 1996 {
// 如果條件為 true,則執行以下語句
fmt.Printf("%d大於1996\n", year)
}
fmt.Println("year的值為: ", year)
}
執行結果為:
2020大於1996
year的值為: 2020
if...else語句
if...else
語句的基本語法:
if 條件判斷 {
// 在當前條件判斷為true時執行
} else {
// 在當前條件判斷為false時執行
}
條件判斷如果為真(true),那么就執行其后緊跟的語句塊;如果為假(false),則執行 else
后面的語句塊。
值得注意的是:else
必須與上一個 if
右邊的大括號在同一行;與 else
匹配的左括號 {
也必須與 else
卸載同一行。
示例
package main
import "fmt"
func main(){
year := 2020
if year > 1996 {
// 如果條件為 true,則執行以下語句
fmt.Printf("%d大於1996\n", year)
} else {
// 如果條件為 false,則執行以下語句
fmt.Printf("%d小於1996\n", year)
}
fmt.Println("year的值為: ", year)
}
執行結果為:
2020大於1996
year的值為: 2020
if...else if ...else語句
if...else if ...else
語句的基本語法:
if 條件判斷1 {
// 如果條件判斷1為 true,則執行這里的語句
} else if 條件判斷2 {
// 如果條件判斷2為 true,則執行這里的語句
} else {
// 如果以上條件判斷都為 false,則執行這里的語句
}
同樣的:else if
必須與上一個 if
或者 else if
右邊的大括號在同一行。
示例
package main
import "fmt"
func main(){
year := 2020
if year > 2050 {
fmt.Printf("%d大於2050\n", year)
} else if year > 2000 {
fmt.Printf("%d大於2000\n", year)
} else {
fmt.Println("year的值為: ", year)
}
}
執行結果為:
2020大於2000
if嵌套語句
可以在以上語句中嵌套多個同樣的語句,均是合法的。
在 if語句
中嵌套 if語句
的基本語法如下:
if 條件判斷1 {
// 在條件判斷1為 true 時,執行這里的語句
if 條件判斷2 {
// 在條件判斷2為 true 時,執行這里的語句
}
}
示例
package main
import "fmt"
func main(){
year := 2020
if year > 2000 {
if year > 2010 {
fmt.Println("year 大於2010.")
}
}
}
執行結果為:
year 大於2010.
switch語句
switch 語句用於基於不同條件執行不同動作,每一個 case 分支都是唯一的,從上至下逐一測試,直到匹配為止。
注意:雖然說 case
表達式不能重復,但是如果 case
為布爾值,則可以重復。
package main
import "fmt"
func main() {
a := false
switch false {
case a:
fmt.Println("123")
case a:
fmt.Println("456")
}
}
執行結果:
123
下面來看一下一般的例子:
package main
import "fmt"
func main(){
date := 3
switch date {
case 1:
fmt.Println("周一")
case 2:
fmt.Println("周二")
case 3:
fmt.Println("周三")
case 4:
fmt.Println("周四")
case 5:
fmt.Println("周五")
case 6:
fmt.Println("周六")
case 7:
fmt.Println("周日")
default:
fmt.Println("無效的輸入")
}
}
執行的結果:
周三
Go語言規定每個 switch
只能有一個 default
分支。
一個分支可以有多個值,多個 case
值中間使用英文逗號分隔。
package main
import "fmt"
func main(){
num := 5
switch num {
case 1, 3, 5, 7, 9:
fmt.Println("num是奇數")
case 2, 4, 6, 8, 10:
fmt.Println("num是偶數")
default:
fmt.Println("num:", num)
}
}
執行的結果:
num是奇數
當 case
分支后面使用的是表達式時,switch
語句后面不需要在跟判斷變量。
package main
import "fmt"
func main(){
score := 61
switch {
case score > 80:
fmt.Println("考得不錯")
case score >= 60:
fmt.Println("努力學習吧")
default:
fmt.Println("還不學習?")
}
}
執行結果:
努力學習吧
fallthrough
會強制執行后面的一條case語句。
package main
import "fmt"
func main(){
num := 1
switch num {
case 1:
fmt.Println(1)
fallthrough
case 2:
fmt.Println(2)
case 3:
fmt.Println(3)
default:
fmt.Println("...")
}
}
執行結果:
1
2
我們使用 fallthrough
來執行多個 case
,也可以使用 break
來終止。
package main
import "fmt"
func main(){
num := 1
switch num {
case 1:
fmt.Println(1)
if num == 1 {
break
}
fallthrough
case 2:
fmt.Println(2)
case 3:
fmt.Println(3)
default:
fmt.Println("...")
}
}
執行結果:
1
select語句
select
語句在后面會講解。
李培冠博客
歡迎訪問我的個人網站:
李培冠博客:lpgit.com