PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience

Roblox Coding Basics – Loops

Roblox Coding Basics – Loops

Jul 03 2018, 10:41 AM PST 5 min

A loop is a chunk of code that is executed multiple times. There are three types of loops: for, while, and repeat. Each type loops a block of code, but in different ways.

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

ForFlowchart.jpg

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.

In this example 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).

for i = 1, 5 do   	
    print("Hello Mom!")
end
Output:
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!
Hello Mom!

Counting Down

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
Output:
10
9
8
7
6
5
4
3
2
1

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

Counting by Decimals

Here’s another example that counts up using decimals:

for i=1, 10, 0.5 do   	
    print(i)
end
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

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.

Whileflowchart.JPG

Example

local i = 1
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.

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”).

Repeatloop.JPG

Example

local i = 1
repeat
    print(i,"< 10")
    i = i + 1
until i==10
print (i, "= 10")
Output:
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.

Breaking Loops

If you have a while, for, or repeat loop that otherwise won’t end, you can program it to end with the break command so that you can continue with the next part of code:

Examples

while wait() do
    print("Hi mom!")
    break  -- This forces the endless loop to end
end
for i = 1, 1000000000 do
    print("Hi mom!")
    wait()
    break  -- This forces the ridiculously long loop to end
end

Both of these loops only run once because of the break command, and print “Hi mom!” once.

See Also