Loops: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
>Mindraker
No edit summary
Line 8: Line 8:
At the end of this tutorial, you should:
At the end of this tutorial, you should:


* Know what a <b>for</b> loop does
* Know what a <b>for</b> loop does.
* Know what a <b>while</b> loop does
* Know what a <b>while</b> loop does.
* Be able to use both a <b>for</b> and a <b>while</b> loop in a basic script
* Be able to use both a <b>for</b>, a <b>while</b>, and a <b>repeat</b> loop in a basic script.
 
* Know what a <b>break</b> command does.


== The for loop ==
== The for loop ==
Line 48: Line 48:


The <b>while</b> loop will evaluate the condition to see if it is true or false.  If it is false, the loop will end.  If it is true, the body of the loop will be executed, and the true/false condition will be reevaluated.  <b>It is critically important to have a wait() statement in a while loop, otherwise your program can freeze up.</b>
The <b>while</b> loop will evaluate the condition to see if it is true or false.  If it is false, the loop will end.  If it is true, the body of the loop will be executed, and the true/false condition will be reevaluated.  <b>It is critically important to have a wait() statement in a while loop, otherwise your program can freeze up.</b>


<pre>
<pre>
Line 62: Line 61:
As you can see in the above script, as long as i is less than 10, it will print the statement that i < 10.  Once i has been incremented to a value equal to 10 (namely, 10), the while loop will end, and the final line will print that i=10.
As you can see in the above script, as long as i is less than 10, it will print the statement that i < 10.  Once i has been incremented to a value equal to 10 (namely, 10), the while loop will end, and the final line will print that i=10.


If you have a while or a for loop that otherwise won't end, you can program it to end with the <b>break</b> command:
== The repeat loop ==
 
A repeat... until statement will repeat until a certain condition is met.  The body is executed at least once, because the test is performed after the body. [http://www.lua.org/pil/4.3.3.html]
 
Example:
 
<pre>
local i = 1
repeat
    print(i,"< 10")
    i = i + 1
until i==10
print (i, "=10")
</pre>
 
This will print i < 10 until <b>i</b> has reached the value of 10, at which point it will print that i = 10.
 
== Break ==
 
If you have a while, a for, or a repeat loop that otherwise won't end, you can program it to end with the <b>break</b> command:


<pre>
<pre>
Line 68: Line 86:
print("hi mom!")
print("hi mom!")
wait()
wait()
break -- this forces the loop to end
break -- this forces the endless loop to end
end  
end  
</pre>
</pre>
Line 76: Line 94:
print("hi mom!")
print("hi mom!")
wait()
wait()
break -- this forces the loop to end
break -- this forces the ridiculously long loop to end
end  
end  
</pre>
</pre>


Both of these loops only run once, and print "Hi mom" once.  This is somewhat sloppy programming, however.
These loops only run once because of the <b>break</b> command, and print "Hi mom" once.   
 
<pre>
local i = 1
repeat
    print(i,"< 10")
    i = i - 1
if i == -5 then break end -- this forces the otherwise neverending loop to end
until i==10
print (1, "minus one will always be less than 10")
</pre>
 
Notice in these three loops, something is wrong -- the loops would either take too long to end, or are neverending.  We have to force them to end with <b>break</b>.


== See Also ==
== See Also ==


[http://www.lua.org/pil/4.3.2.html Programming in Lua: While]
[http://www.lua.org/pil/4.3.2.html Programming in Lua: While]
[http://www.lua.org/pil/4.3.3.html Repeat]


[http://www.lua.org/pil/4.3.4.html Programming in Lua: For]
[http://www.lua.org/pil/4.3.4.html Programming in Lua: For]

Revision as of 22:15, 3 October 2008

Introduction

The goal of this tutorial is to introduce you to the for loop and the while loop. It is intended for beginners. Please consult Your first script if you haven't already done so to familiarize yourself with Roblox Studio.

At the end of this tutorial, you should:

  • Know what a for loop does.
  • Know what a while loop does.
  • Be able to use both a for, a while, and a repeat loop in a basic script.
  • Know what a break command does.

The for loop

The for loop is a way of running a command or set of commands a set number of times. For example, if you know you want to print "Hello Mom!" ten times, then you can use the for loop:

for i=1,10 do   	
	print("Hello Mom!")
end

In the for command, you see two numbers: 1, which is the starting value, and 10, which is the ending value. The loop will run from 1 to 10, and print "Hello Mom!" once per each number between 1 and 10 -- 10 times.

Lua will assume you are going to be adding positive numbers. If you want to get fancy, such as subtracting numbers, or adding decimals, you have to specify this as follows:

for i=10,1, -1 do   	
	print(i)
end

Notice that we have specified that we want to count downwards from 10 to 1, and we are subtracting 1 number every time. The output will be 10, 9, 8, 7, 6, 5, 4, 3, 2, 1.

Another example, but with decimals:

for i=1,10, .5 do   	
	print(i)
end

This will count upwards from 1 to 10 by halves.

The while condition

The while loop will evaluate the condition to see if it is true or false. If it is false, the loop will end. If it is true, the body of the loop will be executed, and the true/false condition will be reevaluated. It is critically important to have a wait() statement in a while loop, otherwise your program can freeze up.

local i = 1
    while i < 10 do
    wait()
    print(i,"< 10")
    i = i + 1
    end
print (i,"= 10")

As you can see in the above script, as long as i is less than 10, it will print the statement that i < 10. Once i has been incremented to a value equal to 10 (namely, 10), the while loop will end, and the final line will print that i=10.

The repeat loop

A repeat... until statement will repeat until a certain condition is met. The body is executed at least once, because the test is performed after the body. [1]

Example:

local i = 1
repeat
    print(i,"< 10")
    i = i + 1
until i==10
print (i, "=10")

This will print i < 10 until i has reached the value of 10, at which point it will print that i = 10.

Break

If you have a while, a for, or a repeat loop that otherwise won't end, you can program it to end with the break command:

while true do
print("hi mom!")
wait()
break -- this forces the endless loop to end
end 
for i = 1, 100000000 do
print("hi mom!")
wait()
break -- this forces the ridiculously long loop to end
end 

These loops only run once because of the break command, and print "Hi mom" once.

local i = 1
repeat
    print(i,"< 10")
    i = i - 1
if i == -5 then break end -- this forces the otherwise neverending loop to end
until i==10
print (1, "minus one will always be less than 10")

Notice in these three loops, something is wrong -- the loops would either take too long to end, or are neverending. We have to force them to end with break.

See Also

Programming in Lua: While

Repeat

Programming in Lua: For