lua 3

Perfect. Chapter 3 is where programming starts to feel powerful.

Chapter 3 — Loops (The Robot 🤖)

🎯 Mission

Imagine you're playing Minecraft.

Would you rather place 100 blocks by writing:

placeBlock()
placeBlock()
placeBlock()
...

100 times?

Or tell the computer:

"Place this block 100 times."

That's what loops do.


🟢 LEVEL 1 — Survival (Basics)


Q1

You want to print "Hello" 5 times.

What's the best tool?

A) Variable

B) if

C) Loop

D) Table

Answer: C

💡 Loops repeat work automatically.


Q2

Which loop is best when you already know you'll repeat something exactly 10 times?

A) if

B) for

C) while

D) elseif

Answer: B

💡 for is made for fixed repetitions.


Q3

for i = 1,3 do
    print("Hi")
end

How many times is "Hi" printed?

A) 2

B) 3

C) 4

D) Infinite

Answer: B

💡 The loop runs for 1, 2, and 3.


Q4

for i = 1,5 do
    print(i)
end

What's printed last?

A) 4

B) 5

C) 6

D) 1

Answer: B

💡 The last value of i is 5.


Q5

The variable i usually means...

A) Inventory

B) Index/Counter

C) Integer only

D) Input

Answer: B

💡 It's just a counting variable. The name could be anything.


Q6

What keyword ends a for loop?

A) stop

B) finish

C) end

D) close

Answer: C

💡 Lua closes blocks with end.


Q7

for i = 5,5 do
    print(i)
end

Output?

A) Nothing

B) 5

C) Error

D) 55

Answer: B

💡 Start and end are the same, so it runs once.


Q8

Which loop repeats while a condition stays true?

A) for

B) while

C) if

D) print

Answer: B

💡 while keeps going until its condition becomes false.



🟡 LEVEL 2 — Think Like a Programmer


Q9

for i = 1,4 do
    print(i)
end

Output?

A)

1
2
3
4

B)

0
1
2
3

C)

1
2
3

D) Error

Answer: A


Q10

coins = 10

for i = 1,3 do
    coins = coins + 5
end

print(coins)

Output?

A) 10

B) 15

C) 20

D) 25

Answer: D

💡 10 → 15 → 20 → 25


Q11

sum = 0

for i = 1,3 do
    sum = sum + i
end

print(sum)

Output?

A) 3

B) 6

C) 9

D) 0

Answer: B

💡 0 + 1 + 2 + 3 = 6


Q12

for i = 2,4 do
    print("Lua")
end

How many times?

A) 2

B) 3

C) 4

D) 5

Answer: B

💡 Runs for 2, 3, and 4.


Q13

health = 100

for i = 1,2 do
    health = health - 20
end

print(health)

Output?

A) 60

B) 80

C) 40

D) 100

Answer: A

💡 100 → 80 → 60


Q14

for i = 3,1 do
    print(i)
end

Output?

A)

3
2
1

B) Nothing

C) Error

D)

1
2
3

Answer: B

💡 Without a negative step, Lua can't count downward.



🟠 LEVEL 3 — Real-Life Programming


Q15

A game gives 10 XP after every monster.

You defeat 5 monsters.

Best solution?

A) Five xp = xp + 10 lines

B) One for loop

C) if

D) Table

Answer: B


Q16

You need to send 100 emails.

What should you use?

A) 100 print() statements

B) Variable

C) Loop

D) Boolean

Answer: C


Q17

A countdown should display:

5
4
3
2
1

Which loop is best?

A) if

B) while

C) for (with a negative step)

D) elseif

Answer: C

💡 Example:

for i = 5,1,-1 do
    print(i)
end

Q18

You don't know how many times a player will fight monsters.

Best loop?

A) for

B) while

C) Variable

D) Table

Answer: B

💡 Unknown repetitions usually use while.


Q19

Your phone keeps checking for Wi-Fi until it connects.

Best loop?

A) while

B) for

C) if

D) print

Answer: A


Q20

You want a game menu to keep showing until the player chooses "Exit."

Best loop?

A) while

B) for

C) if

D) Table

Answer: A


🔴 LEVEL 4 — Bug Hunter


Q21

for i = 1,5
    print(i)
end

What's wrong?

A) Missing do

B) Missing then

C) Missing if

D) Nothing

Answer: A


Q22

for i = 1,5 do
print(i)

Problem?

A) Missing end

B) Missing do

C) Missing then

D) Nothing

Answer: A


Q23

while true do
    print("Hi")
end

What happens?

A) Prints once

B) Infinite loop

C) Error

D) Prints twice

Answer: B

💡 The condition is always true.


Q24

count = 1

while count <= 3 do
    print(count)
end

Problem?

A) Missing count = count + 1

B) Missing for

C) Missing print

D) Nothing

Answer: A

💡 count never changes, so the loop never ends.


Q25

for i = 1,5 do
    break
end

How many iterations happen?

A) 5

B) 1

C) 0

D) Infinite

Answer: B

💡 break exits the loop immediately.


🟣 LEVEL 5 — Boss Battle 👑


Q26

Which loop is usually the best choice when you know exactly how many times something should happen?

A) while

B) for

C) if

D) repeat

Answer: B


Q27

Which loop is better when the number of repetitions is unknown?

A) for

B) while

C) Variable

D) Table

Answer: B


Q28

coins = 0

for i = 1,5 do
    coins = coins + 2
end

print(coins)

Output?

A) 2

B) 5

C) 10

D) 12

Answer: C


Q29

for i = 1,3 do
    print("Win")
end

print("Done")

What prints last?

A) Win

B) Done

C) 3

D) Error

Answer: B


Q30 (The Final Question)

What is the real purpose of loops?

A) Make code longer

B) Store data

C) Repeat work automatically

D) Compare values

Answer: C

💡 If variables are the memory of a program and if is its brain, then loops are its robot workers. They happily repeat tasks thousands or even millions of times without getting tired.


🧠 Pareto Cheat Sheet (Save This)

SituationUse
Repeat exactly 10 timesfor
Repeat until something changeswhile
Exit a loop earlybreak
Start a for loopfor ... do
End any loopend
Counter variablei (or any name)

🏆 Boss Tip

Don't memorize syntax. Instead, train yourself to answer this question whenever you face a problem:

"Do I know how many times this should repeat?"

  • Yes → Use a for loop.

  • No → Use a while loop.

That single decision covers the majority of loop-related code you'll write as a beginner.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.