Difference between revisions of "Loops"
m (added bunch of links) |
|||
Line 7: | Line 7: | ||
Here is a script without loops | Here is a script without loops | ||
− | local | + | local function GetName() |
− | + | write("Enter a name: ") | |
− | return | + | return read() |
end | end | ||
Line 18: | Line 18: | ||
local Name5 = GetName() | local Name5 = GetName() | ||
− | + | write("You have entered "..Name1.." "..Name2.." "..Name3.." "..Name4.." "..Name5) | |
As you can see, there is quite a lot of repetition, even when we're using a function. | As you can see, there is quite a lot of repetition, even when we're using a function. | ||
Line 32: | Line 32: | ||
The best way to discover how the for loop works is to try it yourself. Try changing the numbers the following code to see how it reacts. | The best way to discover how the for loop works is to try it yourself. Try changing the numbers the following code to see how it reacts. | ||
for i=1,10 do | for i=1,10 do | ||
− | + | print( "i is "..tostring(i) ) | |
end | end | ||
Notice how it always counts up until Variable is the same or greater than End. Start, End, and Interval can also be variables, so it can loop a different number of times | Notice how it always counts up until Variable is the same or greater than End. Start, End, and Interval can also be variables, so it can loop a different number of times | ||
− | + | write("How many times should I loop? ") | |
local Num = tostring( read() ) | local Num = tostring( read() ) | ||
for i=1,Num do | for i=1,Num do | ||
− | + | print("Looped "..tostring(i).." time(s).") | |
end | end | ||
Line 45: | Line 45: | ||
local string = "" | local string = "" | ||
for i=1,5 do | for i=1,5 do | ||
− | + | write("Enter a name: ") | |
string=string.." "..read() | string=string.." "..read() | ||
end | end | ||
− | + | write("You have entered "..string) | |
== The While loop == | == The While loop == | ||
Line 55: | Line 55: | ||
local Answer, Correct = "", "2" | local Answer, Correct = "", "2" | ||
while Answer~=Correct do | while Answer~=Correct do | ||
− | + | write("What is the sum of 1 and 1 (1+1)? ") | |
− | Answer = | + | Answer = read() |
if Answer == Correct then print("Correct!") else print("Incorrect!") end | if Answer == Correct then print("Correct!") else print("Incorrect!") end | ||
Line 62: | Line 62: | ||
This type of loop is useful for if you don't want to specify a definite end for the loop. Although infinite loops are ill-advised, you may want to use one at some point. To terminate a program stuck in a loop, hold Ctrl + T while in the computers interface. | This type of loop is useful for if you don't want to specify a definite end for the loop. Although infinite loops are ill-advised, you may want to use one at some point. To terminate a program stuck in a loop, hold Ctrl + T while in the computers interface. | ||
− | while | + | while true do --Always loop |
--Code here | --Code here | ||
end | end | ||
Line 73: | Line 73: | ||
local i = 1 | local i = 1 | ||
repeat | repeat | ||
− | + | print(i) | |
i = i + 1 | i = i + 1 | ||
until i == 5''' | until i == 5''' |
Revision as of 21:54, 13 July 2012
This is a tutorial page for loops and their application.
Introducing Loops
At some point while coding, you will most likely want to run some code multiple times, without writing out a lot of code. While it is true that functions can shorten your code, there may be situations where even thouse don't simplify it enough. That's where loops come in.
Here is a script without loops
local function GetName() write("Enter a name: ") return read() end local Name1 = GetName() local Name2 = GetName() local Name3 = GetName() local Name4 = GetName() local Name5 = GetName() write("You have entered "..Name1.." "..Name2.." "..Name3.." "..Name4.." "..Name5)
As you can see, there is quite a lot of repetition, even when we're using a function.
The For loop
The for loop is the most basic loop. It is in the following format
for Variable = Start, End, Interval do --Insert code here end
Variable is a number that will track where we are in the loop. It is usually called "i", or something more appropriate to the script. Start is a number to indicate the start point for the loop. This is generally 1, so the loop can count up easily. End is the number the loop will count to. Interval is an optional argument, and will specify how big the jumps would be . Ie, it specifies how much to add or subtract from Variable every loop.
The best way to discover how the for loop works is to try it yourself. Try changing the numbers the following code to see how it reacts.
for i=1,10 do print( "i is "..tostring(i) ) end
Notice how it always counts up until Variable is the same or greater than End. Start, End, and Interval can also be variables, so it can loop a different number of times
write("How many times should I loop? ") local Num = tostring( read() ) for i=1,Num do print("Looped "..tostring(i).." time(s).") end
To simplify the first script using a for loop, it would look something like this:
local string = "" for i=1,5 do write("Enter a name: ") string=string.." "..read() end write("You have entered "..string)
The While loop
The While loop will continue looping until a certain condition is met.
local Answer, Correct = "", "2" while Answer~=Correct do write("What is the sum of 1 and 1 (1+1)? ") Answer = read() if Answer == Correct then print("Correct!") else print("Incorrect!") end end
This type of loop is useful for if you don't want to specify a definite end for the loop. Although infinite loops are ill-advised, you may want to use one at some point. To terminate a program stuck in a loop, hold Ctrl + T while in the computers interface.
while true do --Always loop --Code here end
The Repeat loop
A very simple loop to repeat something a stated number of times. The first line declares a local variable and sets its value to 1 the repeat loop will print i until i = 5. Other conditions can also be used. unlike the while loop the repeat loop will always be executed once.
local i = 1 repeat print(i) i = i + 1 until i == 5