Control structures are fundamental in any programming language as they allow you to control the flow of your program. In Lua, control structures include conditional statements and loops. This section will cover the following topics:

  1. Conditional Statements
    • if statements
    • if-else statements
    • elseif statements
    • Nested if statements
  2. Loops
    • while loops
    • repeat-until loops
    • for loops

Conditional Statements

Conditional statements allow you to execute certain pieces of code based on whether a condition is true or false.

if Statements

The if statement is the simplest form of conditional statement. It executes a block of code if a specified condition is true.

Syntax:

if condition then
    -- code to execute if condition is true
end

Example:

local age = 18

if age >= 18 then
    print("You are an adult.")
end

if-else Statements

The if-else statement allows you to execute one block of code if the condition is true and another block if the condition is false.

Syntax:

if condition then
    -- code to execute if condition is true
else
    -- code to execute if condition is false
end

Example:

local age = 16

if age >= 18 then
    print("You are an adult.")
else
    print("You are a minor.")
end

elseif Statements

The elseif statement allows you to check multiple conditions in sequence. If one condition is true, its corresponding block of code is executed, and the rest are skipped.

Syntax:

if condition1 then
    -- code to execute if condition1 is true
elseif condition2 then
    -- code to execute if condition2 is true
else
    -- code to execute if none of the conditions are true
end

Example:

local score = 85

if score >= 90 then
    print("Grade: A")
elseif score >= 80 then
    print("Grade: B")
elseif score >= 70 then
    print("Grade: C")
else
    print("Grade: F")
end

Nested if Statements

You can nest if statements within other if statements to create more complex conditions.

Example:

local age = 20
local hasID = true

if age >= 18 then
    if hasID then
        print("You can enter the club.")
    else
        print("You need an ID to enter.")
    end
else
    print("You are too young to enter.")
end

Loops

Loops allow you to execute a block of code multiple times. Lua supports three types of loops: while, repeat-until, and for.

while Loops

The while loop executes a block of code as long as a specified condition is true.

Syntax:

while condition do
    -- code to execute as long as condition is true
end

Example:

local count = 1

while count <= 5 do
    print("Count is: " .. count)
    count = count + 1
end

repeat-until Loops

The repeat-until loop is similar to the while loop, but it checks the condition after executing the block of code. This means the code block will always execute at least once.

Syntax:

repeat
    -- code to execute
until condition

Example:

local count = 1

repeat
    print("Count is: " .. count)
    count = count + 1
until count > 5

for Loops

The for loop is used to iterate over a range of values. Lua supports numeric for loops and generic for loops.

Numeric for Loop

Syntax:

for variable = start, stop, step do
    -- code to execute
end

Example:

for i = 1, 5 do
    print("i is: " .. i)
end

Generic for Loop

The generic for loop is used to iterate over all elements in a table.

Syntax:

for key, value in pairs(table) do
    -- code to execute
end

Example:

local fruits = {"apple", "banana", "cherry"}

for index, fruit in ipairs(fruits) do
    print("Fruit " .. index .. ": " .. fruit)
end

Practical Exercises

Exercise 1: Conditional Statements

Write a Lua script that checks if a number is positive, negative, or zero and prints the result.

Solution:

local number = -5

if number > 0 then
    print("The number is positive.")
elseif number < 0 then
    print("The number is negative.")
else
    print("The number is zero.")
end

Exercise 2: while Loop

Write a Lua script that prints the first 10 natural numbers using a while loop.

Solution:

local count = 1

while count <= 10 do
    print(count)
    count = count + 1
end

Exercise 3: for Loop

Write a Lua script that prints all even numbers from 1 to 20 using a for loop.

Solution:

for i = 1, 20 do
    if i % 2 == 0 then
        print(i)
    end
end

Summary

In this section, you learned about control structures in Lua, including conditional statements (if, if-else, elseif, and nested if statements) and loops (while, repeat-until, and for loops). These constructs are essential for controlling the flow of your program and making decisions based on conditions. Practice using these control structures to become more comfortable with them, as they are fundamental to writing effective Lua scripts.

© Copyright 2024. All rights reserved