Golang系列二:控制结构

控制结构

if

  • 特点:
(1) if后面的条件判断子句不需要用小括号括起来;
(2) {必须放在行尾,和if或if else放在一行;
(3) if后面可以带一个简单的初始化语句,并以分号分割,该简单语句声明的变量的作用域是整个if语句块,包括后面的else if和else分支;
(4) if分支语句遇到return后直接返回。
if x:=5; x<2{
    fmt.Println(1)
}else if x>3{
    fmt.Println(2)
} else {
    fmt.Println(3)
}

switch

switch语句会根据传入的参数检测并执行符合条件的分支。

  • 特点:
(1) switch条件表达式的值不像C语言那样必须限制为整数,可以是任意支持相等比较运算的类型变量。
(2) switch支持default语句,当所有的case分支都不符合时,执行default语句,并且default语句可以放到任意位置,并不影响switch的判断逻辑。
score:=80
grade:=' '
if score >= 90{
    grade = 'A'
} else if score>=80 {
    grade = 'B'
} else if score>=70 {
    grade = 'C'
} else if score>=60 {
    grade = 'D'
} else {
    grade = 'F'
}

fmt.Printf("grade is %c\n", grade)

// 上面的if else可以改写为下面的switch语句
switch {
case score>=90:
    grade='A'
case score>=80:
    grade = 'B'
case score>=70:
    grade = 'C'
case score>=60:
    grade = 'D'
default:
    grade = 'F'
}

fmt.Printf("grade is %c\n", grade)

// switch
switch score {
case 60:
    grade = 'C'
case 80:
    grade = 'B'
case 100:
    grade = 'A'
default:
    grade = 'F'
}


fmt.Printf("grade is %c\n", grade) // grade is B

for语句

(1) 类似C语言的for
for init; conditions; post { }
(2) 类似C语言的while
for conditions { }
(3) 类似C语言的while(1)死循环语句
for { }
(4) for还有一种用法,对数组、切片、字符串、map和通道访问:
// 访问map
for key, value := range map { }
for key := range map { }

// 访问数组
for index, value := range array { }
for index := range array { }
for _, value := range array { }

// 访问切片
for index, value := range slice { }
for index := range slice { }
for _, value := range slice { }

// 访问通道
for value := range channel { }

标签和跳转

标签

Golang使用标签(Label)来标识一个语句的位置。用于goto,break,continue语句的跳转。

Label: Statement

goto

goto语句用于函数内部的跳转,需要配合标签一起使用。

goto Label
  • 特点:
    (1) 只能在函数内跳转。
    (2) 不能跳过内部变量声明语句。
// 代码段执行结果:situation 1
v := 1
if v==1{
    goto L1
} else {
    fmt.Println("situation 2")
}
fmt.Println("process...")
L1:
    fmt.Println("situation 1")

break

(1) 单独使用,用于跳出break当前所在的for,switch,select语句;
(2) 和标签一起使用,用于跳出标签所标识的for,switch,select语句的执行,可用于跳出多重循环,但标签和break必须在同一函数内。

L1:
    for i:=0;;i++{
        for j:=0;;j++{
            if i>=5{
                //跳出L1标签所在的for循环
                break L1
            }
            if j>10{
                // 默认仅跳出离break最近的内层循环
                break
            }
        }
    }

continue

(1) 单独使用,用于跳出continue当前所在的for循环的本次迭代;
(2) 和标签一起使用,用于跳出标签所标识的for语句的本次迭代,但标签和continue必须在同一函数内。

L1:
    for i:=0;;i++{
        for j:=0;;j++{
            if i>=5{
                //跳到L1标签所在的for循环i++处执行
                continue L1
            }
            if j>10{
                // 默认仅跳到离break最近的内层循环j++处执行
                continue
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WUYANGEZRA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值