# Lua Practice Exercises: Essential Techniques for Mastering the Framework

Here’s a practice exercise to test your understanding of Lua basics:

Write a Lua program that does the following:

1. Prints the message “Hello, world!” to the console.
2. Assigns the value 10 to a variable called ‘num’.
3. Checks whether the value of ‘num’ is greater than 5. If it is, print the message “num is greater than 5” to the console. If it isn’t, print the message “num is less than or equal to 5”.
4. Use a ‘for’ loop to print the values of the numbers from 1 to 5 to the console.
5. Define a function that takes two parameters and returns their sum. Call the function with the values 5 and 7 and print the result to the console.

Here’s a possible solution:

```luaCopy code```-- 1. Print "Hello, world!" to the console.
print("Hello, world!")

-- 2. Assign the value 10 to a variable called 'num'.
num = 10

-- 3. Check whether 'num' is greater than 5 and print the result.
if num > 5 then
print("num is greater than 5")
else
print("num is less than or equal to 5")
end

-- 4. Use a 'for' loop to print the values of the numbers from 1 to 5 to the console.
for i=1,5 do
print(i)
end

-- 5. Define a function that takes two parameters and returns their sum. Call the function with the values 5 and 7 and print the result to the console.
return x + y
end

print(result)
``````

This program should print the following output:

```pythonCopy code```Hello, world!
num is greater than 5
1
2
3
4
5
12``````
Tags: No tags