dougua9328 2017-06-10 23:09
浏览 106
已采纳

Go中多个for循环中的多个变量

I have two for loops with two different set of variables, where I also reuse one variable from one loop in the next. The code looks roughly like this:

func naive(z, x, y []uint32, n int) {
    var i, j, k int
    var A, B uint32

    for i = 0; i < n; i++ {
        B = 0

        for j = 1; j <= i; j++ {
            muladd(x[j], y[i - j], &A)
        }

        for k = 1; j < n; j++, k++ {
            muladd(x[j], y[n - k], &B)
        }
    }
}

But I get an error message at the second for loop. It says missing { after for clause. Any ideas how to solve it?

  • 写回答

1条回答 默认 最新

  • doushan7077 2017-06-10 23:16
    关注

    when you increment both j and k on the last loop go doesn't like it

    so try to change your code to

    func naive(z, x, y []uint32, n int) {
        var i, j, k int
        var A, B uint32
    
        for i = 0; i < n; i++ {
            B = 0
    
            for j = 1; j <= i; j++ {
                muladd(x[j], y[i-j], &A)
            }
    
            for k = 1; j < n; j++ {
                muladd(x[j], y[n-k], &B)
                k++
            }
        }
    }
    

    pay attention to the last loop I just moved the k++ statement inside the loop

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?