dongpiao1983 2016-05-27 12:51
浏览 15
已采纳

Go中以下哪些有效关键字可用于控制循环?

I saw this question that for correct answer had 'for and range'.

But the for statement is the only available looping statement in Go,and the range keyword allows you to iterate over items of a list like an array or a map. For understanding it, you could translate the range keyword to for each index of.

//for loop
package main

import "fmt"

func main() {
for i := 0; i < 5; i++ {
    fmt.Println("Value of i is now:", i)
    }
}

 //range is used inside a for loop


a := [...]string{"a", "b", "c", "d"}
for i := range a {
    fmt.Println("Array item", i, "is", a[i])
}
capitals := map[string] string {"France":"Paris", "Italy":"Rome",     "Japan":"Tokyo" }
for key := range capitals {
    fmt.Println("Map item: Capital of", key, "is", capitals[key])
    }

//range can also return two items, the index/key and the corresponding value 
for key2, val := range capitals {
    fmt.Println("Map item: Capital of", key2, "is", val)
    }
  • 写回答

1条回答 默认 最新

  • duanfei8897 2016-05-28 20:09
    关注

    I think the question is about Different Forms of For Loop:
    simple loop variants working sample:

    package main
    
    import "fmt"
    
    func main() {
        //0 1 2 3 4 5 6 7 8 9
        for i := 0; i < 10; i++ {
            fmt.Print(i, " ")
        }
        fmt.Println()
    
        i := 0
        for ; i < 10; i++ {
            fmt.Print(i, " ")
        }
        fmt.Println()
    
        for i := 0; i < 10; {
            fmt.Print(i, " ")
            i++
        }
        fmt.Println()
    
        //2 4 6 8 10 12 14 16 18 20
        for i := 1; ; i++ {
            if i&1 == 1 {
                continue
            }
            if i == 22 {
                break
            }
            fmt.Print(i, " ")
        }
        fmt.Println()
    
        i = 0
        for {
            fmt.Print(i, " ")
            i++
            if i == 10 {
                break
            }
    
        }
        fmt.Println()
    
        for i, j := 0, 0; i < 5 && j < 10; i, j = i+1, j+2 {
            fmt.Println(i, j)
        }
    }
    

    for array, slice,string, map, channel using range keyword:

    package main
    
    import "fmt"
    
    func main() {
        ary := [5]int{0, 1, 2, 3, 4}
        for index, value := range ary {
            fmt.Print("ary[", index, "] =", value, " ")
        }
        fmt.Println()
        for index := range ary {
            fmt.Print("ary[", index, "] =", ary[index], " ")
        }
        fmt.Println()
        for _, value := range ary {
            fmt.Print(value, " ")
        }
        fmt.Println()
    
        slice := []int{20, 21, 22, 23, 24, 25, 26, 27, 28, 29}
        for index, value := range slice {
            fmt.Println("slice[", index, "] =", value)
        }
        fmt.Println()
    
        str := "Hello"
        for index, value := range str {
            fmt.Println("str[", index, "] =", value)
        }
        fmt.Println()
    
        mp := map[string]int{"One": 1, "Two": 2, "Three": 3}
        for key, value := range mp {
            fmt.Println("map[", key, "] =", value)
        }
        fmt.Println()
    
        ch := make(chan int, 10)
        for i := 0; i < 10; i++ {
            ch <- i
        }
        close(ch)
    
        for i := range ch {
            fmt.Print(i, " ")
        }
        fmt.Println()
    }
    

    and Label for break Label and continue Label:

    package main
    
    import "fmt"
    
    func main() {
        a, b := 1, 1
    Loop1:
        for {
            b++
        Loop2:
            for {
                a++
                switch {
                case a == 2:
                    fallthrough
                case a == 3:
                    fmt.Println(3)
                case a == 4, a == 5:
                    continue
                case a == 7:
                    continue Loop1
                case a == 9:
                    break Loop2
                case a == 10:
                    break Loop1
                case a == 8:
                    break
                default:
                    fmt.Println(a, b)
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线