# Understanding Loops In Lua

In this article, we will learn different types of Lua loops, their syntax, and how they can be used in various scenarios with examples.

In computer programming, a loop involves repeatedly executing a piece of code until a specific condition is met.

Loops automate repetitive tasks and iterate over a sequence of values or elements without having to write the same code over and over again.

## Lua Loops

In Lua, there are several types of loops that provide different ways to iterate over a collection of values or execute a block of code for a specific number of times.

1. While Loop
2. Repeat-Until Loop
3. Numeric for loop
4. Generic for loop

We will examine each loop in detail.

## While Loop In Lua

This type of loop executes a block of code repeatedly while a certain condition is true.

### Syntax

The syntax of a while loop in Lua is:

```while condition do
-- block of code
end```

The following example prints the numbers from 1 to 10 using a while loop in Lua:

#### Example:

local x = 1while x <= 10 do print('The value of x is',x) x = x + 1 end

## Repeat-Until Loop

This type of Lua loop is similar to a while loop, but the condition is checked at the end of the loop, so the block of code will execute at least once.

### Syntax

```repeat
-- block of code
until condition```

In the below example, numbers from 1 to 10 are printed using a repeat until loop in Lua:

#### Example:

local x = 1repeat print('The value of x is',x) x = x + 1 until x > 10

## For loop

This type of loop executes a block of code a fixed number of times.

There are two ways to use the for loop in Lua:

• The numeric for loop.
• The generic for loop.

## Numeric for loop

This type of Lua loop is a variant of the for loop, used specifically for iterating over a sequence of numbers.

### Syntax

```for var = start_value, end_value, step_value do
-- block of code
end```

Following example showcases the implementation of the for loop in Lua:

#### Example:

for x = 1, 10 do print('The value of x is',x) end

## Generic for loop

This type of Lua loop is used to iterate over a sequence of values, such as elements in a table.

### Syntax

```for key, value in ipairs(table_name) do
-- block of code
end```

This example implements the generic for loop in Lua:

#### Example:

local table_name= {10,20,30,40,50}for key, value in ipairs(table_name) do print(value,'is on index #',key) end

## Loop Control Statement

A programming language provides built-in loop control statements that allow you to control the execution of loops.

The purpose of these statements is to modify the behavior of loops in some way, for example, to exit a loop prematurely or jump to a certain point in the loop’s code.

In Lua, the two built-in loop control statements are break and goto which are explained below:

### Break Statement In Lua

Break statement is used to exit loops immediately.

The loop is terminated early when it is executed, skipping the remaining iterations.

Below example implements the break statement in a for loop:

#### Example:

for i = 1, 10 do if i == 5 then break end print(i) end

### Goto Statement

A goto statement transfers program control to a specific point in the code.

It allows us to create loops, implement complex flow control, or jump out of loops prematurely.

The goto statement is demonstrated in the example below:

#### Example:

local i = 1::loop:: if i <= 10 then print(i) i = i + 1 goto loop end

## Infinite loops In Lua

In computer programming, an infinite loop can be described as a loop that continues to execute indefinitely without ever terminating.

In other words, the loop will repeat the same code block indefinitely.

Programmers often use infinite loops when they need to continuously perform a task until some condition is met.

A break statement can be used to exit an infinite loop.

The following example illustrates the use of infinite loop in Lua:

#### Example:

print("1. Enter a number to get its square.") print("2. To exit this program enter exit.") while true do io.write("Enter a number: ") user_input = io.read() if user_input == "quit" then break else number = tonumber(user_input) if number then print("The square of", number, "is", number^2) else print("Invalid input") end end end
+1
1
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0