Thanks, help will be much appricated. :)/>
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Loop thats user defined
Started by Falco, 14 December 2012 - 06:34 PMPosted 14 December 2012 - 07:34 PM
Basicly i need some help with a script that will only loop a certain amount of times via user input, im struggling to find any help on the internet, or maybe its my poor searching abilty.
Thanks, help will be much appricated. :)/>
Thanks, help will be much appricated. :)/>
Posted 14 December 2012 - 07:38 PM
do something like the following
times = read()
for i = 1, tonumber(times) do
-- do stuff
end
Posted 14 December 2012 - 07:41 PM
do something like the followingtimes = read() for i = 1, tonumber(times) do -- do stuff end
I'm very very new to programing in computercraft, would you mind explaining how i would use this in my programs? pretty please…
Posted 14 December 2012 - 08:01 PM
do something like the followingtimes = read() for i = 1, tonumber(times) do -- do stuff end
I'm very very new to programing in computercraft, would you mind explaining how i would use this in my programs? pretty please…
Ok so for starters we are declaring a new variable called "times".
We then are calling the read() function. This function is a system function that reads the input from the terminal and only continues the program after enter/return is pressed.
Next we have a for loop. We declare the variable "i" as the number for the loop, and assign it to 1. the comma is similar to the ; in other programming languages. we then call another system function called tonumber() which converts the string the user has entered to a number. this specifies the maximum for our for loop. end specifies where the loop will go back to the start.
for loops can also have another comma and a 3rd number, this number is the increment amount. for example
for i = 0, 6 do would mean it loops from 0 to 6 in the default increment of 1
for i = 0, 6, 2 do would loop from 0 to 6 in increments of 2
for i = 6, 0, -1 do would loop from 6 to 0 in decrements of 1
So in terms of your program you would have something like this
-- Code up to the point of where you want user input
times = read() -- the program will pause here until the user hits enter
for i = 1, tonumber(times) do -- here say if the user enters 5 if will loop from 1 to 5
-- what you want the script to do
end -- end of loop
-- continue program if required
hope this helps :)/>
EDIT: I feel i should also point out that everything after – in lua is ignored as – tells the compiler its a comment
using –[[ will ignore all code until it finds ]]
Posted 14 December 2012 - 08:03 PM
Can OP please post some more info on what he would like to accomplish?
Posted 14 December 2012 - 08:07 PM
Wdo something like the followingtimes = read() for i = 1, tonumber(times) do -- do stuff end
I'm very very new to programing in computercraft, would you mind explaining how i would use this in my programs? pretty please…
Ok so for starters we are declaring a new variable called "times".
We then are calling the read() function. This function is a system function that reads the input from the terminal and only continues the program after enter/return is pressed.
Next we have a for loop. We declare the variable "i" as the number for the loop, and assign it to 1. the comma is similar to the ; in other programming languages. we then call another system function called tonumber() which converts the string the user has entered to a number. this specifies the maximum for our for loop. end specifies where the loop will go back to the start.
for loops can also have another comma and a 3rd number, this number is the increment amount. for example
for i = 0, 6 do would mean it loops from 0 to 6 in the default increment of 1
for i = 0, 6, 2 do would loop from 0 to 6 in increments of 2
for i = 6, 0, -1 do would loop from 6 to 0 in decrements of 1
So in terms of your program you would have something like this-- Code up to the point of where you want user input times = read() -- the program will pause here until the user hits enter for i = 1, tonumber(times) do -- here say if the user enters 5 if will loop from 1 to 5 -- what you want the script to do end -- end of loop -- continue program if required
hope this helps :)/>
EDIT: I feel i should also point out that everything after – in lua is ignored as – tells the compiler its a comment
using –[[ will ignore all code until it finds ]]
Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head… YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?
Can OP please post some more info on what he would like to accomplish?
Thanks for attempting to help me, but help has been found. :)/>
Posted 14 December 2012 - 08:11 PM
Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head… YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?
Haha, no problems, anytime, seriously, just PM me (PM through profiles).
Yes there is a way to give rep. As to how, that is something I don't know how to do. I know that there is the "Vote this post up" green arrow. And I think there may be a "star rating" thingy on profiles (Click the persons photo).
Posted 15 December 2012 - 01:41 AM
Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head… YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?
Haha, no problems, anytime, seriously, just PM me (PM through profiles).
Yes there is a way to give rep. As to how, that is something I don't know how to do. I know that there is the "Vote this post up" green arrow. And I think there may be a "star rating" thingy on profiles (Click the persons photo).
Thanks, I'll will try my hardest to give you REP of some sort, you are a fine addition to this very helpful community. :)/>
Posted 15 December 2012 - 01:50 AM
Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head… YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?
Haha, no problems, anytime, seriously, just PM me (PM through profiles).
Yes there is a way to give rep. As to how, that is something I don't know how to do. I know that there is the "Vote this post up" green arrow. And I think there may be a "star rating" thingy on profiles (Click the persons photo).
Thanks, I'll will try my hardest to give you REP of some sort, you are a fine addition to this very helpful community. :)/>
Why thank you kind sir or madam :)/>
Posted 15 December 2012 - 03:29 AM
Wow thank you very much for this information, I'll be sure to read it untill it sticks in my head… YOU ARE A GOD!!!
No seriously thank you very much. Is there anyway I can give rep or anything on this forum?
Haha, no problems, anytime, seriously, just PM me (PM through profiles).
Yes there is a way to give rep. As to how, that is something I don't know how to do. I know that there is the "Vote this post up" green arrow. And I think there may be a "star rating" thingy on profiles (Click the persons photo).
Thanks, I'll will try my hardest to give you REP of some sort, you are a fine addition to this very helpful community. :)/>
Why thank you kind sir or madam :)/>
Sir :)/> :P/>