Working with loops

This tutorial should be suitable for beginners

To perform the activities in this tutorial, you will need to first follow the “Preparing Teverse” steps here to get set up.

Already an experienced programmer? Check out the Lua docs for a more succinct and technical explanation.

Preface

When programming in any language or situation, often we want to tell the computer to do the same thing multiple times. To do this, we could simply repeat the command - for example if I wanted to print values 1-10 in console:

print("1")
print("2")
print("3")
print("4")
print("5")
print("6")
print("7")
print("8")
print("9")
print("10")

This works. It’s basic and easy to understand, but it’s bad. If you can see that it takes up a lot of space but it’s also known as code which is “inefficient”. This means that it uses many more lines and function calls. To make this more efficient and thus easier to “maintain” - an essential part of programming - we need to use loops.

What are loops?

Loops are, as the name suggests, a way to repeat a set of commands or function calls several times based on various conditions. There are two types: Fixed and conditional.

Fixed (for) loop

A for loop is a loop which iterates (runs) a set number of times.

 for counter = initialValue, endValue, increaseAmount do
      -- Do something
 end

This is where counter is the name of the “counter” variable we want to increase every time, endValue is the number that will cause the loop to stop running. This means that the loop will increase the counter every loop until it is equal to the endValue, when it will move on.
increaseAmount is an optional value. It lets you specify how much the counter changes by every time.

How does this look in real code?

Let’s go back to our previous example. We want to print the numbers 1-10.

-- We don't need to include the increment value, because we want it to increase by 1.
 for counter = 1, 10 do
     print(counter)
 end

-- This is the same as:
for counter = 1, 10, 1 do
     print(counter)
 end

Pretty simple, right?

Try it yourself

Why not try the following tasks yourself? You can see print output in the console by pressing 0 on your keyboard when the Teverse Code sandbox is open and running.

  1. Run the above code so that you have the numbers 1-10 printed in your console.
  2. This time, print the numbers 1-20 and make it print only even numbers!
  3. (Hard) Print the odd numbers 1-20 backwards. So you should start with 19, end with 1!

Conditional loops

For loops are exciting, and so are conditional loops!

While loop

The while loop will iterate (run) until it’s Condition is true

What is a “Condition”?

A condition is a statement that can be “Yes” or “No”. In code terms “Yes” and “no” are represented as a Boolean - a boolean can be true (Yes) or false (no). For example: myName == "neztore" would be true for me as that is correct but false for you as your name is not “neztore”.

While syntax:

while statement do
    -- do something
end

As the while is at the top in this loop, this means that the loop will not run once the condition is false. If the condition is initially false, the loop will not run at all.
An example of an infinite loop:

while true do
   print("I will do this FOREVER!")
  -- You must always include a wait
   wait(0.2)
end

This will run until Teverse is stopped. Note - As this loop runs forver, if you do not add a wait call, this function can crash your application or cause other nasty errors as your computer gets stuck.

Things to do and think about

  1. Implement our counter from earlier with a while loop. Make it count from 1 to 10!

Repeat loop

The repeat loop is very much like a while loop, but the condition check is performed at the end of the loop, so it will run at least once.
Syntax:

repeat
	-- Do something
until condition

Other loops

For loops can also be used as what are known as For ... in loops. Lua has a type called a table. These are used to store values like lists - we’ll go over them in more detail in a later tutorial.
You can use a for… in loop to iterate a table using the pairs function. Example:

for key, value in pairs(table) do
    print(key .. " = " .. value)
end

In Teverse this can also be used to iterate the children of an object and do something to every single one of them, for example:

for _, child in pairs(engine.workspace.children) do
 --muhahah, delete all of workspace!
 child:destroy()
end

That’s the end of the tutorial! If you have any questions or suggestions for our next tutorial, feel free to let us know in our Discord!

3 Likes

Thank you for providing the community with this wonderful tutorial. I’m currently in the process of learning how to script so this helps a lot. Gotta love the dev team! :smiley:

2 Likes

Thanks for sharing this. I might have to think of a way to implement this in my little TevInvest script I am mucking around with some how.

Can’t wait for a tutorial on HTTP Service. Keep up the great work, I appreciate it.