简介:在Go语言中,条件控制语句用于根据特定条件执行不同的代码块。本文将介绍Go语言中的if、if/else、if/else if/else和嵌套if等条件控制语句的用法和注意事项。
Go语言中的条件控制语句主要包括if、if/else、if/else if/else和嵌套if等。这些语句用于根据特定条件执行不同的代码块。下面我们将逐一介绍它们的语法和用法。
package mainimport "fmt"func main() {age := 18if age >= 18 {fmt.Println("已成年")} else {fmt.Println("未成年")}}
package mainimport "fmt"func main() {score := 60.5if score < 60 {fmt.Println("不合格")} else {fmt.Println("合格")}}
package mainimport "fmt"func main() {score := 75.5if score <60 {fmt.Println("不合格")} else if score >=60 && score <70 {fmt.Println("合格")} else if score >=70 && score <=80 {fmt.Println("良好")} else {fmt.Println("优秀")}}