Repeating Tasks with For Loops

10 min

Repeating Tasks with For Loops

Robot looking at production line

Learning Objectives Students will be able to:
  • Be able to recognize the formula for creating a for loop and understand each component, including the keyword, control variable, and three values.
  • Create a for loop that counts down.
  • Explain how a for loop is different than a while true do loop.
Prerequisites Students should:

There are different ways to make code run over and over. If you want the code to only run a certain amount of times, use a for loop. A good example, would be when creating countdown timers where the clock ticks down one second at a time. If you’re unsure of how long the code should repeat, or if you want it to repeat forever, that can be done with while loops as covered in Loops and BrickColor.

How For Loops Work

For loops use three values to control how many times they run: a control variable, an end value, and an increment value. Starting from the value of the control variable, the for loops will either count up or down each time it runs code inside the loop until it passes the end value. Positive increment values count up, negative increment values count down.

Steps in a For Loop

First, the for loop compares the control variable with the end value.

After running the code, the increment value is added to the control variable.

The loop then checks the control variable and starts over.

Once the control variable passes the end value, the loop will stop.

For example, if a loop has an end value of 10, once the control variable has passed 10, the for loop will stop.

Code a Practice For Loop

To see how a for loop works, use these steps to code a for loop that starts at 10 and counts down to 0, one number at a time. Every time the loop runs, it’ll print the current value inside the control variable.

  1. In ServerScriptService, create a new script named PracticeLoop.
  2. To begin a for loop, type the keyword for.
Lua Keywords

Keywords are words with specific purposes in code that can’t be used for anything but that purpose. For example, typing while will start creating a while loop. Keywords cannot be used as variable names.

  1. Create a control variable named count and set a starting value of 10.
Naming Control Variables

A control variable can be any acceptable variable name. Like other variable names, a control variable name should be clear and descriptive to what the for loop is doing.

  1. Set the end value to 0, by typing , 0. Make sure to include a comma to separate the values.
  1. Create an increment value of -1 by typing , -1. After the loop has finished its action, it’ll add the increment value to the control variable, count. Because the increment is negative, it’ll be subtracted when added to the control variable.
Increments can be Optional

A for loop doesn’t need an increment. Without a given increment, the for loop will by default add 1 after each loop. Because there isn’t a third number, you only need a comma to separate the control variable and end value, like in this loop: for count = 10, 0

  1. To finish the for loop, type do and press Enter to add end. Any code typed between do and end will run each time the loop repeats.
  1. Now, to create a second-by-second countdown, just print the value of count and use the wait function to create a timer.
Printing Variables

Because you’re printing a variable and not a string, you don’t need to include “quotation marks”.

  1. Run the project and watch the Output Window to see the for loop.
The loop will print out the current value of count each time it goes through an iteration. An iteration is the complete process of checking the control value, running code, and updating the increment value. Because the control variable starts at 0 and has to go pass 10, the loop will go through 11 iterations before stopping.
  • Check that you have two commas separating the numbers in your for loop. Having extra or missing commas will make the loop not start.
  • If the for loop prints all at once, make sure that there is a wait function that uses at least 1 second.

Different For Loop Examples

Changing the values of the control variable, end, and increment will change how the loop functions. The for loop you just wrote could instead count up to 10, or count up in odd numbers. Below are different examples of for loops with different start, end, and increment values.

Counting Up By One

Counting Up in Even Numbers

If For Loops Don't Run At All

If the control variable starts out beyond the end value, like in the example below, the for loop won’t run at all. In this case, the for loop is counting up and checking if count is greater than 0. When the for loop does it’s first check, it sees that 10 is greater than 0, and so it’ll stop the loop without printing anything.