ผลต่างระหว่างรุ่นของ "ภาษาลูอา"

เนื้อหาที่ลบ เนื้อหาที่เพิ่ม
ZilentFyld (คุย | ส่วนร่วม)
ZilentFyld (คุย | ส่วนร่วม)
บรรทัด 49:
end
return x
end
</syntaxhighlight>
 
=== การควบคุมการไหล ===
ลูอามีการ[[วนซ้ำ (คอมพิวเตอร์)|ทำซ้ำ]]อยู่สี่แบบ: [[while loop|<code>while</code> loop]], <code>repeat</code> loop (คล้ายกับ [[do while loop|<code> do while</code> loop]]), [[for loop|<code>for</code> loop]] แบบตัวเลข และ <code>for</code> loop ทัว่ไป
<syntaxhighlight lang="lua">
--condition = true
 
while condition do
--statements
end
 
repeat
--statements
until condition
 
for i = first, last, delta do --delta may be negative, allowing the for loop to count down or up
--statements
--example: print(i)
end
</syntaxhighlight>
 
<code>for</code> loop แบบทั่วไป:
<syntaxhighlight lang="lua">
for key, value in pairs(_G) do
print(key, value)
end
</syntaxhighlight>
จะวนซ้ำบนตาราง <code>_G</code> โดยใช้ฟังก์ชันมาตรฐาน <code>pairs</code> วนซ้ำจนกว่าจะคืนค่า <code>nil</code>
 
การวนซ้ำยังสามารถใช้ซ้อนทับกันได้
<syntaxhighlight lang="lua">
local grid = {
{ 11, 12, 13 },
{ 21, 22, 23 },
{ 31, 32, 33 }
}
 
for y, row in ipairs(grid) do
for x, value in ipairs(row) do
print(x, y, grid[y][x])
end
end
</syntaxhighlight>