|
| 26 Sep 2012 01:46 PM |
hey guys. i need some help with for loops. i dont quite understand them yet
for 1, 10 do print("lol") end
i get this error
input:1: [name] expected near '1'
what is wrong? pls help
the [ is < and ] is > |
|
|
| Report Abuse |
|
|
|
| 26 Sep 2012 01:53 PM |
When you use a for loop, you need to supply a variable. Example:
for i = 1, 10 do print(i) end
Output > 1 2 3 4 5 6 7 8 9 10
The variable can be whatever you want; it doesn't just have to be i. |
|
|
| Report Abuse |
|
|
|
| 26 Sep 2012 01:55 PM |
for variable = 1, 10 do print(i) end
why does it keep printing nil? im so confused |
|
|
| Report Abuse |
|
|
| |
|
|
| 26 Sep 2012 02:05 PM |
you do
print(variable)
in your case |
|
|
| Report Abuse |
|
|
|
| 26 Sep 2012 02:09 PM |
| ah i see. is there any way to count backwards? |
|
|
| Report Abuse |
|
|
Daelus
|
  |
| Joined: 06 Sep 2012 |
| Total Posts: 144 |
|
|
| 26 Sep 2012 02:09 PM |
--[[ Supply variable is defined as "i". You call "i" on the second line, printing the number. ]]
for i = 1, 10 do print(i) end
--[[ Supply variable is defined as "variable". You call undefined "i" on the second line, printing nil. ]]
for variable = 1, 10 do print(i) end |
|
|
| Report Abuse |
|
|
usama8800
|
  |
| Joined: 29 Dec 2009 |
| Total Posts: 225 |
|
|
| 26 Sep 2012 02:12 PM |
the optional iterator is what u need for i = 10 , 1 , -1 do -- makes i 10 and adds -1 on it until it becomes 1 print(i) end |
|
|
| Report Abuse |
|
|
Daelus
|
  |
| Joined: 06 Sep 2012 |
| Total Posts: 144 |
|
|
| 26 Sep 2012 02:14 PM |
That wouldn't work, usama. for loops are meant to count up, and it will stop once the supply variable is at, or higher, than the max value. |
|
|
| Report Abuse |
|
|
|
| 26 Sep 2012 02:15 PM |
for i = 10, 1, -2 do print(i) end
prints out 10 8 6 4 2 subtracting 2 each time. i think i get it now thanks
new question tho. what is pairs? i see it a lot in other scripts |
|
|
| Report Abuse |
|
|
|
| 26 Sep 2012 02:16 PM |
"for loops are meant to count up, and it will stop once the supply variable is at, or higher, than the max value."
Wrong. For loops can count both up and down, depending on the third argument. |
|
|
| Report Abuse |
|
|
Daelus
|
  |
| Joined: 06 Sep 2012 |
| Total Posts: 144 |
|
|
| 26 Sep 2012 02:19 PM |
| That's interesting. I guess I've always made the script wrong; it's never worked for me. |
|
|
| Report Abuse |
|
|