1. 程式人生 > >loop迴圈

loop迴圈

--------------------------loop-------------------------
-- 一個table
local languages =
{
	"lua",
	"c",
	"c++",
	"java",
	"python",
}
local elementnum = #(languages)

-- while 迴圈
print("\nwhile loop result:")

local index = 1
while index <= elementnum do
	print("[" .. index .. "] = " .. languages[index])
	index = index + 1
end

-- repeat 迴圈
print("\nrepeat loop result:")

index = 1
repeat
	print("[" .. index .. "] = " .. languages[index])
	index = index + 1
until index > elementnum

-- for 迴圈
print("\nfor loop result:")

for i = 1,elementnum,1 do
	print("[" .. i .. "] = " .. languages[i])
end

-- generic for 迴圈
print("\ngeneric for loop result:")
for i,value in ipairs(languages) do
	print("[" .. i .. "] = " .. value)
end
print('-----pairs------')
for i,value in pairs(languages) do
	print("[" .. i .. "] = " .. value)
end