了解多个条件在C环 [英] Understanding multiple conditions in a C loop

查看:99
本文介绍了了解多个条件在C环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是一块C $ C $的C

There is a piece of C code

int i, j = 0;
for (i = 0, j = n-1; i < n; j = i++) {
    // array operations
}

这是我试图转换成Lua的code

that I'm trying to convert into Lua code

local j = n-1
for i = 1, n do -- arrays are 1-based in Lua
    -- array operations
    j = i+1
end

但由于某些原因的Lua code不起作用。我收到了空调回路吧?

but for some reason Lua code doesn't work. Am I getting that C loop right?

推荐答案

设想一个数组作为循环缓冲区。 I 是当前项目和Ĵ始终是previous之一:

Imagine an array as a circular buffer. i is the current item and j is always the previous one:

n = 5
local j = n
for i = 1, n do -- arrays are 1-based in Lua
    -- array operations
    io.write('i=', i, ' j=', j, '\n')
    j = i
end

演示

i=1 j=5
i=2 j=1
i=3 j=2
i=4 j=3
i=5 j=4

这篇关于了解多个条件在C环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆