252 posts
Posted 17 October 2012 - 09:20 PM
Please help me with this code, it is not working. Here is the error: [string "filename"]:The "FOR" Line (In code): ',' expected.
for i = 8 do
if i < 0 then
term.setCursorPos(x, y)
print("\nRemaining Time: " .. i .. " Seconds.")
os.sleep(1)
i - 1
else
print("\nThe Door Has Been Closed.")
os.sleep(2)
Programs()
end
end
If I am going about doing this wrong, then please explain. Thanks.
136 posts
Posted 17 October 2012 - 09:22 PM
818 posts
Posted 17 October 2012 - 09:24 PM
at the first line
for i=1,8 do
you forgot to tell it where to start :P/>/>
252 posts
Posted 17 October 2012 - 09:25 PM
Wait, so is the "for" for identifying whether a certain thing is supposed to be something, or something else? Like whether something should be…1, or 2? Or something similar to that?
252 posts
Posted 17 October 2012 - 09:26 PM
at the first line
for i=1,8 do
you forgot to tell it where to start :P/>/>
Please explain. I thought "do" started it.I figured out that you changed the code, but it still does not count down :/.
252 posts
Posted 17 October 2012 - 09:41 PM
Figured it out. Used "repeat" instead of "for"
521 posts
Location
Stockholm, Sweden
Posted 17 October 2012 - 11:28 PM
If you want the count in a for loop to decrease instead of increase then you can add another syntax/value:
--old code
for i = 1, 8 do
--new code
for i = 1, 8, -1 do
You see, the 3rd value defines how many steps to "jump" between each loop run.
So for example
for i = 1, 8, 2 do
print(i)
end
this will output
1
3
5
7
It starts at 1, jumps 2 steps and lands on 3 (1+2=3, basic math) then prints the value (3 in this case).
1688 posts
Location
'MURICA
Posted 18 October 2012 - 12:59 AM
I'd like to make it clear that
for i=1, 8, -1 do
will not work, because lua will try to start at 1 and count down to 8, when it realizes that this is impossible without going to infinity so it just does nothing.
The correct way of counting down is
for i=8, 1, -1 do
8543 posts
Posted 18 October 2012 - 04:03 PM
It's possible I'm misremembering, but I believe that when the start is bigger than the finish, step is automatically set to -1 when unspecified.
1688 posts
Location
'MURICA
Posted 18 October 2012 - 04:07 PM
It's possible I'm misremembering, but I believe that when the start is bigger than the finish, step is automatically set to -1 when unspecified.
Just did a test, nope.
2005 posts
Posted 18 October 2012 - 07:29 PM
It would be convenient in some cases, but no, it doesn't do that. And frankly, given that it is common to pass variables into the numeric for, there could be dangers and even inconveniences in having it decide to use decrementation every time the second value happened to be bigger. Usually in that case you want to loop to not run at all.
2447 posts
Posted 18 October 2012 - 07:35 PM
Figured it out. Used "repeat" instead of "for"
Oh god why. It is one of my pet peeves when people use the wrong type of loop :P/>/>
1688 posts
Location
'MURICA
Posted 18 October 2012 - 08:54 PM
There really is no such thing as "using the wrong loop" though. There are recommended loops for various cases but that's about it. Every loop can emulate the purpose of other loops.
From 1 to 10:
for i=1, 10 do
end
i = 1
while i < 10 do
i = i + 1
end
i = 1
repeat
i = i+1
until i == 10
Keep going until condition:
repeat until condition
while not contition do
for i=1, math.huge do
if condition then break end
end
Forever loops:
while true do
repeat until false
for i=1, math.huge do
252 posts
Posted 19 October 2012 - 02:08 AM
Figured it out. Used "repeat" instead of "for"
Oh god why. It is one of my pet peeves when people use the wrong type of loop :)/>/>
Lol, I apologize. I am new to loops :P/>/>. we're you saying I used the wrong one is for? Or repeat? (I am guessing for, but…)
2005 posts
Posted 19 October 2012 - 02:34 AM
There definitely is such a thing as a wrong kind of loop. And it is pretty annoying sometimes to encounter something like a while true do … if something then break end … end kind of construction.
In most of the applications we write, that costs only a few extra processor cycles. In a kernel of serious code, it can be a real performance killer.
8543 posts
Posted 19 October 2012 - 02:38 AM
In many compiled languages, that sort of thing would most likely be optimized away.
2005 posts
Posted 19 October 2012 - 03:15 AM
Yes, but lua is interpreted, not compiled. There are significant benefits in that fact, but it also means that some kinds of advanced optimization are not as…optimal. Besides, "many" x "most likely" still ends up not equaling "always".
2447 posts
Posted 19 October 2012 - 08:21 AM
Yes, but lua is interpreted, not compiled. There are significant benefits in that fact, but it also means that some kinds of advanced optimization are not as…optimal. Besides, "many" x "most likely" still ends up not equaling "always".
Meh, Lua is compiled into Lua byte code before being executed (you can get the byte code using string.dump). However it is never a certainty that things like this get optimised away.
2005 posts
Posted 19 October 2012 - 06:51 PM
Certainly not with lua. The compiling is called "interpretation" because it skips much of what has become semi-standard in most compliers, particularly the complex optimizations of code that are a substantial part of why modern compilers take so long to build a binary. Lua interpretation may technically be compiling in the original sense of the term, but it is different from the modern sense and the interpreter doesn't optimize your code.
There are real benefits to this, many of the best tricks in lua are very difficult to match in a fully compiled language. But the programmer's poor choice of logical structures doesn't get fixed by a smart compiler that can devote millions or billions of cycles to figuring out what they should have chosen.
8543 posts
Posted 19 October 2012 - 08:00 PM
Certainly not with lua. The compiling is called "interpretation" because it skips much of what has become semi-standard in most compliers, particularly the complex optimizations of code that are a substantial part of why modern compilers take so long to build a binary. Lua interpretation may technically be compiling in the original sense of the term, but it is different from the modern sense and the interpreter doesn't optimize your code.
There are real benefits to this, many of the best tricks in lua are very difficult to match in a fully compiled language. But the programmer's poor choice of logical structures doesn't get fixed by a smart compiler that can devote millions or billions of cycles to figuring out what they should have chosen.
I am well aware of all of this, thank you. I simply misinterpreted your original sentence in relation to "serious code" as relating to things other than Lua, when that was obviously not the case.
2005 posts
Posted 19 October 2012 - 08:58 PM
Oh…well, generally it is true that kernels will be programmed in languages that use advanced compiling for just this reason. But even advanced optimization can be fooled by badly structured code. Computers are still just machines that do as they're told, as evidenced by the fact that they haven't yet arisen and destroyed the human race :P/>/>