:books: :octocat: Github static blog post, experience the fun of using Issues.Welcome star( 静态博客文章,体验一下使用 Issues 的乐趣,欢迎 star )个人博客地址:blog.hvkcoder.me/love
package basic
import "fmt"
func Switch(){
mark := 60
// 不加条件判断
switch mark {
case 90 :
fmt.Println("A")
case 80:
fmt.Println("B")
case 70, 60:
fmt.Println("C")
default:
fmt.Println("D")
}
// 加入条件判断
switch {
case mark >= 90:
fmt.Println("A")
case mark < 90 && mark >= 80:
fmt.Println("B")
case mark < 80 && mark >= 60:
fmt.Println("C")
default:
fmt.Println("D")
}
}
Go 语言的控制语句
判断语句
if-else
,支持初始化表达式;循环语句
for
,有 3 种形式do-while
形式;while
形式;for
形式;使用
for + if
实现选择排序;控制语句
switch
switch
支持任何类型或表达式作为条件语句,不需要写break
,条件成立自动终止;需要接着执行下一个case
,使用fallthrough
语句。goto
goto
语句可以无条件地转移到当前函数内定义的标签,通常与条件语句配合使用。但是,在结构化程序设计中一般不主张使用goto
,以免造成程序流程的混乱。