Welcome to Numeric For-Loops in Lua

Welcome! In this lesson, you will begin your journey into one of the most useful tools in programming: loops. Specifically, we will focus on the numeric "for" loop in Lua. If you are new to programming, don't worry — this is a great place to start. If you have some experience, consider this a solid foundation for more advanced looping techniques you'll see later in the course.

What You'll Learn

A numeric "for" loop is a way to repeat actions a certain number of times. This is perfect for tasks like counting, repeating steps, or creating simple schedules. In Lua, the numeric "for" loop is very flexible and easy to use.

Note: Lua’s numeric for loop includes both the start and stop values (unlike some other languages). For example, for i = 1, 5 do runs 5 times, including when i is 5.

Here is a simple example:

In this code, the loop starts at 1 and ends at 5. For each value of day, it prints a message. This is a great way to automate repetitive tasks.

You can also count down using a numeric "for" loop by adding a step value:

Here, the loop counts down from 3 to 1, printing each number. The -1 tells Lua to decrease the value each time.

Why This Is Important

Loops help you avoid writing the same code over and over. They make your programs shorter, easier to read, and less likely to have mistakes. Numeric "for" loops are especially useful for counting, repeating actions, and working with sequences. Once you master this, you will be able to solve many real-world problems more efficiently.

Ready to see how loops can make your code smarter and your life easier? Let’s get started with some practice!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal