<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.starfall.wtf/index.php?action=history&amp;feed=atom&amp;title=Loops</id>
	<title>Loops - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.starfall.wtf/index.php?action=history&amp;feed=atom&amp;title=Loops"/>
	<link rel="alternate" type="text/html" href="https://wiki.starfall.wtf/index.php?title=Loops&amp;action=history"/>
	<updated>2026-07-28T17:09:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.3</generator>
	<entry>
		<id>https://wiki.starfall.wtf/index.php?title=Loops&amp;diff=469&amp;oldid=prev</id>
		<title>PRG: Created page with &quot;{{ImproperArchive}} __TOC__ {{CatUp|Tutorials}}  == Introduction ==  The goal of this tutorial is to introduce you to the &lt;b&gt;for&lt;/b&gt; loop and the &lt;b&gt;while&lt;/b&gt; loop.  It is intended for beginners.  Please consult Your first script if you haven&#039;t already done so to familiarize yourself with Roblox Studio.  At the end of this tutorial, you should:  * Know what a &lt;b&gt;for&lt;/b&gt; loop does. * Know what a &lt;b&gt;while&lt;/b&gt; loop does. * Be able to use both a &lt;b&gt;for&lt;/b&gt;, a &lt;b&gt;whil...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.starfall.wtf/index.php?title=Loops&amp;diff=469&amp;oldid=prev"/>
		<updated>2024-12-10T22:07:28Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;{{ImproperArchive}} __TOC__ {{CatUp|Tutorials}}  == Introduction ==  The goal of this tutorial is to introduce you to the &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop and the &amp;lt;b&amp;gt;while&amp;lt;/b&amp;gt; loop.  It is intended for beginners.  Please consult &lt;a href=&quot;/index.php/Your_first_script&quot; title=&quot;Your first script&quot;&gt;Your first script&lt;/a&gt; if you haven&amp;#039;t already done so to familiarize yourself with &lt;a href=&quot;/index.php/Roblox_Studio&quot; class=&quot;mw-redirect&quot; title=&quot;Roblox Studio&quot;&gt;Roblox Studio&lt;/a&gt;.  At the end of this tutorial, you should:  * Know what a &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop does. * Know what a &amp;lt;b&amp;gt;while&amp;lt;/b&amp;gt; loop does. * Be able to use both a &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt;, a &amp;lt;b&amp;gt;whil...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{ImproperArchive}}&lt;br /&gt;
__TOC__&lt;br /&gt;
{{CatUp|Tutorials}}&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The goal of this tutorial is to introduce you to the &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop and the &amp;lt;b&amp;gt;while&amp;lt;/b&amp;gt; loop.  It is intended for beginners.  Please consult [[Your first script]] if you haven&amp;#039;t already done so to familiarize yourself with [[Roblox Studio]].&lt;br /&gt;
&lt;br /&gt;
At the end of this tutorial, you should:&lt;br /&gt;
&lt;br /&gt;
* Know what a &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop does.&lt;br /&gt;
* Know what a &amp;lt;b&amp;gt;while&amp;lt;/b&amp;gt; loop does.&lt;br /&gt;
* Be able to use both a &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt;, a &amp;lt;b&amp;gt;while&amp;lt;/b&amp;gt;, and a &amp;lt;b&amp;gt;repeat&amp;lt;/b&amp;gt; loop in a basic script.&lt;br /&gt;
* Know what a &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; command does.&lt;br /&gt;
&lt;br /&gt;
== The for loop ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop is a way of running a command or set of commands a set number of times.&lt;br /&gt;
&lt;br /&gt;
[[Image:Forflowchart.JPG]]&lt;br /&gt;
&lt;br /&gt;
For example, if you know you want to print &amp;quot;Hello Mom!&amp;quot; ten times, then you can use the &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; loop.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i=1,10 do   	&lt;br /&gt;
	print(&amp;quot;Hello Mom!&amp;quot;)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the &amp;lt;b&amp;gt;for&amp;lt;/b&amp;gt; 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 &amp;quot;Hello Mom!&amp;quot; once per each number between 1 and 10 -- 10 times.&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i=10,1, -1 do   	&lt;br /&gt;
	print(i)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Another example, but with decimals:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i=1,10, .5 do   	&lt;br /&gt;
	print(i)&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will count upwards from 1 to 10 by halves.&lt;br /&gt;
&lt;br /&gt;
== The while condition ==&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;b&amp;gt;while&amp;lt;/b&amp;gt; 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.  &amp;lt;b&amp;gt;It is critically important to have a wait() statement in a while loop, otherwise your program can freeze up.&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[Image:Whileflowchart.JPG]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local i = 1&lt;br /&gt;
    while i &amp;lt; 10 do&lt;br /&gt;
    wait()&lt;br /&gt;
    print(i,&amp;quot;&amp;lt; 10&amp;quot;)&lt;br /&gt;
    i = i + 1&lt;br /&gt;
    end&lt;br /&gt;
print (i,&amp;quot;= 10&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As you can see in the above script, as long as i is less than 10, it will print the statement that i &amp;lt; 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.&lt;br /&gt;
&lt;br /&gt;
== The repeat loop ==&lt;br /&gt;
&lt;br /&gt;
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 (i.e., &amp;quot;the process is preceding the decision&amp;quot;). [http://www.lua.org/pil/4.3.3.html]&lt;br /&gt;
&lt;br /&gt;
[[Image:Repeatloop.JPG]]&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local i = 1&lt;br /&gt;
repeat&lt;br /&gt;
    print(i,&amp;quot;&amp;lt; 10&amp;quot;)&lt;br /&gt;
    i = i + 1&lt;br /&gt;
until i==10&lt;br /&gt;
print (i, &amp;quot;=10&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will print i &amp;lt; 10 until &amp;lt;b&amp;gt;i&amp;lt;/b&amp;gt; has reached the value of 10, at which point it will print that i = 10.&lt;br /&gt;
&lt;br /&gt;
== Break ==&lt;br /&gt;
&lt;br /&gt;
If you have a while, a for, or a repeat loop that otherwise won&amp;#039;t end, you can program it to end with the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; command:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
while true do&lt;br /&gt;
print(&amp;quot;hi mom!&amp;quot;)&lt;br /&gt;
wait()&lt;br /&gt;
break -- this forces the endless loop to end&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
for i = 1, 100000000 do&lt;br /&gt;
print(&amp;quot;hi mom!&amp;quot;)&lt;br /&gt;
wait()&lt;br /&gt;
break -- this forces the ridiculously long loop to end&lt;br /&gt;
end &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
These loops only run once because of the &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt; command, and print &amp;quot;Hi mom&amp;quot; once.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
local i = 1&lt;br /&gt;
repeat&lt;br /&gt;
    print(i,&amp;quot;&amp;lt; 10&amp;quot;)&lt;br /&gt;
    i = i - 1&lt;br /&gt;
if i == -5 then break end -- this forces the otherwise neverending loop to end&lt;br /&gt;
until i==10&lt;br /&gt;
print (1, &amp;quot;minus one will always be less than 10&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 &amp;lt;b&amp;gt;break&amp;lt;/b&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== See Also ==&lt;br /&gt;
&lt;br /&gt;
[http://www.lua.org/pil/4.3.2.html Programming in Lua: While]&lt;br /&gt;
&lt;br /&gt;
[http://www.lua.org/pil/4.3.3.html Repeat]&lt;br /&gt;
&lt;br /&gt;
[http://www.lua.org/pil/4.3.4.html Programming in Lua: For]&lt;br /&gt;
&lt;br /&gt;
[[Category:Scripting Tutorials]]&lt;/div&gt;</summary>
		<author><name>PRG</name></author>
	</entry>
</feed>