5.4 for 结构

    注意事项 其它许多语言中也没有发现和 do while 完全对等的 for 结构,可能是因为这种需求并不是那么强烈。

    文件 for1.go 中演示了最简单的基于计数器的迭代,基本形式为:

    示例 5.6 :

    1. import "fmt"
    2. func main() {
    3. for i := 0; i < 5; i++ {
    4. fmt.Printf("This is the %d iteration\n", i)
    5. }
    6. }

    输出:

    1. This is the 0 iteration
    2. This is the 1 iteration
    3. This is the 2 iteration
    4. This is the 3 iteration
    5. This is the 4 iteration

    由花括号括起来的代码块会被重复执行已知次数,该次数是根据计数器(此例为 i)决定的。循环开始前,会执行且仅会执行一次初始化语句 i := 0;;这比在循环之前声明更为简短。紧接着的是条件语句 i < 5;,在每次循环开始前都会进行判断,一旦判断结果为 false,则退出循环体。最后一部分为修饰语句 i++,一般用于增加或减少计数器。

    这三部分组成的循环的头部,它们之间使用分号 ; 相隔,但并不需要括号 () 将它们括起来。例如:for (i = 0; i < 10; i++) { },这是无效的代码!

    同样的,左花括号 { 必须和 for 语句在同一行,计数器的生命周期在遇到右花括号 } 时便终止。一般习惯使用 i、j、z 或 ix 等较短的名称命名计数器。

    特别注意,永远不要在循环体内修改计数器,这在任何语言中都是非常差的实践!

    您还可以在循环中同时使用多个计数器:

    1. for i, j := 0, N; i < j; i, j = i+1, j-1 {}

    这得益于 Go 语言具有的平行赋值的特性(可以查看第 7 章 string_reverse.go 中反转数组的示例)。

    您可以将两个 for 循环嵌套起来:

    1. for i:=0; i<5; i++ {
    2. for j:=0; j<10; j++ {
    3. println(j)
    4. }
    5. }

    如果您使用 for 循环迭代一个 Unicode 编码的字符串,会发生什么?

    示例 5.7 for_string.go

    1. package main
    2. import "fmt"
    3. func main() {
    4. str := "Go is a beautiful language!"
    5. fmt.Printf("The length of str is: %d\n", len(str))
    6. for ix :=0; ix < len(str); ix++ {
    7. fmt.Printf("Character on position %d is: %c \n", ix, str[ix])
    8. }
    9. str2 := "日本語"
    10. fmt.Printf("The length of str2 is: %d\n", len(str2))
    11. for ix :=0; ix < len(str2); ix++ {
    12. fmt.Printf("Character on position %d is: %c \n", ix, str2[ix])
    13. }
    14. }

    输出:

    如果我们打印 str 和 str2 的长度,会分别得到 27 和 9。

    练习 5.4

    1. 使用 for 结构创建一个简单的循环。要求循环 15 次然后使用 fmt 包来打印计数器的值。
    2. 使用 goto 语句重写循环,要求不能使用 for 关键字。

    练习 5.5 for_character.go

    创建一个程序,要求能够打印类似下面的结果(尾行达 25 个字符为止):

    1. G
    2. GG
    3. GGG
    4. GGGG
    5. GGGGG
    1. 使用 2 层嵌套 for 循环。
    2. 仅用 1 层 for 循环以及字符串连接。

    练习 5.6

    使用按位补码从 0 到 10,使用位表达式 %b 来格式化输出。

    练习 5.7 Fizz-Buzz 问题:fizzbuzz.go

    写一个从 1 打印到 100 的程序,但是每当遇到 3 的倍数时,不打印相应的数字,但打印一次 “Fizz”。遇到 5 的倍数时,打印 Buzz 而不是相应的数字。对于同时为 3 和 5 的倍数的数,打印 FizzBuzz(提示:使用 switch 语句)。

    练习 5.8

    使用 * 符号打印宽为 20,高为 10 的矩形。

    for 结构的第二种形式是没有头部的条件判断迭代(类似其它语言中的 while 循环),基本形式为:。

    您也可以认为这是没有初始化语句和修饰语句的 for 结构,因此 ;; 便是多余的了。

    Listing 5.8 for2.go

    1. package main
    2. import "fmt"
    3. func main() {
    4. var i int = 5
    5. for i >= 0 {
    6. i = i - 1
    7. fmt.Printf("The variable i is now: %d\n", i)
    8. }
    9. }

    输出:

    1. The variable i is now: 4
    2. The variable i is now: 3
    3. The variable i is now: 2
    4. The variable i is now: 1
    5. The variable i is now: 0
    6. The variable i is now: -1

    条件语句是可以被省略的,如 i:=0; ; i++for { }for ;; { };; 会在使用 gofmt 时被移除):这些循环的本质就是无限循环。最后一个形式也可以被改写为 for true { },但一般情况下都会直接写 for { }

    如果 for 循环的头部没有条件语句,那么就会认为条件永远为 true,因此循环体内必须有相关的条件判断以确保会在某个时刻退出循环。

    但这两者之间有所区别,break 只是退出当前的循环体,而 return 语句提前对函数进行返回,不会执行后续的代码。

    无限循环的经典应用是服务器,用于不断等待和接受新的请求。

    1. for t, err = p.Token(); err == nil; t, err = p.Token() {
    2. ...
    3. }

    这是 Go 特有的一种的迭代结构,您会发现它在许多情况下都非常有用。它可以迭代任何一个集合(包括数组和 map,详见第 7 和 8 章)。语法上很类似其它语言中 foreach 语句,但您依旧可以获得每次迭代所对应的索引。一般形式为:for ix, val := range coll { }

    要注意的是,val 始终为集合中对应索引的值拷贝,因此它一般只具有只读性质,对它所做的任何修改都不会影响到集合中原有的值(译者注:如果 val 为指针,则会产生指针的拷贝,依旧可以修改集合中的原值)。一个字符串是 Unicode 编码的字符(或称之为 rune)集合,因此您也可以用它迭代字符串:

    1. for pos, char := range str {
    2. ...
    3. }

    每个 rune 字符和索引在 for-range 循环中是一一对应的。它能够自动根据 UTF-8 规则识别 Unicode 编码的字符。

    示例 5.9 :

    输出:

    1. The length of str is: 27
    2. Character on position 0 is: G
    3. Character on position 1 is: o
    4. Character on position 2 is:
    5. Character on position 3 is: i
    6. Character on position 4 is: s
    7. Character on position 5 is:
    8. Character on position 6 is: a
    9. Character on position 7 is:
    10. Character on position 8 is: b
    11. Character on position 9 is: e
    12. Character on position 10 is: a
    13. Character on position 11 is: u
    14. Character on position 12 is: t
    15. Character on position 13 is: i
    16. Character on position 14 is: f
    17. Character on position 15 is: u
    18. Character on position 16 is: l
    19. Character on position 18 is: l
    20. Character on position 19 is: a
    21. Character on position 20 is: n
    22. Character on position 21 is: g
    23. Character on position 23 is: a
    24. Character on position 24 is: g
    25. Character on position 25 is: e
    26. Character on position 26 is: !
    27. The length of str2 is: 18
    28. character C starts at byte position 0
    29. character h starts at byte position 1
    30. character i starts at byte position 2
    31. character n starts at byte position 3
    32. character e starts at byte position 4
    33. character s starts at byte position 5
    34. character e starts at byte position 6
    35. character : starts at byte position 7
    36. character starts at byte position 8
    37. character starts at byte position 9
    38. character starts at byte position 12
    39. character starts at byte position 15
    40. index int(rune) rune char bytes
    41. 0 67 U+0043 'C' 43
    42. 1 104 U+0068 'h' 68
    43. 2 105 U+0069 'i' 69
    44. 3 110 U+006E 'n' 6E
    45. 4 101 U+0065 'e' 65
    46. 5 115 U+0073 's' 73
    47. 6 101 U+0065 'e' 65
    48. 7 58 U+003A ':' 3A
    49. 8 32 U+0020 ' ' 20
    50. 9 26085 U+65E5 '日' E6 97 A5
    51. 12 26412 U+672C '本' E6 9C AC
    52. 15 35486 U+8A9E '語' E8 AA 9E

    请将输出结果和 Listing 5.7(for_string.go)进行对比。

    我们可以看到,常用英文字符使用 1 个字节表示,而汉字(译者注:严格来说,“Chinese: 日本語”的Chinese应该是Japanese)使用 3 个字符表示。

    练习 5.9 以下程序的输出结果是什么?

    1. for i := 0; i < 5; i++ {
    2. var v int
    3. fmt.Printf("%d ", v)
    4. v = 5
    5. }

    问题 5.2: 请描述以下 for 循环的输出结果:

    1.

    1. for i := 0; ; i++ {
    2. fmt.Println("Value of i is now:", i)
    3. }

    2.

    1. for i := 0; i < 3; {
    2. fmt.Println("Value of i:", i)
    3. }

    3.

    1. s := ""
    2. for ; s != "aaaaa"; {
    3. fmt.Println("Value of s:", s)
    4. s = s + "a"
    5. }

    4.