G0(流程控制)

二、流程控制

2.1.if语句

条件语句。
语法:if condition {}

    func main() {  
        num := 10
        if num % 2 == 0 { //checks if number is even
            fmt.Println("the number is even") 
        }  else {
           fmt.Println("the number is odd")
        }
    }

if 还有另外一种形式,它包含一个 statement 可选语句部分,该组件在条件判断之前运行
语法:if statement; condition {}

示例:

func main() {  
    if num := 10; num % 2 == 0 { //checks if number is even
        fmt.Println(num,"is even") 
    }  else {
        fmt.Println(num,"is odd")
    }
}

注意:else 语句应该在 if 语句的大括号 } 之后的同一行中。如果不是,编译器会不通过。

2.2.循环(for)

for 是 Go 语言唯一的循环语句。Go 语言中并没有其他语言比如 C 语言中的 while 和 do while 循环。
语法:for initialisation; condition; post {}

func main() {  
    for i := 1; i <= 10; i++ {
        fmt.Printf(" %d",i)
    }
}

初始化语句、条件语句、post 语句都是可选的

func main() {  
    i := 0
    for ;i <= 10; { // initialisation and post are omitted
        fmt.Printf("%d ", i)
        i += 2
    }
}

for 循环中的分号也可以省略

func main() {  
    i := 0
    for i <= 10 { //semicolons are ommitted and only condition is present
        fmt.Printf("%d ", i)
        i += 2
    }
}

在 for 循环中可以声明和操作多个变量。让我们写一个使用声明多个变量来打印下面序列的程序。

func main() {  
    for no, i := 10, 1; i <= 10 && no <= 19; i, no = i+1, no+1 { //multiple initialisation and increment
        fmt.Printf("%d * %d = %d\n", no, i, no*i)
    }
}

无限循环。

func main() {  
    for {
        fmt.Println("Hello World")
    }
}

2.3键值循环(for range)

Go语言可以使用for range 遍历数组、切片、字符串、map和通道。
数组、切片、字符串返回索引和值;map返回键值;通道只返回通道内的值。

//遍历数组、切片
for key , value := range []int{1,2,3,4}{}

//遍历字符串
for key , value := range str{}

//遍历map
for key , value := range map[String]{"key1":"value1"}{}

//遍历通道
for v := range c{}

//使用匿名变量(_)来选择希望的值

2.4.switch语句

  • 正常情况
func main() {
    switch finger := 8; finger {
    case 1:
        fmt.Println("Thumb")
    case 2:
        fmt.Println("Index")
    case 3:
        fmt.Println("Middle")
    case 4:
        fmt.Println("Ring")
    case 5:
        fmt.Println("Pinky")
    default: // 默认情况
        fmt.Println("incorrect finger number")
    }
}
  • 多表达式
func main() {
    letter := "i"
    switch letter {
    case "a", "e", "i", "o", "u": // 一个选项多个表达式
        fmt.Println("vowel")
    default:
        fmt.Println("not a vowel")
    }
}
  • 无表达式的 switch(无实际意义,同if-else)

在 switch 语句中,表达式是可选的,可以被省略。如果省略表达式,则表示这个 switch 语句等同于 switch true,并且每个 case 表达式都被认定为有效,相应的代码块也会被执行。

  • Fallthrough 语句

在 Go 中,每执行完一个 case 后,会从 switch 语句中跳出来,不再做后续 case 的判断和执行。使用 fallthrough 语句可以在已经执行完成的 case 之后,把控制权转移到下一个 case 的执行代码中。

顺序执行,区别于break。

func main() {

    switch num := number(); { // num is not a constant
    case num < 50:
        fmt.Printf("%d is lesser than 50\n", num)
        fallthrough
    case num < 100:
        fmt.Printf("%d is lesser than 100\n", num)
        fallthrough
    case num < 200:
        fmt.Printf("%d is lesser than 200", num)
    }

}

另外:switch 和 case 的表达式不一定是常量,几乎可以是任何值。
fallthrough 语句应该是 case 子句的最后一个语句。如果它出现在了 case 语句的中间,编译器将会报错:fallthrough statement out of place

2.5 跳转到指定代码标签 (goto)

goto 语句通过标签进行代码间的无条件跳转。多层循环时,使用goto比较方便退出。
集中处理错误。

err := firstCheckError()
if err != nil {
    goto onExit
}

err := secondCheckError()
if err != nil {
    goto onExit
}

onExit:
    fmt.Println(err)
    exitProcess()

2.6 跳出指定循环 (break)

break可以结束 for、witch和select的代码块。break可以再语句后边添加标签,表示退出某个标签对应的代码块,标签要求必须定义在对应的代码块上。

OuterLoop:
    for i := 0 ; i < 10 ; i++{
        for j := 0 ; j< 10; j++{
            if j == 3{
                break OuterLoop
            }
        }
    }

你可能感兴趣的:(G0(流程控制))