Loops: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Mindraker
m Text replacement - "</SyntaxHighlight>" to "</syntaxhighlight>"
 
(46 intermediate revisions by 13 users not shown)
Line 1: Line 1:
__TOC__
=Loops=
{{CatUp|Tutorials}}
===What is a Loop?===


== Introduction ==
A loop is a chunk of code that is executed many times over.  There are three types of loops, for, while, and repeat.  Each one loops a block of code, but in different ways.  You will see the differences between each of the loops later on.  The reason why one would use a loop is that loops are great ways to not repeat the same code over and over.  Let's dive in!
{{EmphasisBox|Avoid using infinite loops without any waiting. If you don't, Roblox will probably freeze.|red|dark=yes}}


The goal of this tutorial is to introduce you to the <b>for</b> loop and the <b>while</b> loopIt is intended for beginnersPlease consult [[Your first script]] if you haven't already done so to familiarize yourself with [[Roblox Studio]].
==While==
The '''while''' loop will evaluate the condition to see if it is true or falseIf it is false, the loop will endIf it is true, the body of the loop after the 'do' statement will be executed, and the true/false condition will be reevaluated afterward.


At the end of this tutorial, you should:
[[Image:Whileflowchart.JPG]]


* Know what a <b>for</b> loop does
{{Example|{{code and output|code =
* Know what a <b>while</b> loop does
local i = 1
* Be able to use both a <b>for</b> and a <b>while</b> loop in a basic script
while i < 10 do
  print(i.." < 10")
  i = i + 1
end
print (i.." = 10")
|output =
1 < 10
2 < 10
3 < 10
4 < 10
5 < 10
6 < 10
7 < 10
8 < 10
9 < 10
10 = 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 for loop ==
==For==


The <b>for</b> 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 <b>for</b> loop:
The '''for''' loop is a way of running a command or set of commands a set number of times. The basic syntax is as following:


<pre>
'''for''' ''iterator_variable'' = ''start value, end value, increment'' '''do'''
for i=1,10 do 
 
print("Hello Mom!")
However, there is also a more complicated style of the '''for''' loop called the [[Generic for|Generic For Loop]], but for now we will just discuss the basics.
 
Keep in mind that as long as the iterator is between the start and end values, the code following the 'do' statement and before the 'end' will be executed.
 
[[Image:FlowchatForLoops.png]]
 
For example, if you know you want to print "Hello Mom!" ten times, then you can use the for loop.
 
{{Example|{{code and output|code =
for i=1, 10 do 
  print("Hello Mom!")
end
end
</pre>
|output =
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!
}}}}


In the <b>for</b> 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.
In the '''for''' loop, 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:
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:


<pre>
{{Example|{{code and output|code =
for i=10,1, -1 do 
for i=10,1, -1 do 
print(i)
  print(i)
end
end
</pre>
|output =
10
9
8
7
6
5
4
3
2
1
}}}}


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.
Notice that we have specified that we want to count downwards from 10 to 1, and we are subtracting 1 number every time.


Another example, but with decimals:
Another example, but with decimals:


<pre>
{{Example|{{code and output|code =
for i=1,10, .5 do 
for i=1,10, .5 do 
print(i)
  print(i)
end
end
</pre>
|output =
1
1.5
2
2.5
3
3.5
4
4.5
5
5.5
6
6.5
7
7.5
8
8.5
9
9.5
10
}}}}


This will count upwards from 1 to 10 by halves.
This will count upwards from 1 to 10 by halves.


== The while condition ==
==Repeat==


The <b>while</b> loop will evaluate the condition to see if it is true or falseIf 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>
A repeat ... until statement will repeat until a certain condition is metThe body is executed at least once, because the test is performed after the body (i.e., "the process is preceding the decision"). [http://www.lua.org/pil/4.3.3.html Repeat]


[[Image:Repeatloop.JPG]]


<pre>
{{code and output|code=
local i = 1
local i = 1
    while i < 10 do
repeat
    wait()
  print(i,"< 10")
    print(i,"< 10")
  i = i + 1
    i = i + 1
until i==10
    end
print (i, "=10")
print (i,"= 10")
|output=
</pre>
1 < 10
2 < 10
3 < 10
4 < 10
5 < 10
6 < 10
7 < 10
8 < 10
9 < 10
10 =10
}}
 
This will print i < 10 until '''i''' has reached the value of 10, at which point it 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.
==Break==


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:
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, so you can continue with the next part of code:


<pre>
{{Example|<syntaxhighlight lang="lua">
while true do
while wait() do
print("hi mom!")
  print("hi mom!")
wait()
  break -- this forces the endless loop to end
break -- this forces the loop to end
end  
end  
</pre>


<pre>
for i = 1, math.huge do -- math.huge is infinite or we can use the number 1000000000
for i = 1, 100000000 do
  print("hi mom!")
print("hi mom!")
  wait()
wait()
  break -- this forces the ridiculously long loop to end
break -- this forces the loop to end
end
end  
</syntaxhighlight>}}
</pre>
 
 
These loops only run once because of the '''break''' command, and print "Hi mom" once. 
 
{{Example|<syntaxhighlight lang="lua">
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")
</syntaxhighlight>}}


Both of these loops only run once, and print "Hi mom" onceThis is somewhat sloppy programming, however.
Notice in these three loops, something is wrong -- the loops would either take too long to end, or are neverendingWe have to force them to end with '''break'''.


== 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 Programming in Lua: 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]
[[Category:Scripting Tutorials]]

Latest revision as of 06:02, 27 April 2023

Loops

What is a Loop?

A loop is a chunk of code that is executed many times over. There are three types of loops, for, while, and repeat. Each one loops a block of code, but in different ways. You will see the differences between each of the loops later on. The reason why one would use a loop is that loops are great ways to not repeat the same code over and over. Let's dive in!

Avoid using infinite loops without any waiting. If you don't, Roblox will probably freeze.

While

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 after the 'do' statement will be executed, and the true/false condition will be reevaluated afterward.

Example
local i = 1
while i < 10 do
   print(i.." < 10")
   i = i + 1
end
print (i.." = 10")

1 < 10 2 < 10 3 < 10 4 < 10 5 < 10 6 < 10 7 < 10 8 < 10 9 < 10

10 = 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.

For

The for loop is a way of running a command or set of commands a set number of times. The basic syntax is as following:

for iterator_variable = start value, end value, increment do

However, there is also a more complicated style of the for loop called the Generic For Loop, but for now we will just discuss the basics.

Keep in mind that as long as the iterator is between the start and end values, the code following the 'do' statement and before the 'end' will be executed.

For example, if you know you want to print "Hello Mom!" ten times, then you can use the for loop.

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

Hello Mom! Hello Mom! Hello Mom! Hello Mom! Hello Mom! Hello Mom! Hello Mom! Hello Mom! Hello Mom!

Hello Mom!


In the for loop, 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:

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

10 9 8 7 6 5 4 3 2

1


Notice that we have specified that we want to count downwards from 10 to 1, and we are subtracting 1 number every time.

Another example, but with decimals:

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

1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8 8.5 9 9.5

10


This will count upwards from 1 to 10 by halves.

Repeat

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., "the process is preceding the decision"). Repeat

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

1 < 10 2 < 10 3 < 10 4 < 10 5 < 10 6 < 10 7 < 10 8 < 10 9 < 10

10 =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, so you can continue with the next part of code:

Example
while wait() do
   print("hi mom!")
   break -- this forces the endless loop to end
end 

for i = 1, math.huge do -- math.huge is infinite or we can use the number 1000000000
   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.

Example
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

Programming in Lua: Repeat

Programming in Lua: For