Chapter 6 — Loops + Tables (🤖 The Warehouse Robot)
This chapter is where Lua becomes actually useful.
You've learned:
Variables → Remember one thing
Tables → Remember many things
Loops → Repeat work
Now combine them.
Think of it like this:
A table is a warehouse.
A loop is the robot walking through every shelf.
🎯 Mission
Imagine an inventory:
Sword
Shield
Potion
Bow
Apple
Without loops:
print(inventory[1])
print(inventory[2])
print(inventory[3])
print(inventory[4])
print(inventory[5])
😩 Boring.
Instead:
for i = 1,5 do
print(inventory[i])
end
One loop.
Infinite happiness.
🟢 LEVEL 1 — Survival
Q1
You have 100 inventory items.
How should you print them?
A) 100 print() statements
B) One for loop
C) 100 variables
D) One if
✅ Answer: B
💡 Let the loop do the work.
Q2
fruits = {
"Apple",
"Banana",
"Orange"
}
for i = 1,3 do
print(fruits[i])
end
What prints first?
A) Banana
B) Apple
C) Orange
D) 1
✅ Answer: B
Q3
Same code.
What prints last?
A) Apple
B) Banana
C) Orange
D) 3
✅ Answer: C
Q4
Why is a loop useful with tables?
A) It visits every item automatically.
B) It creates more tables.
C) It prints only one item.
D) It deletes data.
✅ Answer: A
Q5
numbers = {
5,
10,
15
}
for i = 1,3 do
print(numbers[i])
end
How many numbers are printed?
A) 1
B) 2
C) 3
D) Infinite
✅ Answer: C
Q6
Which combination is most powerful?
A) Variable + Variable
B) Function + Function
C) Loop + Table
D) If + Boolean
✅ Answer: C
💡 This is used everywhere in real Lua projects.
🟡 LEVEL 2 — Think Like a Programmer
Q7
items = {
"Sword",
"Bow",
"Potion"
}
for i = 1,3 do
print(items[i])
end
Output?
A)
Sword
Bow
Potion
B)
Potion
Bow
Sword
C)
Sword
Sword
Sword
D) Error
✅ Answer: A
Q8
scores = {
10,
20,
30
}
for i = 1,3 do
scores[i] = scores[i] + 5
end
print(scores[2])
Output?
A) 20
B) 25
C) 30
D) 35
✅ Answer: B
💡 Every score increased by 5.
Q9
coins = {
5,
5,
5
}
total = 0
for i = 1,3 do
total = total + coins[i]
end
print(total)
Output?
A) 5
B) 10
C) 15
D) 20
✅ Answer: C
💡 This is called summing values.
Q10
animals = {
"Dog",
"Cat"
}
for i = 1,2 do
print(i)
end
Output?
A)
1
2
B)
Dog
Cat
C)
Dog
1
D) Error
✅ Answer: A
💡 i is the counter, not the table item.
Q11
animals = {
"Dog",
"Cat"
}
for i = 1,2 do
print(animals[i])
end
Output?
A)
Dog
Cat
B)
1
2
C) Error
D) nil
✅ Answer: A
Q12
Which line accesses the current table item inside the loop?
A)
items
B)
items[i]
C)
i[items]
D)
items()
✅ Answer: B
🟠 LEVEL 3 — Real-Life Programming
Q13
You have 200 player names.
Best approach?
A) 200 variables
B) Table + Loop
C) 200 functions
D) One if
✅ Answer: B
Q14
A shop needs to display every product.
Best tool?
A) Loop through a table
B) 500 print statements
C) Boolean
D) Function only
✅ Answer: A
Q15
Every enemy should lose 10 HP.
You have 50 enemies stored in a table.
Best approach?
A) Update each manually
B) Loop through the table
C) Use if
D) Use print
✅ Answer: B
Q16
A teacher wants to print every student's name.
Best solution?
A) One table + loop
B) 100 variables
C) One function only
D) One boolean
✅ Answer: A
Q17
Spotify wants to display your playlist.
What would they likely use?
A) Loop + Table
B) Variables only
C) if
D) Boolean
✅ Answer: A
Q18
A game loads every saved item.
Best solution?
A) Table + Loop
B) Function only
C) Variable only
D) if
✅ Answer: A
🔴 LEVEL 4 — Bug Hunter
Q19
items = {
"Sword",
"Bow"
}
for i = 0,1 do
print(items[i])
end
What's the problem?
A) Lua tables usually start at 1
B) Missing do
C) Missing end
D) Nothing
✅ Answer: A
Q20
items = {
"Sword",
"Bow"
}
for i = 1,2 do
print(items)
end
Problem?
A) Should be items[i]
B) Missing end
C) Missing for
D) Nothing
✅ Answer: A
Q21
items = {
"Sword",
"Bow"
}
for i = 1,3 do
print(items[i])
end
What prints last?
A)
Sword
B)
Bow
C)
nil
D) Error
✅ Answer: C
💡 There is no third item.
Q22
numbers = {
10,
20
}
for i = 1,2 do
numbers[i] = numbers[i] * 2
end
print(numbers[2])
Output?
A) 20
B) 40
C) 10
D) 80
✅ Answer: B
Q23
items = {
"Sword"
}
print(items[5])
Output?
A) Error
B) Sword
C) nil
D) 5
✅ Answer: C
Q24
What should the loop variable (i) usually represent?
A) The current position in the table
B) The whole table
C) A function
D) A boolean
✅ Answer: A
🟣 LEVEL 5 — Boss Battle 👑
Q25
Which pair is used in almost every Lua game?
A) Variable + Boolean
B) Table + Loop
C) If + Number
D) Function + Boolean
✅ Answer: B
Q26
What does items[i] mean?
A) The entire table
B) The current item at position i
C) Create a table
D) Delete an item
✅ Answer: B
Q27
scores = {
10,
20,
30
}
for i = 1,3 do
print(scores[i] + 1)
end
What prints?
A)
11
21
31
B)
10
20
30
C)
1
2
3
D) Error
✅ Answer: A
Q28
If a table has 1,000 items, should you write 1,000 print() statements?
A) Yes
B) No, use a loop
C) Use 1,000 variables
D) Use 1,000 functions
✅ Answer: B
Q29
You're building a game's inventory system.
Which idea is best?
A) One variable per item
B) Store items in a table and loop through them
C) Use only functions
D) Use only if
✅ Answer: B
Q30 — Final Boss 🏆
Complete the sentence:
Tables store the collection. Loops visit the collection.
Which answer matches that idea?
A) Tables and loops are unrelated.
B) Tables hold data, loops process each piece of data.
C) Loops replace tables.
D) Tables replace loops.
✅ Answer: B
💡 This is one of the most important patterns in programming. Whether you're making a game, a website, or an app, you'll constantly:
Store many things in a collection (a table in Lua).
Loop through that collection to display, update, search, or calculate something.
🧠 Pareto Cheat Sheet
| Situation | Use |
|---|---|
| Store many values | Table {} |
| Visit every value | for loop |
| Current item | table[i] |
| Read an item | print(table[i]) |
| Change an item | table[i] = ... |
| Missing item | nil |
🏆 The Biggest Programming Pattern You've Learned
You now know the four building blocks that appear in almost every program:
🧠 Variables → Remember one value.
🤔 If → Make decisions.
🤖 Loops → Repeat work.
📦 Tables → Store many values.
Almost every real program is just these four working together. Once you're comfortable combining them, the remaining chapters become much easier because they're mostly refinements of these core ideas.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.