for循环遍历
1.数值for
for i = 1,10,1 do --初值,终值,步数(正数则每次循环加该数,负数则i减该数),步数可以省略(默认为1)
print(i*2); --2,4,6,...20
end
2.泛型for in pairs
这种遍历表的方式会遍历所有的键值对
days = {"Sunday", x = 1, y = 2,"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", [20] = "aaa","Saturday"}
for k,v in pairs(days) do
print(k,v)//{1,Sunday},{2,Monday},{3,Tuesday},...,{7,Saturday},{x,1},{20,"aaa"}{y,2}
end
2.泛型for in ipair
这种遍历表的方式会从数字索引(interger key)1开始遍历,如果下一个数字索引不存在,则结束
days = {"Sunday", x = 1, y = 2,"Monday", "Tuesday", "Wednesday",
"Thursday", "Friday",[20] = "aaa", "Saturday"}
for k,v in ipairs(days) do
print(k,v)//{1,Sunday},{2,Monday},{3,Tuesday},...,{7,Saturday}
end