在Go語言中,條件語句主要包括有if
、 switch
與 select
。git
注意: Go語言中沒有三目運算符,不支持 ?:
形式的條件判斷。學習
最簡單的 if
語句的基本語法:測試
if 條件判斷 { // 在當前條件判斷爲true時執行 }
條件判斷若是爲真(true),那麼就執行大括號中的語句;若是爲假(false),就不執行大括號中的語句,繼續執行if
結構後面的代碼。網站
值得注意的是:Go語言規定與 if
匹配的左括號 {
必須與 if和條件判斷
放在同一行。code
package main import "fmt" func main() { var year int = 2020 if year > 1996 { // 若是條件爲 true,則執行如下語句 fmt.Printf("%d大於1996\n", year) } fmt.Println("year的值爲: ", year) }
執行結果爲:get
2020大於1996 year的值爲: 2020
if...else
語句的基本語法:博客
if 條件判斷 { // 在當前條件判斷爲true時執行 } else { // 在當前條件判斷爲false時執行 }
條件判斷若是爲真(true),那麼就執行其後緊跟的語句塊;若是爲假(false),則執行 else
後面的語句塊。it
值得注意的是:else
必須與上一個 if
右邊的大括號在同一行;與 else
匹配的左括號 {
也必須與 else
卸載同一行。class
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) }
執行結果爲:import
2020大於1996 year的值爲: 2020
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 條件判斷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 語句用於基於不一樣條件執行不一樣動做,每個 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
語句在後面會講解。
歡迎訪問個人我的網站:
李培冠博客:lpgit.com