Golang 流程控制以及循环

 

if-else语句

- if-else if-else 语法与python类同;唯一的区别是python中的作用域用缩进表示,Go中的作用域用括号表示:

- 注意:else 语句必须在 if 语句的大括号 } 之后的同一行中;

  - 原因是 Go 语言的分号是自动插入

  - 在 Go 语言规则中,它指定在 } 之后插入一个分号,作为该行的最终标记。因此,在if语句后面的 } 会自动插入一个分号,所以会报错;

- 示例:

package main

import (
    "fmt"
)

func main() {
    num := 80
    if num < 60 {
        fmt.Println("num小于60")
    } else if num < 80 {
        fmt.Println("num小于80,大于60")
    } else {
        fmt.Println("num大于80")
    }

}

 

- 条件判断中 的私有变量:

  - 语法:

  if statement; condition {  
  }

  - statement在if语句中初始化, 所以 statement只能在当前条件判断下的作用域访问,其他地方访问不到;

  - 示例:

package main

import (  
    "fmt"
)

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

 

switch语句

- switch 是一个条件语句,用于将表达式的值与可能匹配的选项列表进行比较,并根据匹配情况执行相应的代码块。是替代多个 else if 子句的常用方式;

- 语法:

    switch 变量 {
        case 条件:
            逻辑
        case 条件2:
            逻辑
        case 条件3:
            逻辑
        ...
        default:// 默认,也就是说当上面的条件都不满足时,执行下面的代码;
            逻辑
    }

 

- 没有表达式的 switch:

  - 在 switch 语句中,表达式是可选的,可以被省略;省略表达式等同于:switch true, 

  - 示例:

package main

import (
    "fmt"
)

func main() {
    num := 75
    switch { // 表达式被省略了
    case num >= 0 && num <= 50:
        fmt.Println("num is greater than 0 and less than 50")
    case num >= 51 && num <= 100:
        fmt.Println("num is greater than 51 and less than 100")
    case num >= 101:
        fmt.Println("num is greater than 100")
    }

}
// 打印内容: num is greater than 51 and less than 100

 

- 多表达式判断:

  - 在case 中 可以存在多个表达式,表达式之间用逗号隔开;

  - 示例:

package main

import (
    "fmt"
)

func main() {
    letter := "i"
    switch letter {
    case "a", "e", "i", "o", "u": // 一个选项多个表达式
        fmt.Println("vowel")
    default:
        fmt.Println("not a vowel")
    }
}
// 打印内容:
vowel

 

- fallthroungh

  - 介绍:

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

  - 注意:fallthroungh 应该在 case 子句的最后一个语句,如果出现在了别处,会报错

  - 示例:

package main

import (
    "fmt"
)

func number() int {
    num := 15 * 5 
    return num
}

func main() {

    switch num := number(); { // switch 和 case 的表达式不一定是常量
    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)
    }

}
// 打印内容:
    75 is lesser than 100  
    75 is lesser than 200

 

循环:for

- for:

for 是 Go 语言唯一的循环语句。
Go 语言中并没有其他语言比如 Python 语言中的 while

 

- 语法:

for initialisation; condition; post {  
}

initialisation :初始化表达式或赋值语句

condition:循环条件判定表达式

post:循环变量修正表达式
以上三个参数,皆可写可不写

 

- break 语句:

  - 用于在完成正常执行之前突然终止 for 循环,之后程序将会在 for 循环下一行代码开始执行。等同于 Python的 break

 

- continue 语句:

  - 用来跳出 for 循环中当前循环。在 continue 语句后的所有的 for 循环语句都不会在本次循环中执行。循环体会在一下次循环中继续执行。等同于 Python的 continue 

 

- 无限循环:

package main

import "fmt"

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

 

 

- 基础例子:

package main

import (  
    "fmt"
)

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

 

- 初始化写在循环外,for后面只跟条件语句:

package main

import (  
    "fmt"
)

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

 

- 声明和操作多个变量

package main

import (  
    "fmt"
)

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)
    }

}
// 打印内容:
"""
10 * 1 = 10  
11 * 2 = 22  
12 * 3 = 36  
13 * 4 = 52  
14 * 5 = 70  
15 * 6 = 90  
16 * 7 = 112  
17 * 8 = 136  
18 * 9 = 162  
19 * 10 = 190
"""

 

posted @ 2018-10-30 21:46  浮生凉年  阅读(374)  评论(0编辑  收藏  举报