Lua is a simple and powerful programming language that is great for beginners. Today we will analyze the three pillars of algorithms: conditions, loops, and functions. No complicated words, just practice!

Conditions: teaching the program to make decisions
Imagine that you are saying to a friend: "If it's raining outside, take an umbrella." You need to explain it to the computer in the same way!
Simple if condition
local погода = "Rain"
if погода == "Rain" then
print("Let's take an umbrella!")
endThe program checks: if the weather is "rain", then it displays a message.
Condition with if-else alternative
local возраст = 16
if возраст >= 18 then
print("You can drive a car")
else
print("It's too early, you need to grow up")
endIf the age is greater than or equal to 18, the program will display one message, otherwise — another.
Several elseif options
local баллы = 85
if баллы >= 90 then
print("Rating: excellent!")
elseif баллы >= 70 then
print("Grade: good")
elseif баллы >= 50 then
print("Grade: satisfactory")
else
print("You need to improve your knowledge")
endThe program checks the conditions in order and executes the first suitable one.
Cycles: repeating actions
Why write the same code 100 times if you can tell the program: "Repeat this 100 times"?
While loop — while the condition is true
local счётчик = 1
while счётчик <= 5 do
print("Repeat number " .. счётчик)
счётчик = счётчик + 1
endThe program will repeat the action until the counter is greater than 5. Result:
Repetition number 1
Repetition number 2
Repetition number 3
Repetition number 4
Repeat number 5
The for loop — when we know how many times to repeat
-- От 1 до 10
for i = 1, 10 do
print("Number: " .. i)
end
-- От 10 до 1 с шагом -1
for i = 10, 1, -1 do
print("Countdown: " .. i)
endThis is the most convenient cycle when we know exactly the number of repetitions.
Repeat-until loop — at least once will be executed
local число = 0
repeat
print("Number: " .. число)
число = число + 1
until число > 3The difference from while: this loop first performs the action, and then checks the condition.
Practical example: multiplication table
local число = 7
print("Multiplication table " .. число)
for i = 1, 10 do
print(число .. " × " .. i .. " = " .. число * i)
endResult:
7 × 1 = 7
7 × 2 = 14
7 × 3 = 21
...and so on
Functions: creating our own commands
A function is like a recipe. We write it down once, and we use it many times.
Simple function
function поздороваться()
print("Hello, world!")
end
-- Вызываем функцию
поздороваться()Function with parameters
function поздороваться_с_именем(имя)
print("Hi, " .. имя .. "!")
end
поздороваться_с_именем("Alexey")
поздороваться_с_именем("Maria")Result:
Hi Alexey!
Hi Maria!
Function with return value
function сложить(a, b)
return a + b
end
local результат = сложить(5, 3)
print("5 + 3 = " .. результат) -- Выведет: 5 + 3 = 8Practical example: parity check
function чётное_или_нечётное(число)
if число % 2 == 0 then
return "even"
else
return "odd"
end
end
print("10 is " .. чётное_или_нечётное(10))
print("7 is " .. чётное_или_нечётное(7))
Let's combine everything together
The real power of programming is when we combine conditions, loops, and functions!
-- Функция проверяет, простое ли число
function простое_число(n)
if n < 2 then
return false
end
for i = 2, n - 1 do
if n % i == 0 then
return false
end
end
return true
end
-- Найдём все простые числа от 1 до 20
print("Prime numbers from 1 to 20:")
for число = 1, 20 do
if простое_число(число) then
print(число)
end
endThis program will display: 2, 3, 5, 7, 11, 13, 17, 19
Useful tips for beginners
1. Indent — the code is easier to read:
-- Плохо
if x > 0 then
print("Positive")
end
-- Хорошо
if x > 0 then
print("Positive")
end2. Name the variables clearly:
-- Непонятно
local a = 25
-- Понятно
local возраст_пользователя = 253. Comment on the code:
-- Вычисляем площадь круга
local радиус = 5
local площадь = 3.14 * радиус * радиус4. Start with the simple — first write the code without functions, then move the repeating parts into functions.
Come to Code — here you will find structured programming courses for beginners. We teach not just syntax, but real programmer thinking, with practical projects and real examples.
Join our Telegram channel is a friendly community of developers where you can ask a question, share your code, find like-minded people and keep abreast of new materials. Programming becomes much easier when you have someone to discuss difficulties and share victories with!
Start your journey into the world of programming with Kodik! 🚀
