Loops

From ComputerCraft Wiki
Revision as of 13:28, 1 December 2012 by AfterLifeLochie (Talk | contribs) (Ported to to Tutorial template)

Jump to: navigation, search
Grid workbench.png  Tutorial: Loops
This is a tutorial page for loops and their application.
Objective To understand the purposes, and uses of, loops and iterators
Prerequisites A basic understanding of Variables and a basic understanding of Functions.


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 those 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 no longer 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 computer's 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